Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to define an n-m relation in doctrine?
    primarykey
    data
    text
    <p>If got a table "Article" and a table "Tags". Articles can have multiple tags and tags can hang to multiple articles.</p> <p>The class BaseArticle looks like this:</p> <pre><code>abstract class BaseArticle extends Doctrine_Record { public function setTableDefinition() { $this-&gt;setTableName('article'); $this-&gt;hasColumn('article_id', 'integer', 8, array( 'type' =&gt; 'integer', 'length' =&gt; 8, 'fixed' =&gt; false, 'unsigned' =&gt; false, 'primary' =&gt; true, 'autoincrement' =&gt; true, )); $this-&gt;hasColumn('title', 'string', null, array( 'type' =&gt; 'string', 'fixed' =&gt; false, 'unsigned' =&gt; false, 'primary' =&gt; false, 'notnull' =&gt; false, 'autoincrement' =&gt; false, )); $this-&gt;hasColumn('text', 'string', null, array( 'type' =&gt; 'string', 'fixed' =&gt; false, 'unsigned' =&gt; false, 'primary' =&gt; false, 'notnull' =&gt; false, 'autoincrement' =&gt; false, $this-&gt;hasColumn('url', 'string', 255, array( 'type' =&gt; 'string', 'length' =&gt; 255, 'fixed' =&gt; false, 'unsigned' =&gt; false, 'primary' =&gt; false, 'notnull' =&gt; false, 'autoincrement' =&gt; false, )); } public function setUp() { parent::setUp(); $this-&gt;hasMany('Tag as Tags', array( 'local' =&gt; 'article_id', 'foreign'=&gt;'tag_id', 'refClass'=&gt;'Articletag') ); } </code></pre> <p>}</p> <p>The BaseTag-class like this:</p> <pre><code>abstract class BaseTag extends Doctrine_Record { public function setTableDefinition() { $this-&gt;setTableName('tag'); $this-&gt;hasColumn('tag_id', 'integer', 4, array( 'type' =&gt; 'integer', 'length' =&gt; 4, 'fixed' =&gt; false, 'unsigned' =&gt; false, 'primary' =&gt; true, 'autoincrement' =&gt; true, )); $this-&gt;hasColumn('name', 'string', null, array( 'type' =&gt; 'string', 'fixed' =&gt; false, 'unsigned' =&gt; false, 'primary' =&gt; false, 'notnull' =&gt; false, 'autoincrement' =&gt; false, )); } public function setUp() { parent::setUp(); $this-&gt;hasMany('Article as Articles', array( 'local' =&gt; 'tag_id', 'foreign'=&gt;'article_id', 'refClass'=&gt;'Articletag') ); } </code></pre> <p>}</p> <p>And the relationship class like this:</p> <pre><code> abstract class BaseArticletag extends Doctrine_Record { public function setTableDefinition() { $this-&gt;setTableName('articletag'); $this-&gt;hasColumn('article_id', 'integer', 8, array( 'type' =&gt; 'integer', 'length' =&gt; 8, 'fixed' =&gt; false, 'unsigned' =&gt; false, 'primary' =&gt; true, 'autoincrement' =&gt; false, )); $this-&gt;hasColumn('tag_id', 'integer', 4, array( 'type' =&gt; 'integer', 'length' =&gt; 4, 'fixed' =&gt; false, 'unsigned' =&gt; false, 'primary' =&gt; true, 'autoincrement' =&gt; false, )); } public function setUp() { parent::setUp(); } } </code></pre> <p>When I try to get a property from the article all goes well by using:</p> <pre><code> $article = Doctrine_Query::create()-&gt;from('Article a') -&gt;where('id = ?' , 1) -&gt;fetchOne(); echo $article-&gt;title; //gives me the title </code></pre> <p>But when I try this:</p> <pre><code> foreach($article-&gt;Tags as $tag) { echo($tag-&gt;name) } </code></pre> <p>I get an error:</p> <pre><code>Unknown record property / related component "Tags" on "Article" </code></pre>
    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.
 

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