0

when i trying to make join between two tables i get error.

here is my controller

public function actionView($id)
{
    $view = (new \yii\db\Query())
        ->select(['id', 'user_id', 'body', 'title'])
        ->join('INNER JOIN', 'users', 'blog.user_id','users.id')
        ->where(['blog.id' => $id])
        ->one();

    return $this->render('view', compact('view'));
}

here is my view

 <div class="col-lg-4">
            <h2><?= $view['name'] . ' ' . $view['surname'] ?></h2>
            <h3><?= $view['title']; ?></h3>
            <p><?= $view['body']; ?></p>
            <a href="<?php echo yii::$app->homeUrl; ?>" class="btn btn-default">Back </a>
            <?= Html::a('Delete', ['delete', 'id' => $view['id']], ['class' => 'btn btn-danger']) ?>
        </div>
Rasimoghlu
  • 73
  • 1
  • 9

1 Answers1

1

On prima vista, it's obvious that you had forgot the from clause of the QueryBuilder.

Take a look into this:

$view = (new \yii\db\Query())
        ->select(['id', 'user_id', 'body', 'title'])
        ->from('blog') // this is your from clause of the query
        ->join('INNER JOIN', 'users', 'blog.user_id = users.id')
        ->where(['blog.id' => $id])
        ->one();
G.Spirov
  • 198
  • 9
  • Thanks you for answer. i tried your method but i get this error (Argument 2 passed to yii\db\conditions\HashConditionBuilder::build() must be of the type array, string given) – Rasimoghlu Apr 25 '20 at 06:40
  • I have edited my answer, check out the `join` clause also. – G.Spirov Apr 25 '20 at 06:43
  • yeah but now i got this error (Trying to access array offset on value of type bool) – Rasimoghlu Apr 25 '20 at 06:44
  • This is something completely different from defining your query builder. I think what I had described should work properly. – G.Spirov Apr 25 '20 at 06:46
  • what you had described its works very good. but i dont understant why im getting this error? i never get errors like this in Laravel – Rasimoghlu Apr 25 '20 at 06:48
  • `Trying to access array offset on value of type bool)` I think this comes from your view. You should check if specific key exists in your $view data. https://stackoverflow.com/questions/59674903/trying-to-access-array-offset-on-value-of-type-bool-in-php-7-4. – G.Spirov Apr 25 '20 at 06:49