Code Igniter中的Datamapper处理多个关系

时间:2014-03-07 07:58:07

标签: codeigniter datamapper

我有一个非常令人困惑的问题..

我配置了数据映射器并开始将它与代码点火器一起使用..我有这个类:

class FamilyMemberDetail extends DataMapper {
var $table = "family_members_details";
var $has_one = array('gender', 'maritalstate');

public function __toString() {
    return is_string($this->id) ? $this->id : "";
}

}

对于性别而言,它工作得很好..

class Gender extends DataMapper {
var $table = "genders";
var $has_many = array("familymemberdetail");

public function __toString() {
    return is_string($this->gender_label) ? $this->gender_label : "";
}

}

但是有了婚姻状况:

class MaritalState extends DataMapper {
var $table = "marital_state";
var $has_many = array("familymemberdetail");

public function __toString() {
    return is_string($this->marital_state_label) ? $this->marital_state_label : "";
}

}

我收到此错误!

Error Number: 1146

Table 'elkhoudary_family.kh_family_members_details_marital_state' doesn't exist

SELECT `kh_marital_state`.* FROM (`kh_marital_state`) LEFT OUTER JOIN `kh_family_members_details_marital_state` kh_family_members_details_marital_state ON `kh_marital_state`.`id` = `kh_family_members_details_marital_state`.`maritalstate_id` WHERE `kh_family_members_details_marital_state`.`familymemberdetail_id` = 1

Filename: C:\AppServ\www\elkhoudary\system\database\DB_driver.php

Line Number: 330

问题在于我没有指定这种关系需要一个连接表但是...... CI坚持认为它需要一个表格之间..它不应该像性别一样工作..什么我在这里失踪了吗?

1 个答案:

答案 0 :(得分:0)

好的,我发现了原因并且它真的很愚蠢......关系列应该命名为[className] _id而不是[tableName] _id,因为我的班级名为" MaritalState"和我的桌名" marital_state"。

所以我将所有实体改为' _而不是Camel案例,一切都很顺利..

class Family_member_detail extends DataMapper {
var $table = "family_members_details";
var $has_one = array('gender', 'marital_state');

public function __toString() {
    return is_string($this->id) ? $this->id : "";
}

}

性别:

class Gender extends DataMapper {
var $table = "genders";
var $has_many = array("family_member_detail");

public function __toString() {
    return is_string($this->gender_label) ? $this->gender_label : "";
}

}

婚姻状况:

class Marital_state extends DataMapper {
var $table = "marital_state";
var $has_many = array("family_member_detail");

public function __toString() {
    return is_string($this->marital_state_label) ? $this->marital_state_label : "";
}
}