How to determine the ratio nm in the doctrine?

If the table "Article" and the table "Tags" are received. Articles can have several tags and tags that can hang on several articles.

The BaseArticle class is as follows:

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 is as follows:

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 is as follows:

    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 an article, everything goes well 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 message:

Unknown record property / related component "Tags" on "Article"
+3
source share
2 answers

To set up many-to-many relationships, you must put the relationships in linked tables, not a join table, for example: (simplified)

Article
...
  Relations:
    Tags:
      class: Tag
      local: article_id
      foreign: tag_id
      refClass: ArticleTag

Tag
...
  Relations:
    Articles:
      class: Article
      local: tag_id
      foreign: article_id
      refClass: ArticleTag

ArticleTag:
(no relations)

, $article- > Tags. Doctrine.

+3

, . , "Tag". : -)

0

Source: https://habr.com/ru/post/1740943/


All Articles