简体   繁体   English

如何使用PHP-activerecord管理关系

[英]How to manage relation using PHP-activerecord

I've an existing project in Code Igniter with HMVC extension. 我在Code Igniter中已有一个带有HMVC扩展的项目。 Also using php-activerecord through sparc. 还通过sparc使用php-activerecord。

For a new module, I created two tables with one-to-many relationship, enforced directly at DB level (in MySQL). 对于新模块,我创建了具有一对多关系的两个表,这些表直接在数据库级别(在MySQL中)执行。 Tables are say 'nassignment' (PK id) and 'nassignment_assignee' (FK assignment_id). 这些表称为“ nassignment”(PK ID)和“ nassignment_assignee”(FK assignment_id)。

Under models, I created two class 'nassignment.php' having class Nassignment and 'nassignment_assignee.php' with class name Nassignment_assignee. 在模型下,我创建了两个类“ nassignment.php”,其类分别为Nassignment和“ nassignment_assignee.php”,其类名为Nassignment_assignee。

Below is the code I'm trying in controller 下面是我在控制器中尝试的代码

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

and getting following error 并得到以下错误

[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

Model code is 型号代码为

nassignment.php 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 nassignment_assignee.php

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

Any suggesations, why it is not working 任何建议,为什么不起作用

Try making sure that the name of the file matches the name of the class in case also. 尝试确保文件名与类名匹配,以防万一。 See the solution to my issue 查看我的问题的解决方案

Apparently, libraries too have to be named accordind to the UCfirst naming convention. 显然,也必须按照UCfirst命名约定将库命名为Accind。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM