- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在尝试使 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/
我正在对一款流行游戏 (Minecraft) 进行一些修改,我在地形生成中看到了这些线条, double d4 = 1.0D; d4 *= d4; d4 *= d4; d4 = 1.0D - d4;
如何在没有浮点单元的处理器上执行 float 学运算?例如低端 8 位微 Controller 。 最佳答案 看看这篇文章:http://www.edwardrosten.com/code/fp_te
抱歉这个冗长的标题。我的代码针对没有浮点单元的微 Controller (msp430),但这应该适用于任何类似的 MCU。 如果我将一个大的运行时变量与通常被认为是浮点十进制数 (1.8) 相乘,M
我偶尔会在这个方法中遇到 stackoverflow 异常。 double norm_cdf(const double x) { double k = 1.0/(1.0 + 0.2316419
这是一个让我在过去几个小时里完全困惑的问题...... 我的程序中有一个硬编码的方程式: double s2; s2 = -(0*13)/84+6/42-0/84+24/12+(6*13)/42; 每
我知道 float 学充其量是丑陋的,但我想知道是否有人可以解释以下怪癖。在我测试的大多数编程语言中,将 0.4 添加到 0.2 会产生轻微错误,而 0.4 + 0.1 + 0.1 则不会。 两者计算
随着数据量持续增长,对合格数据专业人员的需求也会增长。具体而言,对SQL流利的专业人士的需求日益增长,而不仅仅是在初级层面。 因此,Stratascratch的创始人Nathan Rosidi以及我觉
当使用 c++ 或 -O0 编译时,以下 -O1 程序给出了数值不同的结果。 #include #include #include #include int main() { std::a
我正在尝试使用 SVG 在 map 上绘制飞行路径。我在 Leaflet 之上使用 d3,但所使用的框架不会对我的问题产生影响 - 这是三 Angular 关系。 http://fiddle.jshe
使用 IEEE754 float (在 JavaScript 中)时,与数学相关的精度损失风险是什么? 10*.1 即整数乘以有理数。 最佳答案 注意:该问题经过编辑,在发布此答案后很长时间添加了“t
我需要为网站 UI 做一些基本的 float 学运算(金钱的加法和乘法)。我知道 Javascript float 由于存储方式的原因并不准确,但我也知道以某种方式,可以获得我所需的准确度。我知道这一
我有一些像下面这样的宏: #define THING_LENGTH (512) #define MAX_COUNT (4096*8) #define MAX_LENGTH ((int32)((floa
我认为这是一个非常基本的问题 - 我正在执行此功能: private double convertMetersToFeet(double meters) { //function converts
我想在不损失太多精度的情况下替换这些函数中的 float 学,因为我没有 FPU。这可能吗?我认为逗号后的 3 个数字就足够了。 inline float smaller_f(float value,
我需要一个类来表示 double vector (在数学意义上)。 我需要的特殊功能: 任意维度 vector (我通常使用 10 - 100,000 维度) 高性能(用于受 CPU 限制的数字代码)
此社区 Wiki 问题的公认答案:What are best practices that you use when writing Objective-C and Cocoa?说 iPhone 不能
This question already has an answer here: Trouble with float on C [duplicate]
以下代码有问题: private const int movementMultiplier = 2; void Test() { XmlNode xnXCoordinate = xd.Sele
大家早上好 我在 float 学方面遇到了一些问题,完全迷失在“.to_f”、“*100”和“.0”中! 我希望有人能帮助我解决我的具体问题,并准确解释他们的解决方案为何有效,以便我下次理解这一点。
我的嵌入式 C 代码在具有单精度 FPU 的 Cortex M4F 上运行。我担心编译器多久将基于软件的 double 学放在诸如 ** float_var1 = 3.0 * int_var / fl
我是一名优秀的程序员,十分优秀!