如何定义学说中的 nm 关系?

发布于 2024-08-28 16:44:07 字数 3907 浏览 5 评论 0原文

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

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"

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"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

傾城如夢未必闌珊 2024-09-04 16:44:07

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

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 之类的操作。
更多信息请参见 教义文档

For setting-up many-to-many relations, you have to put the relations in the associated tables, not the join table, like so : (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)

Then you will be able to do things such as $article->Tags.
More info in the Doctrine Documentation.

吻泪 2024-09-04 16:44:07

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

It turns out that there was nothing wrong with Doctrine or with my definition of the relation. The cause of the problem was that there was already a class named "Tag" included in the project. After renaming that class, the doctrine-relation worked just fine :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文