嵌入式表单有什么问题?

时间:2014-04-18 18:31:53

标签: php symfony doctrine-orm

有两个实体(人n:1手机),我想在personEntity中嵌入phoneEntity。 我做了基于symfony2嵌入式表单教程的任何事情,但我发现了这个错误:

  

通过这种关系找到了一个新的实体   ' S1 \ EphoneBundle \实体\ PersonEntity#phoneEntity'那不是   配置为级联实体的持久化操作:   S1 \ EphoneBundle \实体\ PhoneEntity @ 0000000033dcabf90000000076607b90。   解决此问题:显式调用EntityManager#persist()on   这个未知的实体或配置级联持久存在此关联   映射例如@ManyToOne(..,cascade = {" persist"})。如果你   无法找出导致问题实施的实体   ' S1 \ EphoneBundle \实体\ PhoneEntity #__的toString()'得到一个线索。

以下是我在personEntityController中的新动作:

public function newAction()
{
    $entity = new PersonEntity();


    $ph1 = new PhoneEntity();
    $entity->getPhoneEntity()->add($ph1);

    $em = $this->getDoctrine()->getManager();
    $em->persist($entity);
    $em->persist($ph1);

    $form   = $this->createCreateForm($entity);

    return $this->render('S1EphoneBundle:PersonEntity:new.html.twig', array(
        'entity' => $entity,
        'form'   => $form->createView(),
    ));
}

我的人类型:

<?php

namespace S1\EphoneBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class PersonEntityType extends AbstractType
{
        /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name')
            ->add('address')
            ->add('type')
            ->add('birthDay');
        $builder
            ->add('phoneEntity', 'collection', array(
                'type' => new PhoneEntityType(),
                'allow_add' => true
                )

            )
        ;
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'S1\EphoneBundle\Entity\PersonEntity'
        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 's1_ephonebundle_personentity';
    }
}

我的人实体:

<?php
namespace S1\EphoneBundle\Entity;

use Doctrine\ORM\Mapping AS ORM;



/**
 * @ORM\Entity
 */
class PersonEntity
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255, nullable=false)
     */
    private $name;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $address;

    /**
     * @ORM\Column(type="boolean", nullable=true)
     */
    private $type;

    /**
     * @ORM\Column(type="date", nullable=true)
     */
    private $birthDay;

    /**
     * @ORM\OneToMany(targetEntity="PhoneEntity", mappedBy="personEntity")
     */
    private $phoneEntity;
    /**
     * Constructor
     */
    public function __construct()
    {
        $this->phoneEntity = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     * @return PersonEntity
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string 
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set address
     *
     * @param string $address
     * @return PersonEntity
     */
    public function setAddress($address)
    {
        $this->address = $address;

        return $this;
    }

    /**
     * Get address
     *
     * @return string 
     */
    public function getAddress()
    {
        return $this->address;
    }

    /**
     * Set type
     *
     * @param boolean $type
     * @return PersonEntity
     */
    public function setType($type)
    {
        $this->type = $type;

        return $this;
    }

    /**
     * Get type
     *
     * @return boolean 
     */
    public function getType()
    {
        return $this->type;
    }

    /**
     * Set birthDay
     *
     * @param \DateTime $birthDay
     * @return PersonEntity
     */
    public function setBirthDay($birthDay)
    {
        $this->birthDay = $birthDay;

        return $this;
    }

    /**
     * Get birthDay
     *
     * @return \DateTime 
     */
    public function getBirthDay()
    {
        return $this->birthDay;
    }

    /**
     * Add phoneEntity
     *
     * @param \S1\EphoneBundle\Entity\PhoneEntity $phoneEntity
     * @return PersonEntity
     */
    public function addPhoneEntity(\S1\EphoneBundle\Entity\PhoneEntity $phoneEntity)
    {
        $this->phoneEntity[] = $phoneEntity;

        return $this;
    }

    /**
     * Remove phoneEntity
     *
     * @param \S1\EphoneBundle\Entity\PhoneEntity $phoneEntity
     */
    public function removePhoneEntity(\S1\EphoneBundle\Entity\PhoneEntity $phoneEntity)
    {
        $this->phoneEntity->removeElement($phoneEntity);
    }

    /**
     * Get phoneEntity
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getPhoneEntity()
    {
        return $this->phoneEntity;
    }
    public function __toString()
    {
        return $this->id."->".$this->name;
    }
}

我的手机实体:

<?php
namespace S1\EphoneBundle\Entity;

use Doctrine\ORM\Mapping AS ORM;

/**
 * @ORM\Entity
 */
class PhoneEntity
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=12, nullable=false)
     */
    private $number;

    /**
     * @ORM\ManyToOne(targetEntity="PersonEntity", inversedBy="phoneEntity", cascade={"persist"})
     * @ORM\JoinColumn(name="pid", referencedColumnName="id")
     */
    private $personEntity;

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set number
     *
     * @param string $number
     * @return PhoneEntity
     */
    public function setNumber($number)
    {
        $this->number = $number;

        return $this;
    }

    /**
     * Get number
     *
     * @return string 
     */
    public function getNumber()
    {
        return $this->number;
    }

    /**
     * Set personEntity
     *
     * @param \S1\EphoneBundle\Entity\PersonEntity $personEntity
     * @return PhoneEntity
     */
    public function setPersonEntity(\S1\EphoneBundle\Entity\PersonEntity $personEntity)
    {
        $this->personEntity = $personEntity;

        return $this;
    }

    /**
     * Get personEntity
     *
     * @return \S1\EphoneBundle\Entity\PersonEntity 
     */
    public function getPersonEntity()
    {
        return $this->personEntity;
    }
}

2 个答案:

答案 0 :(得分:0)

当您persist() PersonEntity然后PhoneEntity个实体时,Doctrine2正在寻找id PersonEntity,以便将此人与电话相关联。缺少此id会导致错误。

您必须在2 flush()次操作之间致电persist()

# this is needed at the start of the file
use Symfony\Component\HttpFoundation\Request;

public function newAction(Request $request)
{
    $entity = new PersonEntity();

    $ph1 = new PhoneEntity();
    $entity->addPhoneEntity($ph1);

    $form = $this->createForm(new PersonEntityType(), $entity);

    $form->handleRequest($request);

    if ($form->isValid())
    {
        $em = $this->getDoctrine()->getManager();
        $em->persist($ph1);
        $em->flush();
        $em->persist($entity);
        $em->flush();
    }

    return $this->render('S1EphoneBundle:PersonEntity:new.html.twig', array(
        'entity' => $entity,
        'form'   => $form->createView(),
    ));
}

答案 1 :(得分:0)

试试这个

 /**
 * Add phoneEntity
 *
 * @param \S1\EphoneBundle\Entity\PhoneEntity $phoneEntity
 * @return PersonEntity
 */
public function addPhoneEntity(\S1\EphoneBundle\Entity\PhoneEntity $phoneEntity)
{
    $this->phoneEntity[] = $phoneEntity;
     $phoneEntity->setPersonEntity($this);
    return $this;
}


 /**
 * set PhoneEntity
 *
 * @param \Doctrine\Common\Collections\ArrayCollection $phones
 */
public function setPhoneEntity(\Doctrine\Common\Collections\ArrayCollection $phones) {
    foreach ($phones as $phone) {
        $phone->setPersonEntity($this);
    }
    $this->phoneEntity = $phones;
}
相关问题