Yii Framework Forum: Yii2 Basic sql query in view - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Yii2 Basic sql query in view

#1 User is offline   Bynd 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 31
  • Joined: 16-December 17

Posted 28 December 2017 - 02:25 AM

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 ,
            ]);
        }

0

#2 User is online   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 3,500
  • Joined: 16-February 11
  • Location:Japan

Posted 28 December 2017 - 04:30 AM

Quote

'queryAvatar '=> $queryAvatar


A white space appended to 'queryAvatar'. It might be the cause of the issue.
1

#3 User is offline   Bynd 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 31
  • Joined: 16-December 17

Posted 28 December 2017 - 04:46 AM

View Postsoftark, on 28 December 2017 - 04:30 AM, said:

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

I changed to 'queryAvatar' but still getting the error
0

#4 User is online   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 3,500
  • Joined: 16-February 11
  • Location:Japan

Posted 28 December 2017 - 05:02 AM

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

#5 User is offline   mbi 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 618
  • Joined: 08-May 09

Posted 28 December 2017 - 06:54 AM

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

#6 User is offline   alrazi 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,533
  • Joined: 08-August 12
  • Location:Durban, South Africa

Posted 28 December 2017 - 09:20 AM

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.
0

#7 User is offline   Bynd 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 31
  • Joined: 16-December 17

Posted 28 December 2017 - 12:35 PM

View Postsoftark, on 28 December 2017 - 05:02 AM, said:

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

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
0

#8 User is offline   Bynd 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 31
  • Joined: 16-December 17

Posted 28 December 2017 - 12:41 PM

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 ,
            ]);
        }

0

#9 User is online   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 3,500
  • Joined: 16-February 11
  • Location:Japan

Posted 28 December 2017 - 06:35 PM

View PostBynd, on 28 December 2017 - 02:25 AM, said:

I get this error on my view
Undefined variable: queryAvatar



View PostBynd, on 28 December 2017 - 04:46 AM, said:

I changed to 'queryAvatar' but still getting the error


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;

1

#10 User is offline   Bynd 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 31
  • Joined: 16-December 17

Posted 28 December 2017 - 07:42 PM

View Postsoftark, on 28 December 2017 - 06:35 PM, said:

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>

0

#11 User is online   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 3,500
  • Joined: 16-February 11
  • Location:Japan

Posted 28 December 2017 - 08:04 PM

Quote

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


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.

Quote

$queryAvatar = (new \yii\db\Query())
                ->select(['uavatar'])
                ->from('userlogin')
                ->where(['uid' => $channel->uid])
                ->All();


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

#12 User is offline   Bynd 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 31
  • Joined: 16-December 17

Posted 28 December 2017 - 09:19 PM

View Postsoftark, on 28 December 2017 - 08:04 PM, said:

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
0

#13 User is online   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 3,500
  • Joined: 16-February 11
  • Location:Japan

Posted 28 December 2017 - 09:30 PM

// 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.yiiframew...relational-data

I believe you'll like it. :)
1

#14 User is offline   Bynd 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 31
  • Joined: 16-December 17

Posted 28 December 2017 - 09:42 PM

View Postsoftark, on 28 December 2017 - 09:30 PM, said:

// 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.yiiframew...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
0

#15 User is online   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 3,500
  • Joined: 16-February 11
  • Location:Japan

Posted 29 December 2017 - 01:28 AM

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.
1

#16 User is offline   Bynd 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 31
  • Joined: 16-December 17

Posted 29 December 2017 - 04:02 AM

View Postsoftark, on 29 December 2017 - 01:28 AM, said:

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
0

#17 User is online   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 3,500
  • Joined: 16-February 11
  • Location:Japan

Posted 30 December 2017 - 08:41 PM

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.yiiframew...uide-index.html
Please take your time to read through it at the very least twice. It's worth doing.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users