Php 保存多对多关系
我对用条令保存投资组合的标签有点问题。 我有一个投资组合模型:Php 保存多对多关系,php,model,doctrine,many-to-many,Php,Model,Doctrine,Many To Many,我对用条令保存投资组合的标签有点问题。 我有一个投资组合模型: abstract class BasePortfolio extends Doctrine_Record { public function setTableDefinition() { $this->setTableName('portfolio'); $this->hasColumn('id', 'integer', 4, array( 'ty
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需要在其设置方法中表达多对多关系:
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'));
}
}
对我来说,这在一开始似乎有点混乱,但语法是有效的。与将本地id直接关联到外部id的标准一对多或一对一语法不同,条令使用BasePortfolioHasTags类的本地id,通过refClass的设置方法BasePortfolioHasTags中设置的关系直接关联到BaseTags类的本地id
完成此设置后,上面的文档链接将向您展示如何保存数据
我希望这有帮助。就像我说的,我也在试图破译这些东西。我用了一个外国人的名字解决了这个问题在模式中。非常感谢。