Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I select from a derived table in Propel?
    primarykey
    data
    text
    <p>I'm using v1.3 of the Propel PHP framework for an application, and I can't find a way to select from a derived table using the <code>Criteria</code> object. Part of the SQL I want is:</p> <pre><code>SELECT unioned_table.foo, quux.stuff, baz.more_stuff... FROM quux INNER JOIN (SELECT foo, bar FROM table1 UNION SELECT foo, bar FROM table2 ) AS unioned_table ON quux.field = unioned_table.foo INNER JOIN baz ON baz.blah = unioned_table.bar INNER JOIN ... WHERE conditions... </code></pre> <p>The actual SQL is more complex than this, but that just includes further joins.</p> <p>I've tried to use <code>Criteria::addAlias()</code>, though unfortunately that tries to SQL-escape the table definition. I eventually gave up on trying to write the SQL in that manner, and created a view (called <code>unisoned_table</code> in this example).</p> <p>I next tried to add joins to this table:</p> <pre><code>$c-&gt;addSelectColumn('unioned_table.foo'); $c-&gt;addSelectColumn(QuuxPeer::STUFF); $c-&gt;addSelectColumn(BazPeer::MORE_STUFF); // ... $c-&gt;addJoin(QuuxPeer::FIELD, 'unioned_table.foo', Criteria::INNER_JOIN); $c-&gt;addJoin(BazPeer::BLAH, 'unioned_table.bar', Criteria::INNER_JOIN); // ... $c-&gt;add(QuuxPeer::STUFF, $someval); // ... </code></pre> <p>Sadly this results in the view being joined <em>twice</em> -- once as an inner join with <code>qux</code>, and once as a cross join with <code>baz</code>. Bizarrely, if I remove the <code>baz</code> SELECT column, then the cross join disappears.</p> <p>Does anyone have any suggestions as to how I can do this? I can't use just bare custom SQL, as this <code>Criteria</code> may need to be modified (columns replaced, extra conditions added, etc) or used in a <code>doCount()</code> call.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload