开发者

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. 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. To put it more simply, users-form_data is a one-to-many relationship. My DataMapper models look like this:

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:

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',开发者_Go百科 $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:

{"id":1,"form_type_id":"3","create_date":"1306241597","created_by":[],"status":"a"}

Notice how created_by is an empty array? The column in my table is definitely called created_by. The odd thing is that when I change this column to user_id and change the form_data class to have this:

var $has_one = array('form_type', 'user');

...and change the user class to have this:

var $has_many = array('form_data') //among others

Everything works perfectly, and I get the right value for 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

Thanks


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

and try to avoid underscores in table names, datamapper uses it at so many places to identify tables relation internally


I was getting same error. It was caused by lack of parent::__construct() in constructor.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜