简体   繁体   English

推进ORM:从另一个未与直接外键连接的表中添加一列

[英]Propel ORM: Add a column from another table which is not connected with a direct foreign key

I want to add a SQL column from another table to my Propel query but I can´t get this with the normal "->withColumn" Statement. 我想将另一个表中的SQL列添加到我的Propel查询中,但是我无法使用常规的“-> withColumn”语句来获得此列。 The second table is not connected with the first by a foreign key, but it has for every entry in the first exactly one entry in the second. 第二个表未通过外键与第一个表连接,但对于第一个表中的每个条目,它具有第二个表中的一个条目。 The connection between the two tables are two ids from two more tables. 两个表之间的连接是另外两个表中的两个id。
For a better understanding, here are my tables: 为了更好地理解,这是我的表格:

entry: 条目:

  • id ID
  • name 名称
  • expert_id expert_id
  • contingent_id contingent_id

favorit: 最喜欢的:

  • id ID
  • position 位置
  • expert_id expert_id
  • contingent_id contingent_id

expert and contingent: 专家和特遣队:

  • id ID
  • ... ...

I want all entries from the entry-table with an additional column position. 我希望条目表中的所有条目都带有额外的列位置。 There exists for every entry exact one favorit. 每个条目都有一个偏爱。 With SQL this query is no problem: 使用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

But I don´t know how to create the query with Propel. 但是我不知道如何用Propel创建查询。 I tried it with this: 我尝试了这个:

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();

But Propel gives me an error 但是Propel给我一个错误

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

I have no problem to add a column from another table when there exists in the entry-table a foreign key to the other table like contingent_id or with calculated columns: 当条目表中存在到另一个表的外键(如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(); EntryQuery::create() ->filterByExpertId(1) ->join('Entry.Contingent') ->withColumn('Contingent.name','_contingentName') ->withColumn("LEFT(TIMEDIFF(TIMEDIFF( timeEnd , timeBegin ), pause ),5)", '_duration', 'type: time') ->find();

I found a solution for my problem: 我找到了解决问题的方法:

$result = EntryQuery::create() ->filterByExpertId($id) ->join('Entry.Contingent') ->join('Contingent.Favorit') ->where('Favorit.expert_id = ?', $id) ->withColumn('Favorit.position','_position') ->find();

With this I got the correct answer. 有了这个,我得到了正确的答案。 But I got one more question: 但是我还有一个问题:

If there exist to an entry a corresponding expert and contingent but no favorit, so this entry will with my actual query not displayed. 如果存在一个条目,则具有相应的专家和条件,但没有收藏夹,因此该条目将不显示我的实际查询。 I want as an extra in these cases this entry too but with a position with some default value like null or 0 (zero). 在这些情况下,我也想额外输入此条目,但其位置具有一些默认值,例如null或0(零)。

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

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