ZF2原则OneToMany注释表单

时间:2014-05-29 16:46:13

标签: doctrine-orm zend-framework2 one-to-many

我有以下实体:

  1. 用户

    /**
     * Users
     *
     * @ORM\Table(name="users")
     * @ORM\Entity(repositoryClass="Users\Entity\Repository\UsersRepository")
     * @Annotation\Name("user")
     * @Annotation\Hydrator("Zend\Stdlib\Hydrator\ClassMethods")
     */
    class User
    {   
    /**
         * @var string
         *
         * @ORM\Column(name="name", type="string", length=100, nullable=false)
         * @Annotation\Filter({"name":"StringTrim"})
         * @Annotation\Validator({"name":"StringLength", "options":{"min":2, "max":100}})
         * @Annotation\Attributes({"type":"text","class":"form-control"})
         * @Annotation\Options({"label":"Full name:"})
         */
        private $name;
    
    /**
         * @var User\Entity\UserItem
         *
         * @ORM\OneToMany(targetEntity="User\Entity\UserItem", mappedBy="user")
         * @Annotation\Exclude()
         */
        private $items;
    
        /**
         * @var integer
         *
         * @ORM\Column(name="id", type="integer")
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="AUTO")
         * @Annotation\Exclude()
         */
        private $id;
    
    public function __construct()
        {
                $this->vessels = new ArrayCollection();
        }
    
     public function setName($name)
        {
            $this->name = $name;
    
            return $this;
        }
    
        public function getName()
        {
            return $this->name;
        }
    
     public function getId()
        {
            return $this->id;
        }
    
    public function getItems()
        {
                return $this->items
        }
    
        public function setItems($items)
        {
                $this->itemr = $items;
                return $this;
        }
    
  2. 项目

     /**
     * Items
     * @ORM\Entity
     * @ORM\Table(name="items")
     * @ORM\Entity(repositoryClass="User\Entity\Repository\ItemsRepository")
     * @Annotation\Name("item")
     * @Annotation\Hydrator("Zend\Stdlib\Hydrator\ClassMethods")
     */
      class Item
       {
        /**
         * @var string
         *
         * @ORM\Column(name="name", type="string", length=100, nullable=false)
         * @Annotation\Filter({"name":"StringTrim"})
         * @Annotation\Validator({"name":"StringLength", "options":{"min":2, "max":100}})
         * @Annotation\Attributes({"type":"text","class":"form-control"})
         * @Annotation\Options({"label":"Name:"})
         */
         private $name;
    
        /**
         * @var integer
         *
         * @ORM\Column(name="id", type="integer")
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="AUTO")
         * @Annotation\Exclude()
         */
         private $id;
    
  3. 最后实体保持这两者之间的关系 UserItem

     /**
      * UserItem
      *
      * @ORM\Table(name="users_items")
      * @ORM\Entity(repositoryClass="User\Entity\Repository\UserItemsRepository")
      * @Annotation\Name("user_item")
      * @Annotation\Hydrator("Zend\Stdlib\Hydrator\ClassMethods")
      */
     class UserItem{    
         /**
          * @var Users\Entity\User
          *
          * @ORM\ManyToOne(targetEntity="Users\Entity\User", inversedBy="items")
          * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
          * @Annotation\Exclude()
          */
         private $user;
    
         /**
          * @var User\Entity\Item
          *
          * @ORM\ManyToOne(targetEntity="User\Entity\Item", inversedBy="users")
          * @ORM\JoinColumn(name="item_id", referencedColumnName="id")
          * @Annotation\Type("DoctrineModule\Form\Element\ObjectSelect")
          */
         private $item;
    
         /**
          * @var integer
          *
          * @ORM\Column(name="id", type="integer")
          * @ORM\Id
          * @ORM\GeneratedValue(strategy="AUTO")
          * @Annotation\Exclude()
          */
         private $id;
    
  4. 通过这些实体,我可以创建和编辑用户和项目。表单由注释生成。 我的问题是我如何生成表单给用户分配项目。优选地,作为具有多个选择的选择字段。提交原则后应该保存关系。

    我写了这个表单来表示要选择的项目:

    class ItemsForm extends Form
    {
        public function __construct($entityManager, $name = null)
        {
            parent::__construct('items');
            $this->setAttribute('method', 'post');
            $this->add(array(
                'type' => 'DoctrineModule\Form\Element\ObjectSelect',
                'name' => 'items',
                'attributes' => array(
                    'id' => 'selectItems',
                    'multiple' => true,
                    'data-placeholder' => 'Items'
                ),
                'options' => array(
                    'object_manager' => $entityManager,
                    'target_class' => 'User\Entity\Item',
                    'property' => 'name',
                    ),
                )
            );        
        }
    }
    

    现在我不知道如何预选用户已分配的项目。

1 个答案:

答案 0 :(得分:0)

在值中的选择字段中,您必须具有来自项目的ID。提交后你可以做类似的事情

foreach($post['items'] as $itemId)
{
    $userItem = new UserItem();
    $userItem->user = $this->getEntityManager()->getReference('User', $userId);
    $userItem->item = $this->getEntityManager()->getReference('Item', $itemId);
    $this->getEntityManager()->save($userItem);
}