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

标签 php doctrine-orm zend-framework2

我正在尝试使 Doctrine 注释与 ZF2 Form 注释一起工作。

我的 Controller 看起来像这样:

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, 与消息: [语义错误] 类 Users\Entity\User 中的注释“@Zend\Form\Annotation\Name”不存在,或者无法自动加载。

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

最佳答案

解决了!

注册自动加载命名空间是必要的,因为 Doctrine 使用它自己的自动加载机制。我需要使用 AnnnotationRegistry 及其 registerAutoloadNamespace 方法。它接受包含注释的命名空间 (Zend\Form\Annotation) 和根命名空间的基本目录(在我的例子中是 C:\Program Files\Zend\Apache2\htdocs\CommunicationApp\vendor\ZF2\library)作为它的参数。 更多信息:Doctrine Annotations

我的实体现在看起来像这样(并且可以使用 Doctrine 和 Zend Form 注释):

namespace Users\Entity;

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

use Doctrine\Common\Annotations\AnnotationRegistry;
$pathToZF2Library = __DIR__.'/../../../../../vendor/ZF2/library/';
AnnotationRegistry::registerAutoloadNamespace('Zend\Form\Annotation', $pathToZF2Library);

/** 
* @ORM\Entity
* @ORM\Table(name="users")
* @Annotation\Name("Users") 
*/
class User {

    /** 
    * @ORM\Id @ORM\Column(type="integer")
    * @ORM\GeneratedValue
    * @Annotation\Type("Zend\Form\Element\Hidden")
    */
    protected $id;

    /** 
    * @ORM\Column(type="string")
    * @Annotation\Type("Zend\Form\Element\Text")
    * @Annotation\Filter({"name":"StripTags"})
    * @Annotation\Filter({"name":"StringTrim"})
    * @Annotation\Validator({"name":"Alnum", "options": {"allowWhiteSpace":"true"}})
    * @Annotation\Validator({"name":"StringLength", "options": {"min":"2", "max":"25"}})
    * @Annotation\Options({"label":"Username: "})
    */
    protected $username;

    /** 
    * @ORM\Column(type="string")
    * @Annotation\Type("Zend\Form\Element\Email")
    * @Annotation\Filter({"name":"StripTags"})
    * @Annotation\Filter({"name":"StringTrim"})
    * @Annotation\Validator({"name":"EmailAddress", "options": {"domain":"true"}})
    * @Annotation\Options({"label":"Email: "})
    */
    protected $email;

    /** 
    * @ORM\Column(type="string")
    * @Annotation\Type("Zend\Form\Element\Password")
    * @Annotation\Filter({"name":"StripTags"})
    * @Annotation\Filter({"name":"StringTrim"})
    * @Annotation\Options({"label":"Password: "})
    */
    protected $password;

    public function __construct() {
    }

    public function setId($id) {
        $this->id = $id;
    }

    public function getId() {
        return $this->id;
    }

    public function setUsername($username) {
        $this->username = $username;
    }

    public function getUsername() {
        return $this->username;
    }

    public function setEmail($email) {
        $this->email = $email;
    }

    public function getEmail() {
        return $this->email;
    }

    public function setPassword($password) {
        $this->password = $password;
    }

    public function getPassword() {
        return $this->password;
    }
}

关于php - 如何使学说和 ZF2 形式注释一起工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20454426/

相关文章:

php - 获取具有所有属性的 Laravel 模型

mysql - 如何与实体经理一起加入 Doctrine ?

php - Doctrine DBAL 可以与 ORM Query Builder 混合使用吗?

php - Zend Form Hydrator 类方法未正确绑定(bind)到对象实体的问题

php - 在 zend Framework 2 的模型中获取数据库适配器

PHP mkdir 0777 失败 chmod 0777 工作

header 重定向后 PHP $_SESSION 为空

Symfony2 应用程序在缓存清除后无法工作

zend-framework2 - 在 Zend Framework 2 中注册自定义 Translator Loader

php - 替换最后 x 个数字