2011-08-05 2 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')); 
    } 
} 

내가 한 포트폴리오의 여러 태그에 저장해야

이 작업을 수행하는 더 좋은 방법이 있습니까? 이 관계를 저장하기 위해 손잡이를 사용하는 것은 추한 것입니다!

답변

0

또한 Doctrine 다 대다 관계를 분류하는 중입니다. 먼저 문서의이 페이지가 매우 도움이된다는 것을 알았습니다. Doctrine Join Table Associations: Many-to-many

Doctrine에서 코드에 "AssociationPort"인 관계를 만들면 코드에 "BasePortfolioHasTags"가 생깁니다. 이것은 처음에는 혼란 조금 있지만, 구문 작품 듯 나에게

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은 다 대다 관계에 필요() 메소드 자신의 설정에서 표현했다. Doctrine은 로컬 ID를 외부 ID에 직접 연결하는 표준 일대 다 또는 일대일 구문 대신, BasePortfolio 클래스의 로컬 ID를 사용하여 관계를 통해 BaseTags 클래스의 로컬 ID와 직접 연관시킵니다 refClass, BasePortfolioHasTags의 setUp() 메소드에서 설정하십시오.

위의 문서 링크는 데이터를 저장하는 방법을 보여줍니다.

이 정보가 도움이되기를 바랍니다. 내가 말했듯이, 나는 또한이 물건들을 해독하려하고있다.

+0

나는 foreignAlias를 스키마에 넣음으로써 그것을 해결했다. 감사합니다. – Mauro

관련 문제