保存多对多关系

发布于 11-27 19:50 字数 3921 浏览 1 评论 0原文

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

有更好的方法吗?用把柄来挽救这段感情,真是太丑了!

I have i little problem with save Tags of an Portfolio with Doctrine.
I have Portfolio Model:

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

and Tags Model

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

i need to save to many tags of one portfolio:

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

There is a better way to do this? It's ugly to use the handle to save this relationship!

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

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

发布评论

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

评论(1

牛↙奶布丁2024-12-04 19:50:19

我也在整理 Doctrine 的多对多关系。首先,我发现文档的这一页非常有帮助:
Doctrine 连接表关联:多对多

在 Doctrine 中,您通过“关联类”(代码中的“BasePortfolioHasTags”)创建关系。关联的类 BasePortfolio 和 BaseTags 需要在其 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。

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

我希望这有帮助。就像我说的,我也在尝试破译这些东西。

I am also in the middle of sorting out Doctrine many-to-many relationships. First, I found this page of the documentation very helpful:
Doctrine Join Table Associations: Many-to-many

In Doctrine you create the relationship via an "Association Class", which is "BasePortfolioHasTags" in your code. The associated classes, BasePortfolio and BaseTags need to the many-to-many relationship expressed in their setup() methods:

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

To me, this seemed a bit confusing at first, but the syntax works. Rather than the standard one-to-many or one-to-one syntax of directly relating a local id to a foreign id, Doctrine uses the local id of the BasePortfolio class to relate directly to the local id of the BaseTags class via the relationships set in the setUp() method of the refClass, BasePortfolioHasTags.

Once you get this set up, the documentation link above will show you how to save the data.

I hope this helps. Like I said, I am also trying to decipher this stuff.

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