簡體   English   中英

保存多對多關系

[英]Save a many to many relationship

使用Doctrine保存投資組合的標簽時,我沒有什么問題。 我有投資組合模型:

abstract class BasePortfolio extends Doctrine_Record
{
    public function setTableDefinition()
    {
        $this->setTableName('portfolio');
        $this->hasColumn('id', 'integer', 4, array(
             'type' => 'integer',
             'length' => 4,
             'fixed' => false,
             'unsigned' => true,
             'primary' => true,
             'autoincrement' => true,
             ));
        $this->hasColumn('title_esp', 'string', 250, array(
             'type' => 'string',
             'length' => 250,
             'fixed' => false,
             'unsigned' => false,
             'primary' => false,
             'notnull' => false,
             'autoincrement' => false,
             ));
        $this->hasColumn('date_creation', 'date', null, array(
             'type' => 'date',
             'fixed' => false,
             'unsigned' => false,
             'primary' => false,
             'notnull' => false,
             'autoincrement' => false,
             ));
    }

    public function setUp()
    {
        parent::setUp();
        $this->hasMany('Images', array(
             'local' => 'id',
             'foreign' => 'id_portfolio'));

        $this->hasMany('PortfolioHasTags', array(
             'local' => 'id',
             'foreign' => 'portfolio_id'));
    }
}

類PortfolioHasTags:

abstract class BasePortfolioHasTags extends Doctrine_Record
    {
        public function setTableDefinition()
        {
            $this->setTableName('portfolio_has_tags');
            $this->hasColumn('portfolio_id', 'integer', 4, array(
                 'type' => 'integer',
                 'length' => 4,
                 'fixed' => false,
                 'unsigned' => true,
                 'primary' => true,
                 'autoincrement' => false,
                 ));
            $this->hasColumn('tags_id', 'integer', 4, array(
                 'type' => 'integer',
                 'length' => 4,
                 'fixed' => false,
                 'unsigned' => false,
                 'primary' => true,
                 'autoincrement' => false,
                 ));
        }

        public function setUp()
        {
            parent::setUp();
            $this->hasOne('Portfolio', array(
                 'local' => 'portfolio_id',
                 'foreign' => 'id'));

            $this->hasOne('Tags', array(
                 'local' => 'tags_id',
                 'foreign' => 'id'));
        }
}

和標簽模型

abstract class BaseTags extends Doctrine_Record
{
    public function setTableDefinition()
    {
        $this->setTableName('tags');
        $this->hasColumn('id', 'integer', 4, array(
             'type' => 'integer',
             'length' => 4,
             'fixed' => false,
             'unsigned' => false,
             'primary' => true,
             'autoincrement' => true,
             ));
        $this->hasColumn('title_esp', 'string', 45, array(
             'type' => 'string',
             'length' => 45,
             'fixed' => false,
             'unsigned' => false,
             'primary' => false,
             'notnull' => false,
             'autoincrement' => false,
             ));
    }

    public function setUp()
    {
        parent::setUp();
        $this->hasMany('PortfolioHasTags', array(
             'local' => 'id',
             'foreign' => 'tags_id'));
    }
}

我需要保存一個投資組合的許多標簽:

$portfolio = new Portfolio;
foreach($tags as $id_tag)
{
$portfolio->PortfolioHasTags[]->tags_id = $id_tag;
}

有更好的方法嗎? 使用句柄保存這種關系是丑陋的!

我也在整理多對多學說。 首先,我發現文檔的此頁面非常有幫助: 原則聯接表關聯:多對多

在Doctrine中,您可以通過“關聯類”(在代碼中為“ BasePortfolioHasTags”)創建關系。 關聯的類BasePortfolio和BaseTag需要與其setup()方法中表示的多對多關系:

    abstract class BasePortfolio extends Doctrine_Record
{
     ...

    public function setUp()
    {
        ...

        $this->hasMany('BaseTags', array(
             'local' => 'id',
             'foreign' => 'tags_id',
             'refClass' => 'BasePortfolioHasTags'));
    }
}

abstract class BaseTags extends Doctrine_Record
{
    ...

    public function setUp()
    {
        parent::setUp();
        $this->hasMany('BasePortfolio', array(
             'local' => 'id',
             'foreign' => 'portfolio_id',
             'refClass' => 'BasePortfolioHasTags'));
    }
}

在我看來,起初這似乎有些混亂,但是語法有效。 Doctrine不是使用將本地ID與外部ID直接相關的標准一對多或一對一語法,而是使用BasePortfolio類的本地ID通過關系直接與BaseTags類的本地ID相關在refClass的setUp()方法中設置BasePortfolioHasTags。

一旦設置完成,上面的文檔鏈接將向您展示如何保存數據。

我希望這有幫助。 就像我說的那樣,我也在嘗試解密這些東西。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM