[...]
In **Yii 1.1**, we sometimes got lost trying to search by a HAS_MANY relation. The wiki article [Drills : Search by a HAS_MANY relation in Yii 1.1](http://www.yiiframework.com/wiki/428/drills-search-by-a-has_many-relation-in-yii-1-1/) was an effort to offer some practical techniques of searching by a HAS_MANY relation in Yii 1.1.
Now, we have **Yii 2**. Let's examine what we can do with the new ActiveRecord of Yii 2 when we have to solve the same tasks of searching by a HAS_MANY relation.
## Relation
Two entities are sometimes connected with a relation of **1:N**. Or we may say that 1:N is the only possible relation between 2 entities as long as we are in the RDB world. **1:1** relation is just a particular kind of 1:N where N is always assumed to be 1 at the maximum. And **N:N** relation can be considered as a combination of two 1:N relations (`N:1-1:N`).
Yii supports this 1:N relation in ActiveRecord as **HAS_ONE** and **HAS_MANY** relations. 1:N relation seen from the side of N is HAS_ONE, and from the side of 1 it is HAS_MANY.[...]
Now, let's construct an example of 1:N relation.
## Example of HAS_MANY1:N relation : Author and Post
Think about blog posts and their authors. It's a relation of "An Author has many Posts" and "A Post has an Author" at the same time. This pair of relations is represented with the following code:
```php
<?**An Author has many Posts**
```php
/**
* Author model
* @property integer $id
* @property string $name the author's name
...
*/
class Author extends \yii\db\ActiveRecord
{
...
/**
* @return \yii\db\ActiveQuery
*/[...]
return $this->hasMany(Post::className(), ['author_id' => 'id']);
}
...
```
```php
<?```
**A post has one Author**
```php
/**
* Post model[...]
* @property integer $author_id FK to the author's id
* @property string $title the title of the post
...
*/
class Post extends \yii\db\ActiveRecord
{
...
/**
* @return \yii\db\ActiveQuery
*/[...]
return $this->hasOne(Author::className(), ['id' => 'author_id']);
}
...
```
Note that a relation in Yii 2 is a getter method returning `ActiveQuery`.[...]
We are going to solve the possible use cases of searching with this example.
## Task #1
**Show all posts that has a word in post title1 : Search by Main Attribute, without Relation
**Show all posts with a keyword**
I want to start with a
n easy one basic task. Let's retrieve all the posts that has a certain
keyword in their title
s.
```php
<?php
// get all the posts that has a keyword in their title
$posts = Post::find()[...]
OK, let's move on to the next.
## Task #2
**Show all posts that has a certain word in post title2 : Search by Main Attribute, with Relation
**Show all posts with a keyword, with their authors**
Now we have to retrieve the authors' names, too.
### Lazy Loading
But this is quite easy if you don't care the efficiency. Just adding a line to echo `$post->author->name` would be enough.
```php
<?php
// get all the posts that has a keyword in their title
$posts = Post::find()[...]
But the lazy loading approach has a drawback in this case, because you have to execute one query for retrieving an array of Posts, and every one query per each Post for retrieving its author. It will end up in 1+N queries to be executed.
~~~```sql
SELECT * FROM `post` WHERE `title` LIKE `%searchword%`;
SELECT * FROM `author` WHERE `id`=1;[...]
SELECT * FROM `author` WHERE `id`=7;
...
~~~```
### Eager Loading
The better approach here is the following:
```php
<?php
// get all the posts that has a keyword in their title, with their authors
$posts = Post::find()[...]
Here we are retrieveing Posts and their Authors at the same time by using **yii\db\ActiveQuery::with()**. It enables you to do the job with only 2 queries.
~~~```sql
SELECT * FROM `post` WHERE `title` LIKE `%searchword%`;
SELECT * FROM `author` WHERE `id` IN (1,2,5,7,...);
~~~```
Just after the 1st query
has retrieved the main models, the 2nd one will get all of the related models at once.
In the above, `(1,2,5,7,...)` refers to the `author_id`s in the result set of the 1st query. This is so-called **eager loading** approach.
The eager loading is preferrable in this particular case, because it's more effective. But, you have to note, it is not always so.
## Task
#2-B3 : Search by HAS_ONE Attribute, with Relation
**Show all posts with their authors
thatwho ha
sve a certain word in
the author's name**
This is a variation of the task #2. name**
You have to search by an attribute in the related model, not in the main model.
```php
<?php
// get all the posts that has a keyword in their author's name, with their authors
$posts = Post::find()[...]
The more important thing to note is that the joining tables using `joinWith` will not reduce the number of queries in the eager loading. You might think that just one query would be enough, but Yii will still use 2 queries, one for the main model and the other for the related model.
~~~```sql
SELECT * FROM `post`
LEFT JOIN `author` ON `post`.`author_id` = `author`.`id`
WHERE `author.name` LIKE `%searchword%`;
SELECT * FROM `author` WHERE `id` IN (1,2,5,7,...);
~~~```
**Yii 2 will always use the separated queries for the main model and the related models.**
So, you would not be able to make the code more efficient by modifying `with` to `joinWith` in the answer for the task
#2.
**We join the related table using `joinWith` not because we expect good performance, but because we have to access a column in the related table in order to filter the rows in the main table.**[...]
And `joinWith` will cause ActiveRecord to choose the eager loading approach by default, but you may suppress the loading of the related models by specifying the optional parameter `$eagerLoading` as `false`.
Huh? WhoYou may sa
idy "It's just a **HAS_ONE** relation. Yeah, it's simple. I know."
?
Yes, you are right, definitely. Let's move on to the next task where we will deal with **HAS_MANY**.
## Task
#34 : Search by HAS_MANY Attribute, without Relation
**Show all authors who have a post
that has a certain with a keyword in its title**
Now we will retrieve the authors, not the posts.
```php
<?php
// get all the authors who have a post that has a
certain keyword in its title
$authors = Author::find()
->joinWith('posts', false) // will not get the related models[...]
We join the `posts` relation using `joinWith` because we need `post.title` in the searching, while we set `$eagerLoading` to false because we don't need the posts to be retrieved.
## Task #45 : Search by HAS_MANY Attribute, with Relation
**Show all authors who have a post
that has a certain with a keyword in its title, with all of their posts**
```php
<?php
// get all the authors who have a post that has a
certain keyword in its title, with all of their posts
$authors = Author::find()
->joinWith('posts') // will eagerly load the related models[...]
Well, then, what about the next task? It's a variation of the above.
## Task #4-B6 : Search by HAS_MANY Attribute, with Relevant Relations
**Show all authors who have a post
that has a certain with a keyword in its title, with all of their relevant posts**
In fact, this is a little more confusing than the above.
### Lazy Loading with Customized Relation
One solution is this:
```php
<?php
// get all the authors who have a post that has a
certain keyword in its title
$authors = Author::find()
->joinWith('posts', false) // won't get posts[...]
foreach($authors as $author) {
echo "Author = {$author->name}\n";
// get all the posts that have a certain keyword in its title
$posts = $author->getPosts()->where(['like', 'post.title', $searchword])->all();
foreach($posts as $post) {[...]
We join the `posts` relation using `joinWith` because we need `post.title` in the searching, while we set `$leagerLoading` to false because we don't want to load the posts at that point. And then we perform the lazy loading of the related posts with an on-the-fly condition for each author afterwards.
### Eager Loading with Customized Relation
You could specify an on-the-fly condition to the relation when you eagerly load the related models:
```php
<?php
// get all the authors who have a post that has a
certain keyword in its title
$authors = Author::find()
->joinWith('posts', false) // this is for the main models[...]
Because we are applying the same condition to the relation for searching the main models and that for retrieving related models, we may use a single relation with that condition for both.
```php
<?php
// get all the authors who have a post that has a
certain keyword in its title
$authors = Author::find()
->innerJoinWith([[...]
Let's move on to the next. It's a bit tough, though.
## Task #57 : LIMIT and ORDER
**Show top 5 authors
in the order of the name who has at least one post that has a certainordered by name who has a post with a key word in
postits title, with
his/her allall of their posts**
OK, so we need to add "LIMIT" and "ORDER". Going to try with this one.[...]
### Trial
```php
<?php
$authors = Author::find()[...]
You will notice that there is something strange in the output results. For example, the results may look like the following:
~~~
[- search word =
**foo
]
**
- Author = Andy
- Post = Don't use
**foo
**
- Post = Use yoo for
**foo
**
- Post = Don't use bar
- Post = Use yar for bar
- Author = Ben
- Post =
**foo
** is great
- Post = I love
**foo
**
- Post = I also love bar
- Author = Charlie
- Post = What's
**foo
**?
- Post = What's bar?
[- end
]
~~~
We could get only 3 authors
, instead of 5
that is what we wanted.
Why did it happen? It was because the SQL used to retrieve the main models had been something like the following:
~~~```sql
SELECT * from author
LEFT JOIN post on author.id = post.author_id[...]
ORDER BY author.name
LIMIT 5
~~~```
Notice that `LIMIT 5` is applied to the number of the rows of an virtual table that is the result of joining `author` table with `post` table. The resulted rows may have less unique authors than the limit, because an author may **have many** posts.[...]
In Yii 1.1, we could use **GROUP BY** trick, though it was MySQL specific. It can also be used in Yii 2:
### Solution #1
: GROUP BY
```php
<?php
$authors = Author::find()
->joinWith('posts')[...]
In this way we could remove the redundant rows by grouping the results by author's ID.
### Solution 2 : DISTINCT
You may say "Why don't you use **DISTINCT**? It might be cleaner and more compatible than a dirty trick." Seems fairly reasonable. Let's give it a try:
### Solution #2
```php
<?```php
$authors = Author::find()
->distinct()[...]
We are going to the last task, feeling a bit confused by the unexpected easiness.
## Task #68: LIMIT and ORDER, with Relevant Relations
**Show top 5 authors
in the order ofordered by name who has a
t least one post that has a certain word in post title, with
his/all of the
ir relevant posts**
Now it's a simple task. You just have to apply the same filter both for the searching of the main models and the loading the related models.[...]
### Example of a Solution
```php
<?php
$authors = Author::find()
->distinct()
->joinWith([
'posts' => function($query) use ($searchword) {
$query->where(['like', 'post.title', $searchword]);[...]