PHP ActiveRecord异常:找不到基表或视图
问题说明
我正在尝试php活动记录,这是一个很棒的ORM,但是我处于停滞状态.
I am trying out php active record, it's a great ORM, however I am at a standstill.
几天来,我一直在四处浏览Google,博客,phpactiverecord文档以及statckoverflow,但未能找到解决此问题的合适方法.
I have looked around 谷歌, blogs, phpactiverecord documentation as well as statckoverflow for days but have not been able to come across a suitable solution to this problem.
我能够执行基本的CRUD(插入,获取,修改和删除)操作,但是一旦我使用静态的$ validates_uniqueness_of过滤器验证了对象属性,我就会得到
I am able to carry out the basic CRUD (insert,fetch, modify and delete) operations however as soon as i validate an object property using a static $validates_uniqueness_of filter, i get
致命错误:未捕获的异常'ActiveRecord \ DatabaseException'
Fatal error: Uncaught exception 'ActiveRecord\DatabaseException'
有消息
消息为"SQLSTATE [42S02]"的异常"PDOException":基表或 找不到视图:1146中的表"test_ar.models'不存在" 325行上的C:\ wamp \ www \ test_AR \ AR \ lib \ Connection.php
exception 'PDOException' with message 'SQLSTATE[42S02]: Base table or view not found: 1146 Table 'test_ar.models' doesn't exist' in C:\wamp\www\test_AR\AR\lib\Connection.php on line 325
这是我完整使用的代码.
Here is the code i used in full.
<?php
$path_to_AR = "AR/";
include $path_to_AR . "activerecord.php";
ActiveRecord\Config::initialize(function($cfg) {
$cfg->set_model_directory('model');
$cfg->set_connections(
array(
'development' => 'mysql://root:asdf@localhost/test_ar',
'test' => 'mysql://username:password@localhost/test_database_name',
'production' => 'mysql://username:password@localhost/production_database_name'
)
);
});
/*
class user extends ActiveRecord\Model
{
static $validates_presence_of = array(array('username', 'message' => 'Please supply a username')); //this works just fine
static $validates_uniqueness_of = array(array('username'));//this line causes the PDO exception
}
*/
$user = new user();
user::create((array('username'=>'mike','password'=>'test','created'=>time())));
$user::create(array('username'=>'mike')); //cannot even reach this line because of the exeption
我尝试过/看过的参考书
https://github.com/kla/php-activerecord/issues/274 (尽管我不太了解那里发生的事情)
https://github.com/kla/php-activerecord/issues/274 (though i don't really understand what's going on there)
http://www.phpactiverecord.org/projects/main/wiki/验证次数#validates_uniqueness_of
http://blog.felho.hu/what-is-new-in-php-53-part-2-late-static-binding.html
http:// blog.felho.hu/what-is-new-in-php-53-part-2-late-static-binding.html
以及许多其他.
平台和php版本
我正在使用php 5.3.4并使用每夜构建(2013年5月8日),我几乎没办法解决这个问题.请提供有关纠正方法的建议.
I am using php 5.3.4 and using nightly build (May 8 2013) I have almost failed to get my head around this. Please advise on how to correct this.
正确答案
问题作者已解决了这个问题:
This has been solved by the question author:
在github上与一些开发人员讨论之后.看来这是一个错误.
After discussing it with a few developers on github. It seems this is a bug.
解决方法是在模型中创建自定义验证器
The work around was to create a custom validator in the model
public function validate() {
if ($this->is_new_record() && static::exists(array('conditions' => array('username' => $this->username)))) {
$this->errors->add('username', 'This username is already taken');
}
}
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tanhcfkbac
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
检查邮件是否发送成功
it1352 07-25 -
Android微调工具-删除当前选择
it1352 06-20 -
希伯来语的空格句子标记化错误
it1352 06-22 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20