实例如下:
如果查询加上field('cname'),返回的["Dept"] 值为空,不加上field('cname'),返回的["Dept"]值为think_dept表中关联数据。
1、没加field条件:
$list=$UserRelation->relation('Dept')->select();
dump($list);array(2) {
[0] => array(18) {
["id"] => string(1) "1"
["cname"] => string(7) "manager"
["Dept"] => array(0) {
}
}
[1] => array(18) {
["id"] => string(1) "2"
["cname"] => string(9) "David Lee"
["Dept"] => array(1) {
[0] => array(2) {
["id"] => string(2) "36"
["name"] => string(15) "技术支持部"
}
}
}
}2、加上field条件:$list=$UserRelation->field('cname')->relation('Dept')->select();
dump($list);array(2) {
[0] => array(2) {
["cname"] => string(7) "manager"
["Dept"] => array(0) {
}
}
[1] => array(2) {
["cname"] => string(9) "David Lee"
["Dept"] => array(0) {
}
}
}3、我的RelationModel类<?php
/*
*用户与部门关联模型
*/
class UserRelationModel extends RelationModel{
//定义主表
protected $tableName = 'User';
//定义关联关系
protected $_link = array(
'Dept' => array(
'mapping_type' => MANY_TO_MANY,
'relation_table' => 'flow_user_dept',
'foreign_key' => 'user_id',
'relation_key' => 'dept_id',
'mapping_fields' => 'id,name',
),
);
}
?> 最佳答案