11

我在将 SonataAdminBunle 与 symfony 2.2 结合使用时遇到问题。我有一个 Project 实体和一个 ProjectImage 实体,并指定了这两者之间的一对多关系,如下所示:

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;
}

我已经配置了 ProjectAdmin 和 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
            ))
            ;
    }
}

问题是在数据库的 project_image 表中,project_id 没有保存,而所有其他数据都保存了,图像也保存了。在其他任何地方都找不到有效的答案。

4

6 回答 6

26

虽然不相关,但我会稍微调整您的一对多注释:

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

回到正轨,您的注释和 Sonata 管理表单看起来不错,所以我很确定您在 Project 实体类中缺少这些方法之一:

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;
}

在您的管理员课程中:

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

public function preUpdate($project)
{
    $project->setImages($project->getImages());
}
于 2013-06-07T22:33:08.713 回答
9

由于 Symfony 表单集合发生了一些变化,现在添加addChild()removeChild()并将by_reference选项设置为false会自动保存集合并按预期在反面设置 ID。

这是一个完整的工作版本: 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'
        ))
    ;
}

addChild()方法必须包含子级父级的设置器:

public function addChild($child)
{
    $child->setParent($this); // !important
    $this->childs[] = $child;
    return $this;
} 
于 2015-03-31T12:58:09.773 回答
2

您可以直接在 preUpdate 函数中执行此操作

    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);
        }
    }
}
于 2014-12-31T21:09:13.220 回答
1

最简单的解决方案是,自然地替换你的变量名;这对我有用:'by_reference' => false

如下:

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,
        ))
    ;
}
于 2018-10-29T09:44:42.043 回答
0

我解决的一种方法是通过自定义 Sonata 模型管理器手动设置所有反向关联。

<?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();
                    }
                }
            }
        }
    }
}

请务必在 services.yml 文件中将其定义为新服务:

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]]
于 2014-03-12T19:16:31.267 回答
0
public function prePersist($user)
{
    $this->preUpdate($user);
}

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

这完美地解决了我的问题,谢谢!

于 2017-02-17T14:50:44.987 回答