我在使用 cakephp 和编写 sql 查询时遇到问题,我已经写出了 sql 代码,但不确定如何在 cake 中对其进行编码。
`users` - id, name, address
`accounts` - id, companyname, abn
`accounts_users` - id, account_id, user_id
`templates` - id, name, description, account_id
`fields` - id, name, description, template_id
- 用户 habtm 帐户
- 账户 habtm 用户
- 帐户有许多模板
- 模板属于帐户
- 模板有很多字段
- 字段属于模板
我试图在字段控制器中获取我的 find 语句,添加 template_id 字段就是这样做
select t.id
from template.t, account_users.au, user.u
where t.account_id=au.account_id AND
au.user_id=users.id;
这是到目前为止我在控制器字段中的代码:
function add() {
$this->Session->setFlash("Please create your required fields.");
$templates = $this->Template->find('list', array(
'fields' => array('id'),
'conditions' => array('id'=> $this->Auth->user('id'))));
$this->set('templates','$templates');
if($this->request->is('post')) {
$this->Field->create();
if ($this->Field->save($this->request->data)) {
if ($this->request->data['submit'] == "type_1") {
$this->Session->setFlash('The field has been saved');
$this->redirect(array(
'controller' => 'fields',
'action' => 'add'));
}
if ($this->request->data['submit'] == "type_2") {
$this->Session->setFlash('The template has been saved');
$this->redirect( array('controller' => 'templates','action' => 'index'));
}
} else {
$this->Session->setFlash('The field could not be saved. Please, try again.');
}
}
}
它当前正在打印的 sql 语句是
SELECT `Template`.`id` FROM `pra`.`templates` AS `Template` WHERE `id` = 14
id
指的是users
表ID
错误是我试图在添加表单中获取 template_id。查找函数没有获取模板 ID,而是获取用户 ID