bynd
(Byndstd)
December 28, 2017, 7:25am
1
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 ,
]);
}
softark
(Softark)
December 28, 2017, 9:30am
2
A white space appended to ‘queryAvatar’. It might be the cause of the issue.
bynd
(Byndstd)
December 28, 2017, 9:46am
3
I changed to ‘queryAvatar’ but still getting the error
softark
(Softark)
December 28, 2017, 10:02am
4
That’s strange. Are you sure you are calling ‘channel’ action? I guess you might be calling ‘view’ action where ‘queryAvatar’ is not defined.
mbi
(mbi)
December 28, 2017, 11:54am
5
instead of query-all() try query->scalar()
alirz23
(Ali Raza)
December 28, 2017, 2:20pm
6
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.
bynd
(Byndstd)
December 28, 2017, 5:35pm
7
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
bynd
(Byndstd)
December 28, 2017, 5:41pm
8
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 ,
]);
}
softark
(Softark)
December 28, 2017, 11:35pm
9
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;
bynd
(Byndstd)
December 29, 2017, 12:42am
10
softark:
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>
softark
(Softark)
December 29, 2017, 1:04am
11
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.
bynd
(Byndstd)
December 29, 2017, 2:19am
12
softark:
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
softark
(Softark)
December 29, 2017, 2:30am
13
// 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.
bynd
(Byndstd)
December 29, 2017, 2:42am
14
softark:
// 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
softark
(Softark)
December 29, 2017, 6:28am
15
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.
bynd
(Byndstd)
December 29, 2017, 9:02am
16
softark:
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
softark
(Softark)
December 31, 2017, 1:41am
17
Thanks, but I’m neither a genius nor the best.
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.