2011-08-05 15 views
1

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: 私がポートフォリオモデル持っ

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

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

私は1つのポートフォリオの多くのタグに保存する必要があります

これを行うより良い方法がありますか?この関係を保存するにはハンドルを使用するのは醜いです!

答えて

0

私はまた、Doctrine多対多リレーションシップのソート中です。まず、このページのドキュメントが非常に役に立ちました。 Doctrine Join Table Associations: Many-to-many

Doctrineでは、コード内に「BasePortfolioHasTags」という「Association Class」で関係を作成します。私には

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

、これは、最初は少し混乱に見えたが、構文の作品:関連クラスは、BasePortfolioとBaseTagsは、多対多の関係は、彼らのセットアップ()メソッドで表現する必要があります。ローカルIDを外部IDに直接関連付ける標準の1対多または1対1の構文ではなく、DoctrineはBasePortfolioクラスのローカルIDを使用して、関係を介してBaseTagsクラスのローカルIDに直接関連付けるrefClass、BasePortfolioHasTagsのsetUp()メソッドで設定します。

この設定が完了すると、上記のドキュメントリンクがデータの保存方法を示します。

こちらがお役に立てば幸いです。私が言ったように、私はまた、このものを解読しようとしています。

+0

私はスキーマにforeignAliasを置くことで解決しました。 ありがとうございました! – Mauro

関連する問題