Hello,
I have been trying to understand how Yii deals with MANY_TO_MANY relations in vain.
To cut it short:
I have 2 tables with a many to many relationship that i’ve transformed into 3:
Table article (articleID, x, y ,z, etc) with articleID primary_key + foreign key in table tagConnector. I’ve also introduced a public proprety called $tag;
Table tagConnector (tagConnectorID, tagID, articleID) with tagConnectorID primaryKey.
Table tag (tagID, tag) with tagID foreginKey in table tagConnector.
my relations are as follows:
Class Article:
'tagConnectors' => array(self::HAS_MANY, 'TagConnector', 'articleID' , 'with'=>'tag' , 'together'=>false),
Class TagConnector:
'tag' => array(self::BELONGS_TO, 'Tag', 'tagID'),
'article' => array(self::BELONGS_TO, 'Article', 'articleID'),
Class Tag:
'tagConnectors' => array(self::HAS_MANY, 'TagConnector', 'tagID'),
my search criteria in function search():
Class Article:
$criteria->with=array('tagConnectors'=>array('select'=>'tagConnectors.tagID'));
$criteria->compare('tagConnectors',$this->tag,true);
Class TagConnector:
$criteria->with('tag');
$criteria->compare('tag.tag',$this->tagID,true);
$criteria->compare('article.articleID',$this->articleID,true);
$criteria->compare('tagConnectorID',$this->tagConnectorID,true);
Class Tag:
$criteria->compare('tag',$this->tag,true);
$criteria->compare('tagID',$this->tagID,true);
in my CGridView i managed to fetch the tags from the tag table using:
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'article-grid',
'dataProvider'=>$model->search(),
...
array('name'=>'tagConnectors', 'header'=>'bla bla' ,
'value'=>function ($data) {
$tagIDs="";
$tmp=array();
foreach ($data->tagConnectors as $tag)
$tmp[]=$tag->tag->tag;
return $tagIDs = implode(', ',$tmp);
},
//'filter'=>CHtml::listData(Tag::model()->findAll(), 'tag', 'tag')),
'filter'=>CHtml::textField('tag')),
array(
'class'=>'CButtonColumn',
'template'=>'{view}',
),
),
));
NOW my question is, how can i search tags from the Article Form or CGridView or any other search form.
I need to understand this more than just getting a solution, since i’ve read so many tutorials, wikis and examples and I still can’t understand the logic behind this.
THANK YOU IN ADVANCE FOR YOUR HELP