简体   繁体   中英

Codeigniter datamapper one to many relationship

I have two table rostertype and rostertypepositions

在此处输入图片说明

I have created two model

class Roster_type extends DataMapper{

    var $table = "rostertype";
    var $has_many = array(
        'position'  =>  array(
            'class' =>  'Roster_typeposition',
            'other_field'   => 'roster_position',
            'join_self_as'    =>  '',
            'join_other_as' =>  'RosterType'
        )
    );
    function __construct($id = NULL){
        parent::__construct($id);
    }

    function getAllRoster(){

        return $this->count();
    }
}

class Roster_typeposition  extends DataMapper{

    var $table = "rostertypepositions";
    var $default_order_by = array('PositionIndex'=>'asc');
    var $has_one = array(
        'roster_position'   => array(
            'class' =>  'Roster_type',
            'other_field' => 'position',
            'join_self_as'    =>  'RosterType',
            'join_other_as' =>  ''
        )
    );
    function __construct($id = NULL){
        parent::__construct($id);
    }

    function get_all_positions(){
        $u =  $this->get_iterated();
        foreach ($u as $ab=>$d){
            echo "<pre>";print_r($d->roster_position->get());echo "</pre>";
        }
        die();
    }
}

But for some reason i am getting this error

Error Number: 1146

Table 'ad_8cceab3cf7883a5.rostertype_rostertypepositions' doesn't exist

SELECT `rostertype`.*
FROM (`rostertype`)
LEFT OUTER JOIN `rostertype_rostertypepositions` position_rostertype_rostertypepositions ON `rostertype`.`id` = `position_rostertype_rostertypepositions`.`_id`
WHERE `position_rostertype_rostertypepositions`.`RosterType_id` =  2

Filename: /Applications/MAMP/htdocs/IBM_bluemix/Development/Draftbeast-Dev/libraries/Datamapper.php

Line Number: 1344

I dont know how it is generating that table position_rostertype_rostertypepositions . Please help and what am i doing wrong.

Solved it by taking empty fields in model

class Roster_typeposition  extends DataMapper{
    var $model = 'rostertypepositions';
    var $table = "rostertypepositions";
    var $default_order_by = array('PositionIndex'=>'asc');
    var $has_one = array(
        'roster_position'   => array(
            'class' =>  'Roster_type',
            'other_field' => 'position',
            'join_self_as'    =>  'RosterType',
        )
    );

And

class Roster_type extends DataMapper{
    var $model = 'Roster_type';
    var $table = "rostertype";
    var $has_many = array(
        'position'  =>  array(
            'class' =>  'Roster_typeposition',
            'other_field'   => 'roster_position',
            'join_other_as' =>  'RosterType'
        )
    );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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