【发布时间】:2015-04-01 06:00:35
【问题描述】:
class Store extends AppModel {
var $useTable = 'store';
var $belongsTo = array(
'Employee' => array(
'className' => 'Employee',
'foreignKey' => 'employee_store'
)
);
}
控制器
public function index(){
$options=array(
'joins' =>
array(
array(
'table' => 'Employee',
'alias' => 'Employee',
'foreignKey' => true,
'conditions'=> array('Employee.employee_store = Store.store_name')
)
));
$coupons = $this->Store->find('all', $options);
}
我收到错误 SQLSTATE[42000]:语法错误或访问冲突:1066 Not unique table/alias。
查询如下所示
SELECT `Store`.`id`,
`Store`.`store_name`,
`Store`.`store_address`,
`Store`.`store_phone`,
`Store`.`store_email`,
`Store`.`store_website`,
`Store`.`date_enter`,
`Store`.`store_shortcode`,
`Employee`.`id`,
`Employee`.`employee_name`,
`Employee`.`employee_phone`,
`Employee`.`employee_email`,
`Employee`.`employee_username`,
`Employee`.`employee_password`,
`Employee`.`employee_store`,
`Employee`.`date_enter`
FROM `billing`.`store` AS `Store`
LEFT JOIN `billing`.`employee` AS `Employee` ON (`Store`.`employee_id` = `Employee`.`id`)
JOIN `billing`.`Employee` AS `Employee` ON (`Employee`.`employee_store` = `Store`.`store_name`)
WHERE 1 = 1
【问题讨论】:
-
你使用 $hasMany 而不是 $belongsTo $this->Store->find('all');没有必要加入查询,因为已经有很多人加入您的查询
-
我需要加入 store_name 和 employee_store(store_name=employee_store) 。但当前它的加入 store_name 带有 id (store_name=id)。
标签: cakephp cakephp-2.0 cakephp-1.3 cakephp-2.1 cakephp-2.3