L
L
lolka022019-02-13 08:18:34
MySQL
lolka02, 2019-02-13 08:18:34

Why does Yii2 throw an error Primary key of 'common\models\PostCategory' can not be empty when fetching?

I make a linked selection

$query = PostCategory::find()->joinWith('category')
                    ->where([
                        'post_id' => $id,
                        'category_id' => $category_id,
                    ])
                    ->andWhere([
                        'category_id.user_id' => Yii::$app->user->getId()
                    ]);
                $post = $query->one();

throws
an Exception (Invalid Configuration) 'yii\base\InvalidConfigException' with message 'Primary key of 'common\models\PostCategory' can not be empty.'
if you make the output of the query $query->getRawSql() and call it, then everything works
SELECT `post_category`.* FROM `post_category` LEFT JOIN `category` ON `post_category`.`category_id` = `category`.`id` WHERE ((`post_id`='58') AND (`category_id`='4')) AND (`category`.`user_id`=1)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lander, 2019-02-13
@lolka02

I understand that the class PostCategorydoes not override the primaryKey.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question