Reputation: 2080
I am new in yii framework. I have three different tables in yii framework.
First Table
First table is language(id, language_name) // id is primary key.
Second Table
Second Table is verse(id,topic_id, verse_text) // id is primary key, topic_id is foreign key.
Third Table
Third table is verse_translations(id, verse_id, language_id, translations_text) // id is primary key, language_id is foreign key references with language table, // verse_id is foreign key references with verse table.
Now My Question is.
I want to get all verse_text with specific topic_id.
So how to fetch all records from model Verse through foreign key i.e. topic_id and these record should be sent to view by data provider function.
At this time i am using.
My Controller Method
public function actionVerset()
{
$topic_id = 1;
$result=Verse::model()->with('topic_verse', $topic_id)->findAll();
$dataProvider=new CArrayDataProvider($result, array(
'id'=>'Verse',
'pagination'=>array(
'pageSize'=>2,
),));
$this->render('myverse',array('dataProvider'=>$dataProvider));
}
but it is not giving my desired result.
My Verse Model Relation Method
public function relations()
{
return array(
'topic' => array(self::BELONGS_TO, 'Topic', 'topic_id'),
'verseTranslations' => array(self::HAS_MANY, 'VerseTranslations', 'verse_id'),
'verseLanguage' => array(self::HAS_MANY, 'VerseTranslations', 'language_id'),
'topic_verse' => array(self::BELONGS_TO, 'Verse', 'topic_id'),
);
}
Any help will be appreciated.
Thanks.
Upvotes: 0
Views: 2789
Reputation: 1058
When you have declared the relation in your model you dont need to fetch the data from db using with simply use
$result=Verse::model()->findAll();
and then when you use this
$result->topic->field_name of topic table
This will give the result of that.
Upvotes: 0
Reputation: 18729
To get all Verse records with a specific topic_id, this should do the trick:
$records = Verse::model()->findAllByAttributes(array('topic_id' => $topic_id));
Upvotes: 2