有两个模型是项目和活动。
以下是关系:
var $belongsTo = array(
'Project' => array(
'className' => 'Project',
'foreignKey' => 'project_id'
),
var $hasMany = array(
'Event' => array(
'className' => 'Event',
'foreignKey' => false,
'dependent' => false,
),
问题在于,每当我尝试列出所有项目时,只列出附有事件的项目。 我期待着解决这个问题。
以下是我得到的查询:
选择
Event
。id
,Event
。project_id
,Event
。user_id
,Event
。{{1} },date
。Event
,hours
。Event
,minutes
。Event
,xhours
。Event
,xminutes
。Event
,xdetails
。Event
,assignment
。Event
,start_time
。Project
,{{ 1}}。id
,Project
。name
,Project
。customer_id
,Project
。project_nr
,{{1} }。Project
,address
。Project
,post_nr
。Project
,city
。Project
,company_id
。User
,id
。User
,employee_nr
。User
,name
。User
,surname
。{{ 1}},User
。User
。password
,User
。role
,User
。{{1} },phone
。User
,address
。User
,post_nr
。User
FROMcity
。User
AStoken_hash
LEFT JOINUser
。company_id
ASscheduling
ON(events
。Event
=scheduling
。projects
) LEFT JOINProject
。Event
ASproject_id
ON(Project
。id
=scheduling
。users
)WHERE 1 = 1 LIMIT 15
我在上次更新后获得的查询如下:
选择
User
。Event
,user_id
。User
,id
。Event
,id
。{{1} },Event
。project_id
,Event
。user_id
,Event
。date
,Event
。hours
,Event
。minutes
,Event
。xhours
,Event
。xminutes
,Event
。xdetails
,{{ 1}}。Event
,assignment
。Event
,start_time
。Event
,material_id
。Event
,{{1} }。km_drive
,Event
。time_drive
,Event
。finish_time
,Project
。id
,Project
。name
,Project
。customer_id
,Project
。project_nr
,Project
。address
,Project
。{{ 1}},post_nr
。Project
,city
。Project
,company_id
。Project
,color
。{{1} },User
。id
,User
。employee_nr
,User
。name
,User
。surname
,User
。User
。password
,User
。role
,User
。phone
,{{ 1}}。User
FROMaddress
。User
ASpost_nr
LEFT JOINUser
。city
ASUser
ON({{ 1}}。token_hash
=User
。company_id
)LEFT JOINUser
。car_id
ASscheduling
ON(events
。{ {1}} =Event
。scheduling
)WHERE 1 = 1 LIMIT 20
这是我在尝试列出项目时获得的数组:
projects
所以基本上我得到了相关项目的事件列表,没有相关的客户。我想得到的是这样的数组:
Project
答案 0 :(得分:1)
活动模型
class Event extends AppModel {
public $actsAs = array('Containable');
var $belongsTo = array(
'Project' => array(
'className' => 'Project',
'foreignKey' => 'project_id'
),
// ... more relationships
);
项目模型
class Project extends AppModel {
public $actsAs = array('Containable');
var $hasMany = array(
'Event' => array(
'className' => 'Event',
'foreignKey' => 'project_id'
'dependent' => false,
),
// ... more relationships
);
项目控制器
class ProjectsController extends AppController {
public $paginate = array(
'Project' => array(
'contain' => array(
'Event',
'User'
) // end Project contain
) // end Project pagination
); // end pagination
public function index() {
$this->set('projects', $this->paginate());
}