我正在尝试php活动记录,这是一个伟大的ORM,但是我处于停滞状态。
我已经看了谷歌,博客,phpactiverecord文档以及statckoverflow的天,但没有能够遇到一个合适的解决方案,这个问题。
我能够执行基本的CRUD(插入,获取,修改和删除)操作,但是一旦我使用静态的$validates_uniqueness_of过滤器验证对象属性,我得到
与消息致命错误:未捕获异常'ActiveRecord'DatabaseException'
exception 'PDOException' with message 'SQLSTATE[42S02]: Base table or表test_ar. 1模型"不存在"C:'wamp'www'test_AR'AR'lib'Connection.php on line 325
以下是我使用的完整代码:
<?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(虽然我真的不明白那里发生了什么)
http://www.phpactiverecord.org/projects/main/wiki/Validations validates_uniqueness_of
http://blog.felho.hu/what-is-new-in-php- 53-part2-late -static-binding.html
以及许多其他的。
平台和php版本
我使用php 5.3.4和使用夜间构建(2013年5月8日),我几乎没能得到我的头围绕这个。请告知如何纠正这个问题。
这个问题已经被作者解决了:
在github上与一些开发人员讨论后。这似乎是一个bug。
解决方法是在模型 中创建一个自定义验证器。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'); } }
我今天在一个自定义应用程序中突然出现了这个错误,而不是Drupal。
我的错误是完全相同的。
然而,问题是我在windows服务器上创建了应用程序,然后将其移动到基于linux的服务器上。
显然在创建基于windows的应用程序时运行良好,但在迁移到Linux后抛出了提到的错误。
问题是:
—一个用大写字符创建的数据库表,如statpages…当它被创建时,它被重命名为statpages,没有驼峰大小写字母。
一旦我进入应用程序,应用程序连接并从数据库中提取信息,我删除了不匹配的大写字母,应用程序发现数据库表很好,并像预期的那样工作。