Yii2 Basic sql query in view

I’m trying to get the user profile image of the author of the content inside the view is channel I created

but I get this error on my view


Undefined variable: queryAvatar

on my view I did




//Channel View code


use yii\helpers\Html;

use yii\widgets\DetailView;


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

/* @var $model app\models\Channel */


$this->title = $model->Channel_name;

?>

<div class="channel-view">


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

    <div><?php echo $queryAvatar; ?></div> // THE LINE I ADD


    <?php if($model->uid == Yii::$app->user->id):?>

    <p>

        <?= Html::a('Update', ['update', 'id' => $model->Channel_id], ['class' => 'btn btn-primary']) ?>

        <?= Html::a('Delete', ['delete', 'id' => $model->Channel_id], [

            'class' => 'btn btn-danger',

            'data' => [

                'confirm' => 'Are you sure you want to delete this item?',

                'method' => 'post',

            ],

        ]) ?>

    </p>

    <?php endif;?>

</div>



and on my controller I created this function




//function on my controller


 public function actionChannel($id)

        {

            $cchannel = new Channel();

            $queryAvatar = (new \yii\db\Query())


            ->select(['uavatar'])

            ->from('userlogin')

            ->where(['uid' => $cchannel->uid])

            ->All();


            return $this->render('channel', [

                'model' => $this->findModel($id),

                'queryAvatar '=> $queryAvatar ,

            ]);

        }



A white space appended to ‘queryAvatar’. It might be the cause of the issue.

I changed to ‘queryAvatar’ but still getting the error

That’s strange. Are you sure you are calling ‘channel’ action? I guess you might be calling ‘view’ action where ‘queryAvatar’ is not defined.

instead of query-all() try query->scalar()

wild guess you are likely in wrong view file or a in wrong action because I don’t see anything wrong with the code other than that space pointed out by @softark.

@mbi I don’t think that will make a difference, suppose your $query->all() returns null which is assigned to the $queryAvatar it will still be defined as null.

You can see in the controller


 public function actionChannel($id)

        {

            $cchannel = new Channel();

            $queryAvatar = (new \yii\db\Query())


            ->select(['uavatar'])

            ->from('userlogin')

            ->where(['uid' => $cchannel->uid])

            ->All();


            return $this->render('channel', [

                'model' => $this->findModel($id),

                'queryAvatar '=> $queryAvatar ,

            ]);

        }

it’s channel

I found the solution if someone get the same problem, in the controller I did change


$cchannel = new Channel();

to


$cchannel = $this->findModel($id);




public function actionChannel($id)

        {

            $cchannel = $this->findModel($id);

            $queryAvatar = (new \yii\db\Query())


            ->select(['uavatar'])

            ->from('userlogin')

            ->where(['uid' => $cchannel->uid])

            ->All();


            return $this->render('channel', [

                'model' => $this->findModel($id),

                'queryAvatar '=> $queryAvatar ,

            ]);

        }



OMG, were you talking about another different error in the second post?

BTW, I would do it like the following:




public function actionChannel($id)

        {

            $cchannel = $this->findModel($id);

            $queryAvatar = (new \yii\db\Query())


            ->select(['uavatar'])

            ->from('userlogin')

            ->where(['uid' => $cchannel->uid])

            ->All();


            return $this->render('channel', [

                'model' => $cchannel,

                'queryAvatar' => $queryAvatar ,

            ]);

        }



There’s no need to call $this->findModel() twice.

And I guess you could have established a relation between ‘channel’ and ‘userlogin’ to make things much simpler and easier.




$avatar = $channel->userlogin->uavatar;



Hello your code interesting


$avatar = $channel->userlogin->uavatar;

I tried but I get this error

Getting unknown property: app\models\Channel::userlogin

here is my code in controller


public function actionChannel($id)

    {

		 //$cchannel = $this->findModel($id);

		

		$channel = new Channel();

		$queryAvatar = (new \yii\db\Query())

		->select(['uavatar'])

		->from('userlogin')

		->where(['uid' => $channel->uid])

		->All();

		

		$avatar = $channel->userlogin->uavatar;

		

        return $this->render('channel', [

            'model' => $this->findModel($id),

			'avatar'=> $avatar ,

        ]);


    }

and in my view is


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

	<div><?php 

		echo $avatar;

	?>

	</div>

This is just an example to illustrate the relational feature of Active Record. You have to adjust it to your definitions of the relevant models. Or, probably you have to establish a relation between them in the first place.

From this fragment of your code, I thought that your UserLogin model and Channel model can have a "hasOne" relation between them.

Yes I have in my Channel model hasone


public function getU()

    {

        return $this->hasOne(Userlogin::className(), ['uid' => 'uid']);

    }

But if you can more explain to me please I found this useful and more interesting




// get a Channel

$channel = $this->findModel($id);

// then you can easily get Userlogin;

$userlogin = $channel->u;

// echo avatar

echo $userlogin->uavatar;



Or, just simply:




$channel = $this->findModel($id);

echo $channel->u->uavatar;



Please read the following section of the guide for detailed explanation:

Guide > Working with Databases > Active Record > Working with Relational Data

http://www.yiiframework.com/doc-2.0/guide-db-active-record.html#relational-data

I believe you’ll like it. :)

THANK YOU SO MUCH

where can i add this lines


// get a Channel

$channel = $this->findModel($id);

// then you can easily get Userlogin;

$userlogin = $channel->u;

// echo avatar

echo $userlogin->uavatar;

I read the documents but they don’t explain where to put the code if in

model or controller or view so I don’t know how to use documents

they just say the code but not where I have to put it.

Unlike the example of upload file they explain very well , they show the code for model and code for view and code for controller and they said that is in model , that is in view , and that in controller

and thank you again for your time to answer my post you are a big help

There’s nothing special. You just have to echo the related model’s attribute in the view.




/* view */

...

<div class="channel-view">


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

    <div><?php echo $model->u->uavatar; ?></div>

...






/* controller */

 public function actionChannel($id)

        {

            return $this->render('channel', [

                'model' => $this->findModel($id),

            ]);

        }



Yii’s Active Record will take care of the rest.

My GOD seriously you are a genius, you are the best

Thanks, but I’m neither a genius nor the best. :D

I’ve been just a faithful reader of The Definitive Guide to Yii 2.0.

http://www.yiiframework.com/doc-2.0/guide-index.html

Please take your time to read through it at the very least twice. It’s worth doing.