SQLSTATE[42000]:语法错误或访问冲突:1066 不唯一 table/alias

SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias

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 不唯一 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

首先你必须取消绑定模型,因为你已经在 Store 模型中绑定了 Employee 模型,这就是它与你的模型冲突的原因。

public function index(){ 
  $this->Store->unbindModel(
        array('belongsTo' => array('Employee')), true
    );

$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);
}