By default in yii application, there are several examples to show us how to search in one table (model). But if two models (tables) are related, how can we do it. Here is how can we make a search in two models.
Scenario, I got two model Post and Author . I want to search author side by side with Post title.
model, add the code so yii can know that when search happen, it’ll automatically search author in Author models.
– Add variable
– Add rules to function rule.
array('author.name, author_name', 'safe', 'on'=>'search'),
– In search function, add criteria
$criteria->with=array('author'); $criteria->compare('author.name',$this->author_name, true);
Step 2: In view, with the help of Cactiveform, add the input type search belongs to Post model.
<?php echo $form->textField(<span style="color: #cc0000;">Post:model()</span>,'<span style="color: red;">author_name</span>',array('size'=>60,'maxlength'=>255)); ?>
See, that’s all for the search