如何使学说和ZF2形式的注释一起工作

 Chickny的造梦空间 发布于 2023-02-12 11:01

我试图让Doctrine注释与ZF2 Form注释一起工作.

我的控制器看起来像这样:

namespace Users\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

//Doctrine Stuff
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator;
use DoctrineORMModule\Form\Annotation\AnnotationBuilder;

class IndexController extends AbstractActionController {

    private $entityManager;

    public function getEntityManager() {
        if (!$this->entityManager) {
            $paths = array (
                    realpath ( dirname ( __FILE__ ) . '/../Entity' )
            );
            $isDevMode = true;
            // the connection configuration
            $dbParams = array (
                    'driver' => 'pdo_mysql',
                    'user' => 'root',
                    'password' => 'my_password',
                    'dbname' => 'commapp'
            );
        $config = Setup::createAnnotationMetadataConfiguration ( $paths, $isDevMode, null, null, false );
        $this->entityManager = EntityManager::create ( $dbParams, $config );
        }
        return $this->entityManager;
    }

    public function updateAction() {
        $entityManager = $this->getEntityManager ();

        $repository = $entityManager->getRepository ( 'Users\Entity\User' );
        $id = $this->params ()->fromRoute ( 'id' );
        $user = $repository->findOneBy (array('id' => $id));

        $builder = new AnnotationBuilder ( $entityManager );
        $form = $builder->createForm ( $user );

        $form->setHydrator ( new DoctrineHydrator ( $entityManager, 'Users\Entity\User' ) );
        $form->bind ( $user ); 

        $send = new Element ( 'send' );
        $send->setValue ( 'Create' ); // submit
        $send->setAttributes ( array ('type' => 'submit' ) );
        $form->add ( $send );

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


}

和实体看起来像这样:

namespace Users\Entity;

use Doctrine\ORM\Mapping as ORM;
use Zend\Form\Annotation as Form;

/**
 * @ORM\Entity
 * @ORM\Table(name="users")
 * @Form\Name("user")
 * @Form\Hydrator("Zend\Stdlib\Hydrator\ObjectProperty")
 */
class User
{

    /** 
     * @var int
     * @ORM\Id @ORM\Column(name="id", type="integer")
     * @ORM\GeneratedValue
     * @Form\Exclude()
     */
    protected $id;

    /** 
     * @var string
     * @ORM\Column(name="user_name", type="string", length=255, nullable=false)
     * @Form\Filter({"name":"StringTrim"})
     * @Form\Validator({"name":"StringLength", "options":{"min":1, "max":25}})
     * @Form\Validator({"name":"Regex", "options":{"pattern":"/^[a-zA-Z][a-zA-Z0-9_-]{0,24}$/"}})
     * @Form\Attributes({"type":"text"})
     * @Form\Options({"label":"Username:"})
     */
    protected $username;

    /** 
     * @var string
     * @ORM\Column(name="email", type="string", length=255, unique=true)
     * @Form\Type("Zend\Form\Element\Email")
     * @Form\Options({"label":"Your email address:"})
     */
    protected $email;

}

当我输入URL commapp/users/index/update/1时,我应该为ID = 1的用户显示表单.相反,我得到了 Doctrine\Common\Annotations\AnnotationException消息: [Semantical Error] The annotation "@Zend\Form\Annotation\Name" in class Users\Entity\User does not exist, or could not be auto-loaded.

我看不出我做错了什么......?

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有