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,
开发者_如何学C '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!
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.
精彩评论