保存多对多的关系

时间:2011-08-05 07:52:15

标签: php model doctrine many-to-many

我在使用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'));
    }
}

Class 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;
}

有更好的方法吗?使用句柄来保存这种关系真是太丑了!

1 个答案:

答案 0 :(得分:0)

我也正在整理Doctrine的多对多关系。首先,我发现文档的这一页非常有用: Doctrine Join Table Associations: Many-to-many

在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使用BasePortfolio类的本地id通过关系直接与BaseTags类的本地id相关联,而不是直接将本地id与外部id相关联的标准一对多或一对一语法。在refClass的setUp()方法中设置,BasePortfolioHasTags。

设置完成后,上面的文档链接将向您展示如何保存数据。

我希望这会有所帮助。就像我说的那样,我也试图破译这些东西。