error in joining table in cakephp

…衆ロ難τιáo~ 提交于 2019-12-11 11:47:31

问题


Controller

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

Model

class Store extends AppModel { var $useTable = 'store'; }

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

I need to display Both Employee and Store table columns. (employee_name,employee_mail etc from employee table, Store_name,store_add from store table )


回答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
));



回答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);


来源:https://stackoverflow.com/questions/28277064/error-in-joining-table-in-cakephp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!