Note that there are some explanatory texts on larger screens.

plurals
  1. PODoctrine Relationship on a namespaced table
    text
    copied!<p>I have several doctrine models that have relationships to other models. Let's call those <code>ItemsOne</code>, <code>ItemsTwo</code>, <code>ItemsThree</code>. Each has a relationship defined to the <code>Products</code> doctrine class (products table) and the relationship is defined as:</p> <pre><code>$this-&gt;hasMany( 'Models_Products as Products', array( 'local' =&gt; 'id', 'foreign' =&gt; 'product_id', ) ); </code></pre> <p>Nothing out of the ordinary there.</p> <p>I also have another table (<code>prices</code>) which stores namespaced data. By that I mean that the particular table stores data based on a predefined key (i.e. <code>Vinyl</code>, <code>CD</code>, <code>DVD</code>) so the rows would show something like this:</p> <pre><code>media_namespace entity_id quantity unit_price CD 4 1000 0.99 DVD 4 2500 1.25 CD 7 3750 0.25 Vinyl 15 75 4.25 </code></pre> <p>The <code>entity_id</code> is the id of each of the models accessing this table. So for instance <code>CD/entity_id = 4</code> refers to <code>ItemsOne</code> model, <code>DVD/entity_id = 4</code> refers to the <code>ItemsTwo</code> model, etc.</p> <p>I want to create a relationship - and I don't know if this can be done - whereas it will be namespaced in the model. So in the above snipped for the products relationship I need something like (for the ItemsOne model):</p> <pre><code>$this-&gt;hasMany( 'Models_Prices as Prices', array( 'local' =&gt; 'id', 'foreign' =&gt; 'entity_id', ) ); </code></pre> <p>However the above will return all the prices with the <code>entity_id</code> matching the <code>ItemsOne</code> table which is partly correct. In the above example if <code>entity_id = 4</code>, it will return the <code>CD</code> record but also the <code>DVD</code> one which is not what I need. I want it to be filtering the data based on the <code>media_namespace</code> table. So in short what I need is:</p> <pre><code>prices.media_namespace = 'CD' AND prices.entity_id = itemsone.id </code></pre> <p>Can the above be reflected in a <code>hasMany</code> relationship in Doctrine? I have searched and cannot something that would help.</p> <p>Any pointers are more than appreciated!</p>
 

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