首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Propel ORM:从另一个没有与直接外键连接的表中添加列

Propel ORM:从另一个没有与直接外键连接的表中添加列
EN

Stack Overflow用户
提问于 2015-11-13 15:34:48
回答 1查看 1.1K关注 0票数 0

我想从另一个表中添加一个SQL列到我的Propel查询中,但是我无法用普通的"->withColumn“语句获得这个结果。第二个表不是通过外键与第一个表连接的,但对于第一个表中的每个条目,第二个表中只有一个条目。两个表之间的连接是来自另外两个表的两个ids。

为了更好地理解,这里是我的桌子:

条目:

  • id
  • 名字
  • expert_id
  • contingent_id

法沃里特:

  • id
  • 位置
  • expert_id
  • contingent_id

专家和特遣队:

  • id
  • ..。

我希望条目表中的所有条目都有一个额外的列位置。每个条目都有一个收藏夹。对于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();

EN

回答 1

Stack Overflow用户

发布于 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(零)的位置。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33696304

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档