1
我有我保存組合標籤與教條的小問題。 我的投資組合模型:保存多對多的關係
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;
}
有更好的方法來做到這一點?使用手柄來保存這種關係很醜陋!
我已經通過在模式中放入一個foreignAlias來解決它。 謝謝! – Mauro