【问题标题】:SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/aliasSQLSTATE [42000]:语法错误或访问冲突:1066 不是唯一的表/别名
【发布时间】: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


【解决方案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);
}

【讨论】:

  • 错误:在非对象上调用成员函数 unbindModel()
  • 尝试将true 放在这一行array('belongsTo' => array('Employee')), true 签入更新的答案
  • 同样的错误:在非对象上调用成员函数 unbindModel()。是否需要更改 AppModel 或 Store 中的某些内容...
  • 在店铺型号var $belongsTo = array( 'Employee' => array( 'className' => 'Employee', 'foreignKey' => 'employee_store' ) ); 评论此代码
  • 它只显示商店栏,我需要同时显示商店和员工栏..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-02-06
  • 2018-12-07
  • 2019-03-15
  • 2015-09-12
  • 2015-10-12
  • 2023-04-01
  • 2018-05-31
相关资源
最近更新 更多