我想从另一个表中添加一个SQL列到我的Propel查询中,但是我无法用普通的"->withColumn“语句获得这个结果。第二个表不是通过外键与第一个表连接的,但对于第一个表中的每个条目,第二个表中只有一个条目。两个表之间的连接是来自另外两个表的两个ids。
为了更好地理解,这里是我的桌子:
条目:
法沃里特:
专家和特遣队:
我希望条目表中的所有条目都有一个额外的列位置。每个条目都有一个收藏夹。对于SQL,这个查询没有问题:
SELECT *, (SELECT position FROM favorit AS f WHERE e.expert_id = f.expert_id AND e.contingent_id = f.contingent_id) AS position FROM entry AS e
但我不知道如何创建带有Propel的查询。我试过这个:
EntryQuery::create() ->filterByExpertId(1) ->withColumn("select position from favorit AS f where e.expert_id = f.expert_id and e.contingent_id = f.contingent_id", '_favorit_id', 'type: int') ->find();
但是进步给了我一个错误
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'select position from favorit AS f where e.expert_id = f.expert_id and e.continge' at line 1' in D:\Entwicklung\htdocs\zeiterfassung\vendor\propel\propel\src\Propel\Runtime\Adapter\Pdo\PdoStatement.php:57 Stack trace: #0 D:\Entwicklung\htdocs\zeiterfassung\vendor\propel\propel\src\Propel\Runtime\Adapter\Pdo\PdoStatement.php(57): PDOStatement->execute(NULL) #1 D:\Entwicklung\htdocs\zeiterfassung\vendor\propel\propel\src\Propel\Runtime\Connection\StatementWrapper.php(194): Propel\Runtime\Adapter\Pdo\PdoStatement->execute(NULL) #2 D:\Entwicklung\htdocs\zeiterfassung\vendor\propel\propel\src\Propel\Runtime\ActiveQuery\Criteria.php(2633): Propel\Runtime\Connection\StatementWrapper->execute() #3 D:\Entwicklung\htdocs\zeiterfassung\vendor\propel\propel\src\Propel\Runtime\ActiveQuery in D:\Entwicklung\htdocs\zeiterfassung\vendor\propel\propel\src\Propel\Runtime\ActiveQuery\Criteria.php on line 2639
在条目表中存在外键(如contingent_id )或计算列时,从另一个表中添加列没有问题:
EntryQuery::create() ->filterByExpertId(1) ->join('Entry.Contingent') ->withColumn('Contingent.name','_contingentName') ->withColumn("LEFT(TIMEDIFF(TIMEDIFF(timeEnd,timeBegin),pause),5)", '_duration', 'type: time') ->find();
发布于 2015-11-14 23:51:18
我找到了解决问题的办法:
$result = EntryQuery::create() ->filterByExpertId($id) ->join('Entry.Contingent') ->join('Contingent.Favorit') ->where('Favorit.expert_id = ?', $id) ->withColumn('Favorit.position','_position') ->find();
这样我就得到了正确的答案。但我还有一个问题:
如果有一个条目有相应的专家和特遣队,但没有收藏,那么这个条目将与我的实际查询不显示。在这些情况下,我也希望作为额外的条目,但是有一个默认值为null或0(零)的位置。
https://stackoverflow.com/questions/33696304
复制相似问题