Создаем модели:
Код: Выделить всё
$user = new User();
$user->posts = [];
$user->post[] = new Post();
$user->post[] = new Post();
Код: Выделить всё
$user = User::model()->findByPk($id);
Код: Выделить всё
foreach ($user->posts as $post) echo $post->name....
Код: Выделить всё
function getPosts()
{
return $this->hasMany(Post::className(), ['user_id' => 'id']);
}
1) Почему нельзя сделать так:
Код: Выделить всё
$user = User::findOne($id);
foreach ($user->posts as $post) echo $post->name;
2) Как сделать тоже, что в самом первом коде, только для 2й версии?