Symfony 必须管理传递到选项字段的实体。Can';t将项传递给数组字段

Symfony 必须管理传递到选项字段的实体。Can';t将项传递给数组字段,symfony,doctrine-orm,Symfony,Doctrine Orm,在尝试使用下面的操作打开表单时,我在Symfony 2中遇到以下错误 必须管理传递到选项字段的实体。也许会坚持下去 他们在实体管理器中?500内部服务器错误- 运行时异常 在提交表单之前,如何持久化依赖于另一个实体的实体,正确的方法是什么? 我正在尝试使用以下方法存储传递到表单的项: $imoved = new ItemMoved(); $imoved->setItem($Item); $entity->setItems(array($imoved)); 控制器动作 /** *

在尝试使用下面的操作打开表单时,我在Symfony 2中遇到以下错误

必须管理传递到选项字段的实体。也许会坚持下去 他们在实体管理器中?500内部服务器错误- 运行时异常

在提交表单之前,如何持久化依赖于另一个实体的实体,正确的方法是什么? 我正在尝试使用以下方法存储传递到表单的项:

$imoved = new ItemMoved();
$imoved->setItem($Item);
$entity->setItems(array($imoved));
控制器动作

/**
 * Displays a form to create a new MovementNotification entity.
 *
 * @Route("/new/{id}", name="am_movementnotification__new")
 * @Method("GET")
 * @Template()
 */
public function newAction($id)
{
    $entity = new MovementNotification();
    $em = $this->getDoctrine()->getManager();
    $Item = $em->getRepository("FarAssetManagerBundle:Item")->find($id+0);

    if (!$Item) {
        throw $this->createNotFoundException('Unable to find Item entity. ' . $id );
    }

    $token = $this->container->get('security.context')->getToken();
    if ($token != null ) {
        $user = $token->getUser();
        if (!is_object($user)) {
            // FIXME take some action in this case;
            $user = 0;
        } else {
            $user = $user->getId();
        }
    } else {
        $user = 0;
    }

    $entity->setUser($user);
    $imoved = new ItemMoved();
    $imoved->setItem($Item);
    $entity->setItems(array($imoved));
    $entity->setDatein(new \DateTime("now"));
    //$entity->setLocation($Item->getLocation());
    $entity->setState((string) new OpenState());
    //$entity->setState($Item->getState());

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

    return array(
        'entity' => $entity,
        'form'   => $form->createView(),
    );
}
实体

<?php

namespace Far\AssetManagerBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * MovementNotification
 *
 * @ORM\Table(name="far_assetmanager_movement_notification")
 * @ORM\Entity(repositoryClass="Far\AssetManagerBundle\Entity\MovementNotificationRepository")
 */
class MovementNotification
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var \Far\AssetManagerBundle\Entity\ItemMoved
     *
     * @ORM\OneToMany(targetEntity="Far\AssetManagerBundle\Entity\ItemMoved", mappedBy="MovementNotification", cascade={"persist"})
     * @ORM\JoinColumns({@ORM\JoinColumn(name="movement_notification_id", referencedColumnName="id")})
     */
    private $items;

    /**
     * @var \Far\AssetManagerBundle\Entity\Location
     *
     * @ORM\ManyToOne(targetEntity="Far\AssetManagerBundle\Entity\Location")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="location_id", referencedColumnName="id")
     * })
     */
    private $location;

    /**
     * @var string
     *
     * @ORM\Column(name="state", type="string", length=50)
     */
    private $state;

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

    /**
     * @var \Far\AssetManagerBundle\Entity\State
     *
     * @ORM\ManyToOne(targetEntity="Far\AssetManagerBundle\Entity\State")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="state_object_id", referencedColumnName="id")
     * })
     */
    private $stateObject;

    /**
     * @var \Far\StockBundle\Entity\User
     *
     * in order to avoid a hard dependency on a security
     * module i-ll just leave this has an int
     *
     * @ORM\Column(name="user_id", type="integer", nullable=true)
     */
    private $user;

    /**
     * @var \Far\AssetManagerBundle\Entity\MovementNotification
     *
     * @ORM\ManyToOne(targetEntity="Far\AssetManagerBundle\Entity\MovementNotification")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="parent_id", referencedColumnName="id", nullable=true)
     * })
     */
    private $parent;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="datein", type="datetime")
     */
    private $datein;

    /**
     * @var \Far\AssetManagerBundle\Entity\TransportNotice
     *
     * @ORM\ManyToOne(targetEntity="Far\AssetManagerBundle\Entity\TransportNotice")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="transport_id", referencedColumnName="id", nullable=true)
     * })
     */
    private $transport;


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

    /**
     * Set item
     *
     * @param integer $item
     *
     * @return MovementNotification
     */
    public function setItems($items)
    {
        $this->items = $items;

        return $this;
    }

    /**
     * Get item
     *
     * @return integer
     */
    public function getItems()
    {
        return $this->items;
    }

    /**
     * Set location
     *
     * @param Location $location
     *
     * @return MovementNotification
     */
    public function setLocation($location)
    {
        $this->location = $location;

        return $this;
    }

    /**
     * Get location
     *
     * @return Location
     */
    public function getLocation()
    {
        return $this->location;
    }

    /**
     * Set state
     *
     * @param string $state
     *
     * @return MovementNotification
     */
    public function setState($state)
    {
        $this->state = $state;

        return $this;
    }

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

    /**
     * Set descricao
     *
     * @param string $descricao
     *
     * @return MovementNotification
     */
    public function setDescricao($descricao)
    {
        $this->descricao = $descricao;

        return $this;
    }

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

    /**
     * Set stateObject
     *
     * @param integer $stateObject
     *
     * @return MovementNotification
     */
    public function setStateObject($stateObject)
    {
        $this->stateObject = $stateObject;

        return $this;
    }

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

    /**
     * Set user
     *
     * @param integer $user
     *
     * @return MovementNotification
     */
    public function setUser($user)
    {
        $this->user = $user;

        return $this;
    }

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

    /**
     * Set parent
     *
     * @param integer $parent
     *
     * @return MovementNotification
     */
    public function setParent($parent)
    {
        $this->parent = $parent;

        return $this;
    }

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

    /**
     * Set datein
     *
     * @param \DateTime $datein
     *
     * @return MovementNotification
     */
    public function setDatein($datein)
    {
        $this->datein = $datein;

        return $this;
    }

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

    /**
     * Set transport
     *
     * @param integer $transport
     *
     * @return MovementNotification
     */
    public function setTransport($transport)
    {
        $this->transport = $transport;

        return $this;
    }

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

    public function __toString() {
        return "N. Movimento: " . $this->getId();
    }
}


<?php

namespace Far\AssetManagerBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * ItemMoved
 *
 * @ORM\Table(name="far_assetmanager_movement_notification_item")
 * @ORM\Entity
 */
class ItemMoved
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * Set id
     *
     * @param integer $id
     * @return ItemMoved
     */
    public function setId($id)
    {
        $this->id = $id;
        return $this;
    }

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

    /**
     * @var \Far\AssetManagerBundle\Entity\Item
     *
     * @ORM\ManyToOne(targetEntity="Far\AssetManagerBundle\Entity\Item", inversedBy="items",cascade={"persist"})
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="item_id", referencedColumnName="id")
     * })
     */
    private $Item;

    public function setItem($item)
    {
        $this->Item = $item;
    }

    public function getItem()
    {
        return $this->Item;
    }

    /**
     * @var \Far\AssetManagerBundle\Entity\MovementNotification
     *
     * @ORM\ManyToOne(targetEntity="Far\AssetManagerBundle\Entity\MovementNotification")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="movement_notification_id", referencedColumnName="id", nullable=false)
     * })
     */
    private $MovementNotification;

    public function setMovementNotification($mn)
    {
        $this->MovementNotification = $mn;
        return $this;
    }

    public function getMovementNotification()
    {
        return $this->MovementNotification;
    }

    function __toString() {
        return $this->getItem()->getDescription() . " " . $this->getMovementNotification()->getId();
    }
}

显示表单类型,处理
MovementNotification
实体的表单类型。此外,
MovementNotification
实体中的
$items
属性应设置为
ItemMoved
集合。好的,我添加了MovementNotification的表单类型,我也收到了错误“此表单不应包含额外字段。”在表单中。
$form=$This->createForm($entity);
更改为
$form=$This->createForm($entity);
<?php

namespace Far\AssetManagerBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Far\AssetManagerBundle\Entity\LocationRepository;
use Far\AssetManagerBundle\Entity\StateRepository;

class MovementNotificationType extends AbstractType
{

    public function __construct($em, array $options = null)
    {
        $this->em = $em;

        if ($options != null && isset($options['type'])) {
            $this->type = $options['type'];
        }
    }

    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('descricao','textarea', array('attr' => array("cols" => 40 , "rows" => 10)))
            ->add('user', 'hidden')
            ->add('datein')
            ->add('items', 'collection', 
                array(
                    'type' =>
                    new ItemMovedType(
                            $this->em
                    ),
                    'allow_add' => false,
                    'allow_delete' => false,
                    'prototype' => false
                    )
                )
            ->add('location', 'entity', array(
                'class' => 'FarAssetManagerBundle:Location',
                'query_builder' => function (LocationRepository $er) {
                    return $er->createQueryBuilder('l')
                        ->orderBy('l.description', 'ASC');
                },
                'choice_label' => 'Description',
            ))
            ->add('state', 'hidden')
            ->add('stateObject', 'entity', array(
                'class' => 'FarAssetManagerBundle:State',
                'query_builder' => function (StateRepository $er) {
                    return $er->createQueryBuilder('st')
                        ->orderBy('st.description', 'ASC');
                },
                'choice_label' => 'Description',
            ))
            ->add('parent', 'hidden')
            ->add('transport', 'hidden')
        ;
    }

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

    /**
     * @return string
     */
    public function getName()
    {
        return 'far_assetmanagerbundle_movementnotification';
    }
}
<?php

namespace Far\AssetManagerBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Far\AssetManagerBundle\Entity\LocationRepository;
use Far\AssetManagerBundle\Entity\StateRepository;
use Far\AssetManagerBundle\Form\Type\ItemChoice;

class ItemMovedType extends AbstractType
{

    private $em;

    public function __construct($em, array $options = null)
    {
        $this->em = $em;
    }

    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {

        $builder
            ->add('Item'
                //->addModelTransformer($transformer)
            )
            ->add("movementNotification", 'hidden')
            ;
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Far\AssetManagerBundle\Entity\ItemMoved',
        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'far_assetmanagerbundle_item_movedtype';
    }
}
<?php

namespace Far\AssetManagerBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * ItemMoved
 *
 * @ORM\Table(name="far_assetmanager_movement_notification_item")
 * @ORM\Entity
 */
class ItemMoved
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * Set id
     *
     * @param integer $id
     * @return ItemMoved
     */
    public function setId($id)
    {
        $this->id = $id;
        return $this;
    }

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

    /**
     * @var \Far\AssetManagerBundle\Entity\Item
     *
     * @ORM\ManyToOne(targetEntity="Far\AssetManagerBundle\Entity\Item", inversedBy="items",cascade={"persist"})
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="item_id", referencedColumnName="id")
     * })
     */
    private $Item;

    public function setItem($item)
    {
        $this->Item = $item;
    }

    public function getItem()
    {
        return $this->Item;
    }

    /**
     * @var \Far\AssetManagerBundle\Entity\MovementNotification
     *
     * @ORM\ManyToOne(targetEntity="Far\AssetManagerBundle\Entity\MovementNotification")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="movement_notification_id", referencedColumnName="id", nullable=false)
     * })
     */
    private $MovementNotification;

    public function setMovementNotification($mn)
    {
        $this->MovementNotification = $mn;
        return $this;
    }

    public function getMovementNotification()
    {
        return $this->MovementNotification;
    }

    public function __toString() {
        if ($this->getItem() == null || $this->getMovementNotification() == null) {
            return "";
        } else {
            return $this->getItem()->getDescription() . " " . $this->getMovementNotification()->getId();
        }
    }
}