[英]Using an aliased field in a CodeIgniter DataMapper ORM many-to-one relationship
I'm currently modeling out a website using the DataMapper ORM for CodeIgniter.我目前正在使用 DataMapper ORM 为 CodeIgniter 建模一个网站。 At the moment, I have a
users
table that has all the standard user information, including an id, and a form_data
table that has its own id and a field called created_by
which links to the id
column on the users
table.目前,我有一个包含所有标准用户信息的
users
表,包括一个 id 和一个form_data
表,它有自己的 id 和一个名为created_by
的字段,它链接到users
表上的id
列。 To put it more simply, users-form_data is a one-to-many relationship.更简单地说,users-form_data 是一对多的关系。 My DataMapper models look like this:
我的 DataMapper 模型如下所示:
User用户
class user extends DataMapper {
var $has_many = array(
'form_data_created_by' => array(
'class' => 'form_data',
'other_field' => 'created_by'
)
);
}
...and more, of course, but edited here for brevity ...当然还有更多,但为简洁起见在此编辑
And my form_data model looks like this:我的 form_data model 看起来像这样:
class form_data extends DataMapper {
var $table = 'form_data';
var $has_one = array(
'form_type',
'created_by' => array(
'class' => 'user',
'other_field' => 'form_data_created_by'
)
);
}
Now here's the thing.现在事情就是这样。 When I run this code:
当我运行此代码时:
$form_type = new form_type();
$form_data = $form_type->where('app_id', $app_id)->get()->form_data->get()->all_to_array();
It throws an error that is something like this:它会抛出一个类似这样的错误:
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: datamapper/array.php
Line Number: 53
And when I echo out the JSON of the $form_data object, I get this:当我回显 $form_data object 的 JSON 时,我得到了这个:
{"id":1,"form_type_id":"3","create_date":"1306241597","created_by":[],"status":"a"}
Notice how created_by
is an empty array?注意
created_by
是一个空数组吗? The column in my table is definitely called created_by
.我表中的列绝对称为
created_by
。 The odd thing is that when I change this column to user_id
and change the form_data
class to have this:奇怪的是,当我将此列更改为
user_id
并将form_data
class 更改为:
var $has_one = array('form_type', 'user');
...and change the user
class to have this: ...并将
user
class 更改为:
var $has_many = array('form_data') //among others
Everything works perfectly, and I get the right value for user_id
.一切正常,我得到了
user_id
的正确值。
So can anyone prod me in the right direction?那么任何人都可以在正确的方向上刺激我吗? I've been using this page as a guide: http://datamapper.wanwizard.eu/pages/advancedrelations.html
我一直将此页面用作指南: http://datamapper.wanwizard.eu/pages/advancedrelations.html
Thanks谢谢
I was getting same error.我遇到了同样的错误。 It was caused by lack of
parent::__construct()
in constructor.这是由于构造函数中缺少
parent::__construct()
造成的。
try to mention a "join_table"=>"where both user and form_data are" other wise datamapper will look for this relation in a separate table named form_datas_users尝试提及“join_table”=>“user 和 form_data 都在哪里”,其他明智的数据映射器将在名为 form_datas_users 的单独表中查找此关系
and try to avoid underscores in table names, datamapper uses it at so many places to identify tables relation internally并尽量避免表名中的下划线,datamapper 在很多地方都使用它来在内部识别表关系
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.