duanshan188866 2013-06-10 10:43
浏览 167
已采纳

PHP activerecord异常:找不到基表或视图

I am trying out php active record, it's a great ORM, however I am at a standstill.

I have looked around google, blogs, phpactiverecord documentation as well as statckoverflow for days but have not been able to come across a suitable solution to this problem.

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

Fatal error: Uncaught exception 'ActiveRecord\DatabaseException'

With message

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

References i have tried/looked at

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/Validations#validates_uniqueness_of

http:// blog.felho.hu/what-is-new-in-php-53-part-2-late-static-binding.html

as well as many others.

Platform and php version

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.

  • 写回答

2条回答 默认 最新

  • douciping4283 2013-06-10 19:36
    关注

    This has been solved by the question author:

    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');
            }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化