ZF2 Doctrine2:如何用关系实现表单?

时间:2014-09-25 09:05:09

标签: doctrine-orm zend-framework2 zend-form

我找到了一个实现带有实体注释的表单的教程。

Doctrine entity with zend form

我想知道如何用关系来实现。

这是我的代码......

我的实体:

/**
 * Account
 *
 * @ORM\Table(name="account", indexes={@ORM\Index(name="IDX_7D3656A4C6798DB", columns={"account_type_id"})})
 * @ORM\Entity(repositoryClass="Application\Repository\Account")
 * @Annotation\Name("account")
 */
class Account extends EntityAbstract implements InputFilterAwareInterface, UserInterface
{
    /**
     * @var Zend\InputFilter\InputFilter
     */
    protected $inputFilter;

    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="bigint", nullable=false, options={"input" = "hidden"})
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="SEQUENCE")
     * @ORM\SequenceGenerator(sequenceName="account_id_seq", allocationSize=1, initialValue=1)
     * @Annotation\Attributes({"type":"hidden"})
     */
    protected $id;

    /**
     * @var string
     *
     * @ORM\Column(name="code", type="string", nullable=false, unique=true, options={"input" = "text"})
     * @Annotation\Attributes({"type":"text", "class":"test"})
     * @Annotation\Options({"label":"Code"})
     */
    protected $code;

    /**
     * @var string
     *
     * @ORM\Column(name="password", type="string", nullable=false, options={"input" = "password"})
     * @Annotation\Attributes({"type":"text"})
     * @Annotation\Options({"label":"Mot de passe"})
     */
    protected $password;

    /**
     * @var string
     *
     * @ORM\Column(name="password_key", type="string", nullable=false, options={"input" = "password"})
     * @Annotation\Exclude()
     */
    protected $passwordKey;

    /**
     * @var boolean
     *
     * @ORM\Column(name="active", type="boolean", nullable=false, options={"input" = "checkbox"})
     * @Annotation\Attributes({"type":"checkbox"})
     * @Annotation\Options({"label":"Actif"})
     */
    protected $active = false;

    /**
     * @var string
     */
    protected $displayName;

    /**
     * @var boolean
     *
     * @ORM\Column(name="visible", type="boolean", nullable=false, options={"input" = "checkbox"})
     * @Annotation\Attributes({"type":"checkbox"})
     * @Annotation\Options({"label":"Visible"})
     */
    protected $visible = false;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="start_date", type="datetime", nullable=false, options={"input" = "date"})
     * @Annotation\Attributes({"type":"date"})
     * @Annotation\Options({"label":"Date de début"})
     */
    protected $startDate = 'now()';

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="end_date", type="datetime", nullable=true, options={"input" = "date"})
     * @Annotation\Attributes({"type":"date"})
     * @Annotation\Options({"label":"Date de fin"})
     */
    protected $endDate;

    /**
     * @var string
     *
     * @ORM\Column(name="token", type="string", nullable=false, options={"input" = "password"})
     * @Annotation\Exclude()
     */
    protected $token;

    /**
     * @var integer
     *
     * @ORM\Column(name="expires", type="integer", nullable=true)
     * @Annotation\Exclude()
     */
    protected $expires;

    /**
     * @var \Application\Entity\AccountType
     *
     * @ORM\ManyToOne(targetEntity="Application\Entity\AccountType", fetch="EAGER", inversedBy="accounts")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="account_type_id", referencedColumnName="id", nullable=false)
     * })
     * @Annotation\Type("DoctrineModule\Form\Element\ObjectSelect")
     * @Annotation\Options({"label":"Type de compte", "object_manager":"Doctrine\Common\Persistence\ObjectManager" ,"target_class":"Application\Entity\AccountType"})
     */
    protected $accountType;

    ...

我的控制器编辑操作:

    public function editAction()
    {
        try
        {
            $entityManager = $this->getEntityManager();
            $repository = $this->getRepository();
            $id = (int)$this->params()->fromRoute('id');
            $user = $repository->find($id);

            $builder = new AnnotationBuilder($entityManager);

            $form = $builder->createForm($user);
            $form->setHydrator(new DoctrineHydrator($entityManager,'Application\Entity\Account'));
            $form->bind($user);

            $view =  new ViewModel();
            $view->setVariable('form', $form);

            return $view;
        }
        catch(\Exception $e)
        {
            echo '<pre>';
            print_r($e->getMessage());
            echo '</pre>';
        }
    }

我发现了这个错误:

捕获致命错误:传递给DoctrineModule \ Form \ Element \ Proxy :: setObjectManager()的参数1必须实现接口Doctrine \ Common \ Persistence \ ObjectManager,在/ Users / thibaud / Sites / api / vendor /中调用的字符串第86行上的doctrine / doctrine-module / src / DoctrineModule / Form / Element / Proxy.php,在/ Users / thibaud / Sites / api / vendor / doctrine / doctrine-module / src / DoctrineModule / Form / Element / Proxy中定义。第185行的PHP

确定。我知道关系是如何运作的,以及如何建立一个带有关系的学说的形式,但不知道如何用anotations实现相同的关系。以下是我如何使用关系构建表单:

$this->add
(
    array
    (
        'name' => 'accountType',
        'type' => 'DoctrineModule\Form\Element\ObjectSelect',
        'options' => array
        (
            'label' => 'Type de compte',
            'object_manager' => $this->getObjectManager(),
            'target_class' => 'Application\Entity\AccountType',
            'label_generator' => function($target)
            {
                return $target->getName();
            }
        ),
    )
);

1 个答案:

答案 0 :(得分:1)

 use Zend\Form\Annotation as Form;

 /**
 *
 * @Form\Type("DoctrineModule\Form\Element\ObjectSelect")
 * @Form\Attributes({"required":"required"})
 * @Form\Required(true)
 * @Form\Options({
 *      "label":"Account",
 *      "empty_option": "Please choose",
 *      "target_class": "Application\Entity\Whatever",
 *      "property": "CoolProperty",
 *      "find_method": {
 *          "name": "findBy",
 *          "params": {
 *               "criteria": {"awesomeCriteria": 0},
 *               "orderBy": {"letsSayDate": "ASC"}
 *           }
 *      }
 * })
 */

为了清晰和简短的代码,我给了命名空间一个名为Form的别名。