【发布时间】:2013-03-20 13:06:49
【问题描述】:
我想在下拉列表中显示我们所有项目负责人的姓名。
项目负责人只是在公司工作的一部分员工。
这是我的桌子:
project_leaders
,----,----------------,
| id | hr_employee_id |
|----|----------------|
| 1 | 18 |
'----'----------------'
projects
,----,------,-------------------,
| id | name | project_leader_id |
|----|------|-------------------|
| 1 | cake | 1 |
'----'------'-------------------'
hr_employees
,----,------,---------,
| id | name | surname |
|----|------|---------|
| 18 | joe | dirt |
'----'------'---------'
我的 ProjectsController 如下所示:
public function add() {
if ($this->request->is('post')) {
$this->Project->create();
if ($this->Project->save($this->request->data)) {
$this->_sendProjectRequestEmail($this->Project->getLastInsertID() );
$this->Session->setFlash(__('The project has been saved'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The project could not be saved. Please, try again.'));
}
}
$this->set('projectLeaders',$this->Project->ProjectLeader->find('list');
}
这仅返回项目负责人的 id,而不是姓名和姓氏。因此,它返回 1 而不是 Joe Dirt。
我尝试过 $this->Project->ProjectLeader->HrEmployee->find('list') 但列出了所有员工。
我也尝试过指定字段,但它返回一个未知字段错误。
我做错了什么?
【问题讨论】:
-
你做错的是使用基于 ActiveRecord 的 ORM 来完成类似的事情。
-
不要使用
getLastInsertID()- 使用$this->ModelName->id。还有,为什么是hr_前缀?
标签: php cakephp cakephp-model cakephp-2.2