php - Symfony2 - 使用其他属性保存ManyToMany

时间:2015-03-17 16:49:35

标签: php symfony many-to-many entity symfony-forms

我有一个表格,其中包含实体计划,实体组织和另一个程序组织,它们构成了第一个程序之间的关系。

此实体ProgramOrganization与OneToMany / ManyToOne相关,因为它包含其他字段。

问题在于表单是否正确显示,但是当我想将其保存在我的数据库中时,我收到了以下消息:

“找到类型的实体... \ NomBundle \ Entity \ Organization on association ... \ NomBundle \ Entity \ Program#programmeOrganisations,但期待...... \ NomBundle \ Entity \ ProgramOrganization”

我使用SensioGeneratorBundle和学说:shema:validate是正确的。

另一方面,如果删除此关系,一切都会正确保存。

以下是我的代码摘录:(属性未翻译)

实体计划:

/**
 * @ORM\OneToMany(targetEntity="ProgrammeOrganisation", mappedBy="programme")
 */
protected $programmeOrganisations;
 
 /**
 * Constructor
 */
public function __construct()
{
    $this->programmeOrganisations = new \Doctrine\Common\Collections\ArrayCollection();
}
 
 /**
 * Add programmeOrganisations
 *
 * @param \...\...\Entity\ProgrammeOrganisation $programmeOrganisations
 * @return Programme
 */
public function addProgrammeOrganisation(\...\Entity\ProgrammeOrganisation $programmeOrganisations)
{
    $this->programmeOrganisations[] = $programmeOrganisations;
     
    return $this;
}
 
/**
 * Remove programmeOrganisations
 *
 * @param \...\...\Entity\ProgrammeOrganisation $programmeOrganisations
 */
public function removeProgrammeOrganisation(\...\...\Entity\ProgrammeOrganisation $programmeOrganisations)
{
    $this->programmeOrganisations->removeElement($programmeOrganisations);
}
 
/**
 * Get programmeOrganisations
 *
 * @return \Doctrine\Common\Collections\Collection
 */
public function getProgrammeOrganisations()
{
    return $this->programmeOrganisations;
}

实体组织:

/**
 *
 * @ORM\OneToMany(targetEntity="ProgrammeOrganisation", mappedBy="organisation")
 */
protected $programmeOrganisations;
 
/**
 * Constructor
 */
public function __construct()
{
    $this->programmeOrganisations = new \Doctrine\Common\Collections\ArrayCollection();
}
 
/**
 * Add programmeOrganisations
 *
 * @param \...\...\Entity\ProgrammeOrganisation $programmeOrganisations
 * @return Organisation
 */
public function addProgrammeOrganisation(\...\...\Entity\ProgrammeOrganisation $programmeOrganisations)
{
    $this->programmeOrganisations[] = $programmeOrganisations;
 
    return $this;
}
 
/**
 * Remove programmeOrganisations
 *
 * @param \...\...\Entity\ProgrammeOrganisation $programmeOrganisations
 */
public function removeProgrammeOrganisation(\...\...\Entity\ProgrammeOrganisation $programmeOrganisations)
{
    $this->programmeOrganisations->removeElement($programmeOrganisations);
}
 
/**
 * Get programmeOrganisations
 *
 * @return \Doctrine\Common\Collections\Collection
 */
public function getProgrammeOrganisations()
{
    return $this->programmeOrganisations;
}

实体计划组织:

class ProgrammeOrganisation
{
    /**
     *
     * @ORM\Id
     * @ORM\ManyToOne(targetEntity="Programme", inversedBy="programmeOrganisations")
     * @ORM\JoinColumn(name="programme_id", referencedColumnName="id")
     */
    private $programme;
 
    /**
     *
     * @ORM\Id
     * @ORM\ManyToOne(targetEntity="Organisation", inversedBy="programmeOrganisations")
     * @ORM\JoinColumn(name="organisation_id", referencedColumnName="id")
     */
    private $organisation;
 
    /**
     * @var integer
     *
     * @ORM\ManyToOne(targetEntity="ContributionType")
     */
    private $contributionType;
 
    /**
     * @var \DateTime
     *
     * @ORM\Column(name="date", type="date", nullable=true)
     */
    private $date;
 
}

PROGRAMTYPE

->add('programmeOrganisations' , 'entity' , array(
                      'class'    => 'NomBundle:Organisation' ,
                      'label' => 'Organisations list',
                      'property' => 'label' ,
                      'expanded' => false ,
                      'multiple' => true ,
                       
                    ))

ProgramController

public function createAction(Request $request)
    {
        $entity = new Programme();
        $form = $this->createCreateForm($entity);
        $form->handleRequest($request);
 
        if ($form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $em->persist($entity);
            $em->flush();
 
            return $this->redirect($this->generateUrl('programme_show', array('id' => $entity->getId())));
        }
 
        return array(
            'entity' => $entity,
            'form'   => $form->createView(),
        );
    }
 
    /**
     * Creates a form to create a Programme entity.
     *
     * @param Programme $entity The entity
     *
     * @return \Symfony\Component\Form\Form The form
     */
    private function createCreateForm(Programme $entity)
    {
        $form = $this->createForm(new ProgrammeType(), $entity, array(
            'action' => $this->generateUrl('programme_create'),
            'method' => 'POST',
        ));
 
        $form->add('submit', 'submit', array('label' => 'Save'));
 
        return $form;
    }
 
    /**
     * Displays a form to create a new Programme entity.
     *
     * @Route("/new", name="programme_new")
     * @Method("GET")
     * @Template()
     */
    public function newAction()
    {
        $entity = new Programme();
        $form   = $this->createCreateForm($entity);
 
        return array(
            'entity' => $entity,
            'form'   => $form->createView(),
        );
    }

1 个答案:

答案 0 :(得分:0)

我认为您的错误在这里,我没有看到您的程序和组织之间的链接,但您的表单类型表明我想要一个与$ ProgrammeOrganizations变量链接的组织实体,该变量被声明为ProgrammeOrganization实体而非组织。

 ->add('programmeOrganisations' , 'entity' , array(
                  'class'    => 'NomBundle:ProgrammeOrganisation ' ,
                  'label' => 'Organisations list',
                  'property' => 'label' ,
                  'expanded' => false ,
                  'multiple' => true ,

                ))

应该这样做。也许还有其他错误,我没有注意到