2015-02-06 171 views
1

我有以下SQL SELECT:合併Zend框架2

SELECT c.id, c.code, c.closed, COALESCE(c2.name, c.name) AS name 
FROM `centers` c 
LEFT JOIN `centers_i18n` c2 ON c2.center_id = c.id 
AND c2.lang = 'ES' 

在我的Zend應用Y具有以下選擇:

$select = $sql->select(); 
    $select->from($this->table); 
    $select->join($this->multiLangTable, $this->table . '.id=' .$this->multiLangTable . '.' . $this->foreignKey, $mapping); 
    $select->where($conditions); 

這是一樣的,但我不知道該怎麼從列名稱合併。

回答

2

您必須指定要選擇並添加一個爲表達列

$select->columns(
    array(
    ... 
    'name' => new \Zend\Db\Sql\Expression('COALESCE(c2.name, c.name)') 
    ... 
) 
); 

整個代碼應該是接近這個(我還沒有與Zend工作了一段時間,所以不可能有錯誤這裏):

$select = $sql->select(); 
    $select->columns([ 
    'id' => 'id', 
    'code' => 'code', 
    'closed' => 'closed', 
    'name' => new \Zend\Db\Sql\Expression('COALESCE(c2.name, c.name)') 
    ]); 
    $select->from(['c' => $this->table]); 
    $select->join(['c2' => $this->multiLangTable], 'c.id = c2.' . $this->foreignKey, $mapping); 
    $select->where($conditions);