Cakephp 修订行为会阻碍验证错误?
我有一个 CakePHP 票务应用程序,我正在使用 修订行为以保留每个工单的修订历史记录。我使用此行为时遇到的问题是,它不显示验证错误消息。这是我在模型中添加的行。
public $actsAs = array('Revision' => array('limit'=>10));
当我评论这一行时,它会显示错误消息,否则不会显示。另外,当我使用 x-debug 对其进行调试时,我看到validationErrors 变量已设置并且所有错误消息值均已正确设置。
请在这里阐明一些情况。
编辑:我正在使用 Cake 2.1
I have a CakePHP ticketing app where I am using Revision Behavior to keep revision history of each tickets. The problem I have using this behavior is, it does not display validation error messages. Here is the line I have added in the model.
public $actsAs = array('Revision' => array('limit'=>10));
When I comment this line, it displays error messages and otherwise it does not. Also, when I debug it using x-debug, I see validationErrors variable is set and has all error message values set properly.
Please shed some light here.
Edit: I am using Cake 2.1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,请务必获取此行为的最新版本:http:// alkemann.googlecode.com/svn/trunk/models/behaviors/revision.php
用于集成到 CAKE 2.X 中,问题来自于createShadowModel() 函数:
$Model->ShadowModel->alias = $Model->alias;
该行为为基本模型及其将保存在 _revs 表中的影子模型提供了相同的别名,这似乎弄乱了验证消息。
问题是,当您访问模型时,会自动加载此行为,并且即使您的输入未验证,也会调用 createShadowModel() 函数。
解决方案之一是注释掉 createShadowModel() 中的这一行,然后仅将其添加到将在 DB 中进行操作的行为中的每个函数。肯定有比这更好的方法,比如在 setup() 中检测是否需要进一步初始化,但找不到如何做到这一点。所以这是我至少允许在 Cake 2.X 中使用此行为的第一步。
First, be sure to get the last version of this behavior : http://alkemann.googlecode.com/svn/trunk/models/behaviors/revision.php
for integration in CAKE 2.X, the problem comes from line 980 in the createShadowModel() function:
$Model->ShadowModel->alias = $Model->alias;
The behavior gives the same alias to the base model and its shadowmodel it will save in the _revs table and that seems to mess up the validation messages.
The problem is that this behavior is loaded automatically when you access your model, and the createShadowModel() function is called even if your input doesn't validate.
One of the solution would be to comment out this line from createShadowModel() and then to add it only to every function in the behavior that will make an operation in the DB . There is surely a better way than that, like detecting in the setup() if there is need to go further to initialization, but couldn't find how to do it. So that's my first step towards at least allowing to use this behavior in Cake 2.X.
这里可能会发生一些事情。由于我们没有您的任何代码,因此无法简单地告诉您发生了什么。然而,我非常确定这种行为(因为它是在 2008 年编写的)对于刚刚发布了第一个 alpha 的 CakePHP 2.1 版本来说会存在问题。 Cake 的基础设施发生了很多变化,可能会导致它不起作用。我想说这可能适用于 1.3 版本,而且肯定适用于 1.2 版本,但如果没有更新,可能无法获得对 2.1 的支持。
也就是说,这是一种行为,应该只改变模型代码。因此,(理论上)应该不会影响您的观点。您确定在代码中使用正确的约定来显示错误(即使将其注释掉会更改显示的消息)。
我会寻找 2.0+ 兼容版本的行为。或者,您可以将代码放在 Github 上并开始自行移植。您可能会从一些蛋糕人那里得到一些帮助。
There are a few things that could be happening here. Too much for one to simply tell you what is happening since we don't have any of your code. However, I am pretty certain that this behavior, being that it was written in 2008, will have issues with CakePHP version 2.1, which just released its first alpha. There have been a lot of changes to the infrastructure of Cake that could cause this not to work. I'd say this would probably work with version 1.3 and definitely with 1.2, but getting support for 2.1 probably won't happen without updates.
That said, this is a behavior, which should only alter model code. So, there should be not impact (theoretically) on your view. Are you sure you are using the proper conventions in your code to display errors (even though commenting it out changes displayed messages).
I'd look for a 2.0+ compatible version of the behavior. Or, you could throw the code on Github and start to port it yourself. You may get some help from some Cake people.