简体   繁体   English

在Zend Framework 2中选择左连接变量

[英]Left join with variable in Zend Framework 2 Select

I'm trying to do the following select in Zend Framework 2: 我正在尝试在Zend Framework 2中执行以下选择:

$select->from($this->table);
    $select->join($this->multiLangTable, $this->table . '.id=' .$this->multiLangTable . '.'
            . $this->foreignKey .' AND '. $this->multiLangTable . ".lang =".$locale, $mapping, 'left');

The variable $locale is the language of my application (ca, es, en). 变量$ locale是我的应用程序的语言(ca,es,en)。 The problems is that the app shows and error saying : 问题是应用程序显示和错误说:

Statement could not be executed (42S22 - 1054 - Unknown column 'ca' in 'on clause') 声明无法执行(42S22 - 1054 - 'on子句'中的未知列'ca')

It detects the variable as a column, but it's not a column. 它将变量检测为列,但它不是列。

I have tried the same before but havent really found a good way to do it. 我以前尝试过同样的方法,但还没有真正找到一个好方法。 this is because Zend just qoutes everything in the on statement as identifiers. 这是因为Zend只是将on语句中的所有内容作为标识符。 You can just move the condition to the where statement 您可以将条件移动到where语句

$select->from($this->table);
$select->join($this->multiLangTable, $this->table . '.id=' .$this->multiLangTable . '.'
            . $this->foreignKey, $mapping, \Zend\Db\Sql\Select::JOIN_LEFT);
$select->where(array($this->multiLangTable . ".lang" => $locale));

Or you can use a \\Zend\\Db\\Sql\\Expression in the on statement, but this will remove all the qouting from it. 或者你可以在on语句中使用\\Zend\\Db\\Sql\\Expression ,但这将删除它的所有qouting。

$select->join($this->multiLangTable,new \Zend\Db\Sql\Expression($this->table . '.id=' .$this->multiLangTable . '.'
        . $this->foreignKey .' AND '. $this->multiLangTable . ".lang = ? ", $locale), $mapping, 'left');

I've found a solution that works for me. 我找到了一个适合我的解决方案。

 $select->from($this->table);
 $select->join($this->multiLangTable,new \Zend\Db\Sql\Expression( $this->table . '.id=' .$this->multiLangTable . '.'
            . $this->foreignKey . ' AND '.$this->multiLangTable . '.lang = ?'), $mapping, 'left');
 $statement = $sql->prepareStatementForSqlObject($select);

 $resultsSql = $statement->execute(array($locale));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM