如何使用PHP-activerecord管理关系

时间:2013-08-02 08:28:38

标签: php codeigniter hmvc phpactiverecord

我在Code Igniter中有一个HMVC扩展的现有项目。也通过sparc使用php-activerecord。

对于一个新模块,我创建了两个具有一对多关系的表,直接在DB级别(在MySQL中)强制执行。表是'nassignment'(PK id)和'nassignment_assignee'(FK assignment_id)。

在模型下,我创建了两个类'nassignment.php',其类为Nassignment,'nassignment_assignee.php',类名为Nassignment_assignee。

以下是我在控制器中尝试的代码

$this->load->model("nassignment");
$nAssignmentArr = $this->nassignment->all();

并收到以下错误

[Fri Aug 02 13:50:52 2013] [error] [client 127.0.0.1] PHP Fatal error:  Uncaught 
exception 'ReflectionException' with message 'Class Nassignmentassignee does not exist' 
in /home/kapil/projects/<project>/application/sparks/php-activerecord/0.0.2/vendor/php-
activerecord/lib/Reflections.php:33\nStack trace:\n#0 
/home/kapil/projects/<project>/application/sparks/php-activerecord/0.0.2/vendor/php-
activerecord/lib/Reflections.php(33): ReflectionClass-
>__construct('Nassignmentassi...')\n#1 
/home/kapil/projects/<project>/application/sparks/php-activerecord/0.0.2/vendor/php-
activerecord/lib/Relationship.php(286): ActiveRecord\\Reflections-
>add('Nassignmentassi...')\n#2 /home/kapil/projects/<project>/application/sparks/php-
activerecord/0.0.2/vendor/php-activerecord/lib/Relationship.php(281): 
ActiveRecord\\AbstractRelationship->set_class_name('Nassignmentassi...')\n#3 
/home/kapil/projects/<project>/application/sparks/php-activerecord/0.0.2/vendor/php-
activerecord/lib/Relationship.php(464): ActiveRecord\\AbstractRelationship-
>set_inferred_class_name()\n#4 /home/kapil/projects/<project>/application/sparks/php-
activerecord/0.0 in /home/kapil/projects/<project>/application/sparks/php-
activerecord/0.0.2/vendor/php-activerecord/lib/Reflections.php on line 33

模型代码是

nassignment.php

class Nassignment Extends ActiveRecord\Model{
    static $table_name = 'nassignment';
    //Work fine if I remove following line, but do not get data
    static $has_many = array(
            array('nassignment_assignee')
        );
}

nassignment_assignee.php

class Nassignment_assignee Extends ActiveRecord\Model{
    static $table_name = 'nassignment_assignee';
}

任何建议,为什么它不起作用

1 个答案:

答案 0 :(得分:1)

尝试确保文件名与类的名称相匹配,以防万一。见the solution to my issue

显然,库也必须以UCfirst命名约定命名。