CakePHP 模型无法获取 table 关联
CakePHP model fails to grab table association
我正在与 CakePHP 合作开发一个 Web 应用程序,该应用程序具有几个 table 以及它们之间的一些关系。在这种情况下,我有一系列会议引用日历年、部门和相关学校,但是当数据从控制器返回到视图时,模型无法获取相关的 table 信息。
员工与他们所属的部门和学校之间已经存在 $belongsTo
关系,当日历索引返回日历数组时,我也设法抓住了相关的会议,但理想情况下我想成为能够使用日历、部门和学校的名称而不是存储在会议中的 id 字段列出所有会议 table.
这是我的 tables:
dbo.meetings
id (int) *primary key*
calendar_id (int)
department_id (int)
school_id (int)
created (datetime2(7))
modified (datetime2(7))
dbo.calendar
id (int) *primary key*
name (nvarchar(50))
startdate (datetime2(7))
enddate (datetime2(7))
created (datetime2(7))
modified (datetime2(7))
dbo.schools
id (int) *primary key*
name (nvarchar(255))
(other fields)
dbo.departments
id (int) *primary key*
name (nvarchar(255))
(other fields)
这是会议的控制器:
<?php
App::uses('AppController','Controller');
class MeetingsController extends AppController {
public $helpers = array('Form');
public function beforeFilter() {
parent::beforeFilter();
}
public function index() {
$this->set('meetings', $this->Meeting->find('all'));
}
}
?>
这是会议的模型:
<?php
App::uses('AppModel','Model');
class Meeting extends AppModel {
public $primaryKey = 'id';
public $recursive = -1;
public $belongsTo = array(
'Calendar' => array(
'className' => 'Calendar',
'foreignKey' => 'calendar_id'
),
'School' => array(
'className' => 'School',
'foreignKey' => 'school_id'
),
'Department' => array(
'className' => 'Department',
'foreignKey' => 'department_id'
)
);
}
?>
目前 /View/Meetings
中的 index.ctp
文件仅包含 <?php echo var_dump($meetings); ?>
,它正在打印数组,直到我可以使关联正常工作,然后我将根据需要重新设置它的样式。
数组如下所示:
array(1) {
[0]=>
array(1) {
["Meeting"]=>
array(6) {
["id"] => string(1) "1"
["calendar_id"] => string(1) "1"
["department_id"] => string(2) "33"
["school_id"] => string(1) "1"
["created"] => NULL
["modified"] => NULL
}
}
}
出于某种原因,它不会获取它应该获取的日历、部门和学校详细信息,而我希望它获取。有人可以帮忙吗?
编辑:学校模型
<?php
App::uses('AppModel','Model');
class School extends AppModel {
public $validate = array(
'name' => array(
'required' => array(
'rule' => 'notBlank',
'message' => 'A school name is required.'
)
)
);
}
?>
确保您正在为您的模型启用 Containable behaviour 使用 public $actsAs = ['Containable'];
:-
App::uses('AppModel','Model');
class Meeting extends AppModel {
public $recursive = -1;
public $actsAs = array('Containable');
public $belongsTo = array(
'Calendar',
'School',
'Department'
);
}
要将 Containable 应用于所有模型,请在 AppModel 中设置它,以便每个模型都继承该行为。此外,只要您坚持 CakePHP 命名约定,就不需要指定关联的 className
和 foreignKey
。
现在 $this->Meeting->find('all')
在您的 MeetingsController
中应该 return 所有相关数据以及会议。如果您只希望它 return 一些关联数据,您可以将 contain
选项传递给 find
。例如:-
$this->Meeting->find(
'all',
array(
'contain' => array(
'School',
'Department'
)
)
);
我正在与 CakePHP 合作开发一个 Web 应用程序,该应用程序具有几个 table 以及它们之间的一些关系。在这种情况下,我有一系列会议引用日历年、部门和相关学校,但是当数据从控制器返回到视图时,模型无法获取相关的 table 信息。
员工与他们所属的部门和学校之间已经存在 $belongsTo
关系,当日历索引返回日历数组时,我也设法抓住了相关的会议,但理想情况下我想成为能够使用日历、部门和学校的名称而不是存储在会议中的 id 字段列出所有会议 table.
这是我的 tables:
dbo.meetings
id (int) *primary key*
calendar_id (int)
department_id (int)
school_id (int)
created (datetime2(7))
modified (datetime2(7))
dbo.calendar
id (int) *primary key*
name (nvarchar(50))
startdate (datetime2(7))
enddate (datetime2(7))
created (datetime2(7))
modified (datetime2(7))
dbo.schools
id (int) *primary key*
name (nvarchar(255))
(other fields)
dbo.departments
id (int) *primary key*
name (nvarchar(255))
(other fields)
这是会议的控制器:
<?php
App::uses('AppController','Controller');
class MeetingsController extends AppController {
public $helpers = array('Form');
public function beforeFilter() {
parent::beforeFilter();
}
public function index() {
$this->set('meetings', $this->Meeting->find('all'));
}
}
?>
这是会议的模型:
<?php
App::uses('AppModel','Model');
class Meeting extends AppModel {
public $primaryKey = 'id';
public $recursive = -1;
public $belongsTo = array(
'Calendar' => array(
'className' => 'Calendar',
'foreignKey' => 'calendar_id'
),
'School' => array(
'className' => 'School',
'foreignKey' => 'school_id'
),
'Department' => array(
'className' => 'Department',
'foreignKey' => 'department_id'
)
);
}
?>
目前 /View/Meetings
中的 index.ctp
文件仅包含 <?php echo var_dump($meetings); ?>
,它正在打印数组,直到我可以使关联正常工作,然后我将根据需要重新设置它的样式。
数组如下所示:
array(1) {
[0]=>
array(1) {
["Meeting"]=>
array(6) {
["id"] => string(1) "1"
["calendar_id"] => string(1) "1"
["department_id"] => string(2) "33"
["school_id"] => string(1) "1"
["created"] => NULL
["modified"] => NULL
}
}
}
出于某种原因,它不会获取它应该获取的日历、部门和学校详细信息,而我希望它获取。有人可以帮忙吗?
编辑:学校模型
<?php
App::uses('AppModel','Model');
class School extends AppModel {
public $validate = array(
'name' => array(
'required' => array(
'rule' => 'notBlank',
'message' => 'A school name is required.'
)
)
);
}
?>
确保您正在为您的模型启用 Containable behaviour 使用 public $actsAs = ['Containable'];
:-
App::uses('AppModel','Model');
class Meeting extends AppModel {
public $recursive = -1;
public $actsAs = array('Containable');
public $belongsTo = array(
'Calendar',
'School',
'Department'
);
}
要将 Containable 应用于所有模型,请在 AppModel 中设置它,以便每个模型都继承该行为。此外,只要您坚持 CakePHP 命名约定,就不需要指定关联的 className
和 foreignKey
。
现在 $this->Meeting->find('all')
在您的 MeetingsController
中应该 return 所有相关数据以及会议。如果您只希望它 return 一些关联数据,您可以将 contain
选项传递给 find
。例如:-
$this->Meeting->find(
'all',
array(
'contain' => array(
'School',
'Department'
)
)
);