Answer the question
In order to leave comments, you need to log in
Yii2 / hasOne or what am I doing wrong?
Actually, what's wrong?
Model:
namespace common\models;
use Yii;
class Content extends \yii\db\ActiveRecord
{
...
public function getAuthor() {
return $this->hasOne(User::className(), ['id' => 'author_id']);
}
...
}
namespace backend\controllers;
use Yii;
use common\models\Content;
class ContentController extends Controller
{
...
public function actionIndex()
{
$data = Content::find()->getAuthor()->all();
return $this->render('index', [
'data' => $data,
]);
}
...
}
Calling unknown method: yii\db\ActiveQuery::getAuthor()
Answer the question
In order to leave comments, you need to log in
$data = Content::find()->getAuthor()->all();
You have not selected the correct entry.
If you need an author, then you need this:
$data = Content::find()->one()
->getAuthor()->one();
$data = Content::find()
->with('author')
->all();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question