How to define an n-m relation in doctrine?

Posted by murze on Stack Overflow See other posts from Stack Overflow or by murze
Published on 2010-04-13T15:17:11Z Indexed on 2010/04/15 10:13 UTC
Read the original article Hit count: 308

If got a table "Article" and a table "Tags". Articles can have multiple tags and tags can hang to multiple articles.

The class BaseArticle looks like this:

abstract class BaseArticle extends Doctrine_Record {
public function setTableDefinition() {
    $this->setTableName('article');
    $this->hasColumn('article_id', 'integer', 8, array(
            'type' => 'integer',
            'length' => 8,
            'fixed' => false,
            'unsigned' => false,
            'primary' => true,
            'autoincrement' => true,
    ));
    $this->hasColumn('title', 'string', null, array(
            'type' => 'string',
            'fixed' => false,
            'unsigned' => false,
            'primary' => false,
            'notnull' => false,
            'autoincrement' => false,
    ));
    $this->hasColumn('text', 'string', null, array(
            'type' => 'string',
            'fixed' => false,
            'unsigned' => false,
            'primary' => false,
            'notnull' => false,
            'autoincrement' => false,
    $this->hasColumn('url', 'string', 255, array(
            'type' => 'string',
            'length' => 255,
            'fixed' => false,
            'unsigned' => false,
            'primary' => false,
            'notnull' => false,
            'autoincrement' => false,
    ));
}

public function setUp() {
    parent::setUp();
    $this->hasMany('Tag as Tags', array( 'local' => 'article_id',
            'foreign'=>'tag_id',
            'refClass'=>'Articletag')
    );
}

}

The BaseTag-class like this:

abstract class BaseTag extends Doctrine_Record {
public function setTableDefinition() {
    $this->setTableName('tag');
    $this->hasColumn('tag_id', 'integer', 4, array(
            'type' => 'integer',
            'length' => 4,
            'fixed' => false,
            'unsigned' => false,
            'primary' => true,
            'autoincrement' => true,
    ));
    $this->hasColumn('name', 'string', null, array(
            'type' => 'string',
            'fixed' => false,
            'unsigned' => false,
            'primary' => false,
            'notnull' => false,
            'autoincrement' => false,
    ));
}

public function setUp() {
    parent::setUp();
    $this->hasMany('Article as Articles', array( 'local' => 'tag_id',
            'foreign'=>'article_id',
            'refClass'=>'Articletag')
    );
}

}

And the relationship class like this:

    abstract class BaseArticletag extends Doctrine_Record
{
    public function setTableDefinition()
    {
        $this->setTableName('articletag');
        $this->hasColumn('article_id', 'integer', 8, array(
             'type' => 'integer',
             'length' => 8,
             'fixed' => false,
             'unsigned' => false,
             'primary' => true,
             'autoincrement' => false,
             ));
        $this->hasColumn('tag_id', 'integer', 4, array(
             'type' => 'integer',
             'length' => 4,
             'fixed' => false,
             'unsigned' => false,
             'primary' => true,
             'autoincrement' => false,
             ));
    }

    public function setUp()
    {
        parent::setUp();
    }
}

When I try to get a property from the article all goes well by using:

        $article = Doctrine_Query::create()->from('Article a')
                            ->where('id = ?' , 1)
                            ->fetchOne();
        echo $article->title; //gives me the title

But when I try this:

        foreach($article->Tags as $tag) {
          echo($tag->name)
        }

I get an error:

Unknown record property / related component "Tags" on "Article"

© Stack Overflow or respective owner

Related posts about doctrine

Related posts about schema