代码之家  ›  专栏  ›  技术社区  ›  murze

如何在学说中定义n-m关系?

  •  3
  • murze  · 技术社区  · 14 年前

    基类文章如下所示:

    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"
    
    2 回复  |  直到 14 年前
        1
  •  3
  •   DuoSRX    14 年前

    要设置多对多关系,必须将关系放在关联表中,而不是联接表中,如下所示:(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->标记之类的操作。 更多信息请访问 Doctrine Documentation .

        2
  •  0
  •   murze    14 年前