Find one record from the database

Hello,

My table is called "book" and my columns are "title" and "author".

In my view file I added:




use yii\helpers\Html;

use frontend\models\Book;

use yii\db\ActiveQuery;

use yii\db\Connection;

use yii\db\ActiveRecord;




<?php $model = Book::find(1);

if($model){

	echo $model->title;

	echo $model->author;

}?>



But strangely I cannot retrieve this data from the database, what did I miss please?

I am also not sure if all my "use" statements are correct.

Thank you,

Ben

Should be findOne and not find:

<?php $model = Book::findOne(1); ?>

Thank you Fabrizio,

I have done a few changes but still nothing showing on the page:




<?php

use yii\helpers\Html;

use frontend\models\Book;




/* @var $this yii\web\View */

$this->title = 'About';

$this->params['breadcrumbs'][] = $this->title;

?>

<div class="site-about">

    <h1><?= Html::encode($this->title) ?></h1>


    <p>This is the About page. You may modify the following file to customize its content:</p>


    <p>

<?php $model = Book::findOne(1);

      if($model){

      echo $model->title;

 

}?>

  

        

        

    </p>

    

    

    

    <code><?= __FILE__ ?></code>

</div>



Check model content with:




<?php 

$model = Book::findOne(1);

var_dump($model);



Test it with DEBUG, so if there are other errors you can see them.

Thanks Fabrizio, Will check this debug mod out:-)

Ben