3

如果有一个表格“文章”和一个表格“标签”。文章可以有多个标签,标签可以挂在多篇文章上。

BaseArticle 类如下所示:

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')
    );
}

}

BaseTag 类是这样的:

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')
    );
}

}

和这样的关系类:

    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();
    }
}

当我尝试从文章中获取属性时,使用以下方法一切顺利:

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

但是当我尝试这个时:

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

我收到一个错误:

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

2 回答 2

3

要设置多对多关系,您必须将关系放在关联表中,而不是连接表中,如下所示:(简化)

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 之类的事情了。教义文档中的更多信息。

于 2010-04-13T15:37:21.653 回答
0

事实证明,Doctrine 或我对关系的定义没有任何问题。问题的原因是项目中已经包含了一个名为“Tag”的类。重命名该类后,教义关系工作得很好:-)

于 2010-04-27T13:04:12.347 回答