1

i dont know how to solve this problem

here is my controller

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

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

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>
2
  • Have you checked what $view is in the controller? Commented Apr 25, 2020 at 7:32
  • now i did it but i get nothing. i think i have to check my query again Commented Apr 25, 2020 at 7:38

1 Answer 1

2

Use checks if the specific key exists in your $view data.

<div class="col-lg-4">
    <h2><?= ($view['name'] ?? '') . ' ' . ($view['surname'] ?? '') ?></h2>
    <h3><?= $view['title'] ?? ''; ?></h3>
    <p><?= $view['body'] ?? ''; ?></p>

    <a href="<?= Yii::$app->homeUrl; ?>" class="btn btn-default">Back</a>

    <?php
        if ($view) {
            echo Html::a('Delete', ['delete', 'id' => $view['id']], ['class' => 'btn btn-danger'])
        } 
    ?>
</div>

EDIT:

You can basically check if you fetch $view data in your controller also and respond to user if criteria conditions did not match any record from the database:

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

    if (!empty($view)) {
        return $this->render('view', compact('view','id'));
    }

    throw new \yii\web\NotFoundHttpException('The requested blog does not exist.');
}
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for answer.your method solved my problem.now i have to check my controller becouse i fetch zero data from DB
Yes, according to my answer in your view file, if you do not fetch $view data by criteria in your controller, it will output blank page details with Back button only. I would suggest you also to check in controller, something like this: if (empty($view)) { // 404 Not Found Exception } else { // render your view with data }
public function actionView($id) { $view = (new \yii\db\Query()) ->select(['blog.id', 'user_id', 'body', 'title']) ->from('blog') ->join('INNER JOIN', 'users', 'blog.user_id = users.id') ->where(['blog.user_id' => $id]) ->one(); if (empty($view)) { echo 'no data'; }else { return $this->render('view', compact('view','id')); } return $this->render('view', compact('view','id')); }
You can use exceptions rather than echo the response.
Thanks you very much. my all problems is solved. <3. i always worked in Laravel. but now im working for another company and here have only one project with yii2. i started learning this framework 5 days ago.when i start work with Laravel i forgot core php )
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.