【问题标题】:error in joining table in cakephp在 cakephp 中加入表格时出错
【发布时间】:2015-02-02 11:45:48
【问题描述】:

控制器

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 和 Store 表列。 (员工表中的employee_name、employee_mail 等,商店表中的Store_name、store_add)

【问题讨论】:

    标签: cakephp cakephp-2.0 cakephp-1.3 cakephp-2.1 cakephp-2.3


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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多