Creating Object with Form FileType


#1

Hey there, i want to upload a Image with a form and save it into a Object. I try to worked it out with the description of pimcore doc “Working with Assets via PHP API” and “symfony FileType Field”, but i can not get it.

Someone has a sample here?

Thank you very much!

Here is my not working code:

public function newAction(Request $request)

{
$objectForm = $this->createForm(ObjectForm::class);
$objectForm->handleRequest($request);

    $objectFormSuccess = false;

    //start create object
    if ($objectForm->isSubmitted() && $objectForm->isValid()) {

        $objectFormSuccess = true;

        $data = $objectForm->getData();
        $objectFilename = File::getValidFilename("object-name");

        $myObject = new DataObject\MyObject();
        $myObject->setParent(DataObject\AbstractObject::getByPath("/myobjects/"));

        $objectLogoFile = $objectForm['objektLogo']->getData();
        $objectLogoFilename = File::getValidFilename("logo-file-name");

        $newAsset = new Pimcore\Model\Asset();
        $newAsset->setFilename($objectLogoFilename);
        $newAsset->setData(file_get_contents($objectLogoFile);
        $newAsset->setParent(Pimcore\Model\Asset::getByPath("/myobjectlogos"));
        $newAsset->save(["versionNote" => "my new version"]);
        $asset = $newAsset->getById();

        $myObject->setMyObjectLogo()->setData(file_get_contents($asset));
        
        $myObject->setKey($objectFilename);
        $myObject->setPublished(true);

        $myObject->setEmail($data['email']);

        $myObject->save();

        // add form data as view parameters
        $this->view->getParameters()->add($data);

    }
    //end create object
    $this->view->objectForm = $objectForm->createView();
    $this->view->objectFormSuccess = $objectFormSuccess;

}


#2

Hi,

i got it now.

delete this:
$asset = $newAsset->getById();

change this:
$myObject->setMyObjectLogo()->setData(file_get_contents($asset));
to:
$myObject->setMyObjectLogo(newAsset);

thx