错误CakePHP中加入表(error in joining table in cakephp)

2019-10-21 18:06发布

调节器

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

模型

类存储扩展AppModel {VAR $ useTable = '存储'; } }

SQL:

SELECT `Store`.`id`,
       `Store`.`store_name`,
       `Store`.`store_address`,
       `Store`.`store_phone`,
       `Store`.`store_email`,
       `Store`.`store_website`,
       `Store`.`date_enter`,
       `Store`.`store_shortcode`
FROM `billing`.`store` AS `Store`
JOIN `billing`.`Employee` AS `Employee` ON (`Employee`.`employee_store` = `Store`.`store_name`)
WHERE 1 = 1

我需要显示员工和商店表列。 (employee_name,employee_mail等从雇员表,STORE_NAME,从存储表store_add)

Answer 1:

$options = array(
            array(
                    'table' => 'Employee',
                    'alias' => 'Employee',
                    'foreignKey' => true,
                    'conditions'=> array('Employee.employee_store = Store.store_name')
                    )    
          );

$coupons = $this->Store->find('all',array(
   "fields"=>array("Employee.*","Store.*"),
   "joins"=>$options
));


Answer 2:

$this->Store->Behaviors->load('Containable');
$options = array(
   'contain' => array(
      'Employee' => array(
          'conditions' => array(
              'Employee.employee_store' => 'Store.store_name'
          )
      )
   )
);
$coupons = $this->Store->find('all', $options);


文章来源: error in joining table in cakephp