简体   繁体   中英

Sonata Admin Bundle One-to-Many relationship not saving foreign ID

I have a problem with the SonataAdminBunle in combination with symfony 2.2. I have a Project entity and a ProjectImage entity and specified a One-to-Many relationship between these two like so:

class Project
{
    /**
     * @ORM\OneToMany(targetEntity="ProjectImage", mappedBy="project", cascade={"all"}, orphanRemoval=true)
     */
    private $images;
}

class ProjectImage
{

    /**
     * @ORM\ManyToOne(targetEntity="Project", inversedBy="images")
     * @ORM\JoinColumn(name="project_id", referencedColumnName="id")
     */
    private $project;
}

I've configured the ProjectAdmin and ProjectImageAdmin:

class ProjectAdmin extends Admin
{
    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
            ->add('title')
            ->add('website')
            ->add('description', 'textarea')
            ->add('year')
            ->add('tags')
            ->add('images', 'sonata_type_collection', array(
                            'by_reference' => false
            ), array(
                            'edit' => 'inline',
                            'inline' => 'table',
                            'sortable' => 'id',
            ))
            ;
    }
}

class ProjectImageAdmin extends Admin
{
    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
            ->add('file', 'file', array(
                          'required' => false
            ))
            ;
    }
}

The problem is that in the project_image table in the database the project_id is not saved, while all other data is and also the image is saved. Couldn't find a working answer anywhere else.

Although unrelated, I'd slighty tweak your One-to-Many annotation:

class Project
{
    /**
     * @ORM\OneToMany(targetEntity="ProjectImage", mappedBy="project", cascade={"persist"}, orphanRemoval=true)
     * @ORM\OrderBy({"id" = "ASC"})
     */
    private $images;
}

Back on track, your annotations and Sonata Admin forms look fine, so I'm pretty sure you're missing one of those methods in your Project entity class:

public function __construct() {
    $this->images = new \Doctrine\Common\Collections\ArrayCollection();
}

public function setImages($images)
{
    if (count($images) > 0) {
        foreach ($images as $i) {
            $this->addImage($i);
        }
    }

    return $this;
}

public function addImage(\Acme\YourBundle\Entity\ProjectImage $image)
{
    $image->setProject($this);

    $this->images->add($image);
}

public function removeImage(\Acme\YourBundle\Entity\ProjectImage $image)
{
    $this->images->removeElement($image);
}

public function getImages()
{
    return $this->Images;
}

And in your Admin class:

public function prePersist($project)
{
    $this->preUpdate($project);
}

public function preUpdate($project)
{
    $project->setImages($project->getImages());
}

Since some things have changed with Symfony form collection now adding the addChild() and removeChild() with the by_reference option set to false automatically persist the Collection and set the ID on the inverse side as expected.

Here is a full working version: https://gist.github.com/webdevilopers/1a01eb8c7a8290d0b951

protected function configureFormFields(FormMapper $formMapper)
{
    $formMapper
        ->add('childs', 'sonata_type_collection', array(
            'by_reference' => false
        ), array(
            'edit' => 'inline',
            'inline' => 'table'
        ))
    ;
}

The addChild() method must contain the setter for the parent on the child:

public function addChild($child)
{
    $child->setParent($this); // !important
    $this->childs[] = $child;
    return $this;
} 

You ca do it directly in the preUpdate function

    public function prePersist($societate)
{
    $this->preUpdate($societate);
}

public function preUpdate($societate)
{
    $conturi = $societate->getConturi();
    if (count($conturi) > 0) {
        foreach ($conturi as $cont) {
            $cont->setSocietate($societate);
        }
    }
}

浏览此链接http://sonata-project.org/bundles/doctrine-orm-admin/master/doc/reference/form_field_definition.html#advanced-usage-many-to-one此链接将帮助您了解很多关联在奏鸣曲管理包中映射。

One way that I solved the was to manually set all inverse-side associations through a custom Sonata model manager.

<?php

namespace Sample\AdminBundle\Model;

class ModelManager extends \Sonata\DoctrineORMAdminBundle\Model\ModelManager
{
    /**
     * {@inheritdoc}
     */
    public function create($object)
    {
        try {
            $entityManager = $this->getEntityManager($object);
            $entityManager->persist($object);
            $entityManager->flush();
            $this->persistAssociations($object);
        } catch (\PDOException $e) {
            throw new ModelManagerException('', 0, $e);
        }
    }

    /**
     * {@inheritdoc}
     */
    public function update($object)
    {       
        try {
            $entityManager = $this->getEntityManager($object);
            $entityManager->persist($object);
            $entityManager->flush();
            $this->persistAssociations($object);
        } catch (\PDOException $e) {
            throw new ModelManagerException('', 0, $e);
        }
    }

    /**
     * Persist owning side associations
     */
    public function persistAssociations($object)
    {       
        $associations = $this
            ->getMetadata(get_class($object))
            ->getAssociationMappings();

        if ($associations) {
            $entityManager = $this->getEntityManager($object);

            foreach ($associations as $field => $mapping) {
                if ($mapping['isOwningSide'] == false) {
                    if ($owningObjects = $object->{'get' . ucfirst($mapping['fieldName'])}()) {
                        foreach ($owningObjects as $owningObject) {
                            $owningObject->{'set' . ucfirst($mapping['mappedBy']) }($object);
                            $entityManager->persist($owningObject);
                        }
                        $entityManager->flush();
                    }
                }
            }
        }
    }
}

Be sure to define this as a new service in your services.yml file:

services:
    sample.model.manager:
        class: Sample\AdminBundle\Model\ModelManager
        arguments: [@doctrine]


    sample.admin.business:
        class: Sample\AdminBundle\Admin\BusinessAdmin
        tags:
            - { name: sonata.admin, manager_type: orm, group: "Venues", label: "Venue" }
        arguments: [~, Sample\AppBundle\Entity\Business, ~]
        calls:
            - [ setContainer, [@service_container]]
            - [ setModelManager, [@sample.model.manager]]
public function prePersist($user)
{
    $this->preUpdate($user);
}

public function preUpdate($user)
{
    $user->setProperties($user->getProperties());
}

This has perfectly solved the problem for me thanks !

the simplest solution is, and naturally replace your variable names; this works for me: 'by_reference' => false

as follows:

protected function configureFormFields(FormMapper $formMapper)
{
    $formMapper
        ->add('name')
        ->add('articles', EntityType::class, array(
            'class' => 'EboxoneAdminBundle:Article',
            'required' => false,
            'by_reference' => false,
            'multiple' => true,
            'expanded' => false,
        ))
        ->add('formInputs', CollectionType::class, array(
            'entry_type' => FormInputType::class,
            'allow_add' => true,
            'allow_delete' => true,
            'by_reference' => false,
        ))
    ;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM