hasMany 关系没有 table 有 id 列
hasMany relation without table having id column
我正在使用 CakePHP 2.5
。我正在关注 table
公司主管:
- company_master_id[PK]
- 名称和其他列
CompanySignatoryDetails:(单个公司有多个所有者)
- company_signatory_details_id[PK]
- company_master_id[FK]
- 名称和其他列
现在,我想获得该公司所有所有者的公司详细信息。这是我尝试过的。
$this->CompanyMaster->bindModel(
array(
'hasMany' => array(
'CompanySignatoryDetails' => array(
'className' => 'CompanySignatoryDetails',
'foreignKey' => false,
'conditions' => array(
'CompanySignatoryDetails.company_master_id = CompanyMaster.company_master_id'
),
),
)
)
);
$this->CompanyMaster->recursive = 2;
$company = $this->CompanyMaster->find('first', array(
'fields' => array('CompanyMaster.*'),
'conditions' => $conditions, //company id in condition
));
我收到以下错误:
Database Error
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'CompanyMaster.id' in 'field list'
SQL Query:
SELECT `CompanyMaster`.*, `CompanyMaster`.`id` FROM `crawler_output`.`company_master` AS `CompanyMaster` WHERE `CompanyMaster`.`company_master_id` = 1 LIMIT 1
请告诉我如何在没有 id
作为列名的情况下绑定模型。
好吧,例如,让您的查询看起来像这样:
select CompanyMaster.*,CompanySignatoryDetails.* from
CompanyMaster as cm inner join CompanySignatoryDetails as cd on
cm.company_master_id=cd.company_master_id
order by cm.company_master_id;
You will get all fields from two tables, ordered by company_master_id field. You may reduce number of fields, displayed by this query, by explicitly designate them like this:
select cm.company_master_id, cd.name from....
HNY!(新年快乐!!)
CakePHP 在处理 hasMany
关系时将生成单独的查询,因此您将无法从另一个 table 引用字段。只有 belongsTo
和 hasOne
关系会产生 JOIN
。
但是,您不需要为关系添加条件。以下应该可以正常工作:
$this->CompanyMaster->bindModel(array(
'hasMany' => array(
'CompanySignatoryDetails' => array(
'className' => 'CompanySignatoryDetails',
'foreignKey' => 'company_master_id',
),
)
));
不要忘记为 CompanyMaster
:
定义主键
class CompanyMaster extends AppModel
{
public $primaryKey = 'company_master_id';
}
和CompanySignatoryDetails
:
class CompanySignatoryDetails extends AppModel
{
public $primaryKey = 'company_signatory_details_id';
}
我正在使用 CakePHP 2.5
。我正在关注 table
公司主管:
- company_master_id[PK]
- 名称和其他列
CompanySignatoryDetails:(单个公司有多个所有者)
- company_signatory_details_id[PK]
- company_master_id[FK]
- 名称和其他列
现在,我想获得该公司所有所有者的公司详细信息。这是我尝试过的。
$this->CompanyMaster->bindModel(
array(
'hasMany' => array(
'CompanySignatoryDetails' => array(
'className' => 'CompanySignatoryDetails',
'foreignKey' => false,
'conditions' => array(
'CompanySignatoryDetails.company_master_id = CompanyMaster.company_master_id'
),
),
)
)
);
$this->CompanyMaster->recursive = 2;
$company = $this->CompanyMaster->find('first', array(
'fields' => array('CompanyMaster.*'),
'conditions' => $conditions, //company id in condition
));
我收到以下错误:
Database Error
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'CompanyMaster.id' in 'field list'
SQL Query:
SELECT `CompanyMaster`.*, `CompanyMaster`.`id` FROM `crawler_output`.`company_master` AS `CompanyMaster` WHERE `CompanyMaster`.`company_master_id` = 1 LIMIT 1
请告诉我如何在没有 id
作为列名的情况下绑定模型。
好吧,例如,让您的查询看起来像这样:
select CompanyMaster.*,CompanySignatoryDetails.* from CompanyMaster as cm inner join CompanySignatoryDetails as cd on cm.company_master_id=cd.company_master_id order by cm.company_master_id;
You will get all fields from two tables, ordered by company_master_id field. You may reduce number of fields, displayed by this query, by explicitly designate them like this:
select cm.company_master_id, cd.name from....
HNY!(新年快乐!!)
CakePHP 在处理 hasMany
关系时将生成单独的查询,因此您将无法从另一个 table 引用字段。只有 belongsTo
和 hasOne
关系会产生 JOIN
。
但是,您不需要为关系添加条件。以下应该可以正常工作:
$this->CompanyMaster->bindModel(array(
'hasMany' => array(
'CompanySignatoryDetails' => array(
'className' => 'CompanySignatoryDetails',
'foreignKey' => 'company_master_id',
),
)
));
不要忘记为 CompanyMaster
:
class CompanyMaster extends AppModel
{
public $primaryKey = 'company_master_id';
}
和CompanySignatoryDetails
:
class CompanySignatoryDetails extends AppModel
{
public $primaryKey = 'company_signatory_details_id';
}