SQLSTATE [42000]:语法错误或访问冲突:1066不唯一的表/别名

时间:2022-09-21 09:00:24
class Store extends AppModel {
    var $useTable = 'store';
    var $belongsTo = array(
        'Employee' => array(
            'className' => 'Employee',
            'foreignKey' => 'employee_store'
             )
    ); 
    }

Controller

调节器

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

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

我收到错误SQLSTATE [42000]:语法错误或访问冲突:1066不唯一的表/别名。

Query is displaying like below

查询显示如下

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

1 个解决方案

#1


1  

First you have to unbindModel because you are already bind Employee model in Store model that's why it conflict with your Model.

首先,您必须unbindModel,因为您已经在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);
}

#1


1  

First you have to unbindModel because you are already bind Employee model in Store model that's why it conflict with your Model.

首先,您必须unbindModel,因为您已经在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);
}