Yii Framework Forum: Accessing Models - Yii Framework Forum

Jump to content

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

Accessing Models Rate Topic: -----

#1 User is offline   Sler 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 97
  • Joined: 17-July 12

Posted 16 August 2012 - 10:41 AM

Hello,

I have generated the model 'User' using Gii..

Now, hoe can I access it using a method... Then, after accessing it, I would compare a variable(user) to all the usernames in the model, see if it matches. If it does, it will send back its password.

But my main concern for now is, the access or connection to my model user. :)


Chabx
0

#2 User is offline   waterloomatt 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 477
  • Joined: 09-April 10

Posted 16 August 2012 - 11:21 AM

$user = ModelName::model()->findByPk($userId);

$userList = ModelName::model()->findAll();

$userList = ModelName::model()->findByAttributes(array('attributeName' => $attributeValue));

0

#3 User is offline   xtan 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 13
  • Joined: 10-August 11
  • Location:Bulgaria, Sofia

Posted 16 August 2012 - 12:09 PM

To find a user you can try this
$model = User::model();
$user = $model->find(array(
'condition'=>'username = :user',
'params'=>array(':user'=>$user)
));
//And finally retrieve user's password
$password = $user->password;

Hope this helps you.
Never leave your job unfini...
0

#4 User is offline   bennouna 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,114
  • Joined: 05-January 12
  • Location:Morocco

Posted 16 August 2012 - 01:10 PM

Quote

Then, after accessing it, I would compare a variable(user) to all the usernames in the model, see if it matches. If it does, it will send back its password.


That would mean you'd be storing the clear password? I don't believe that is recommended at all.
0

#5 User is offline   alirz23 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 504
  • Joined: 08-August 12
  • Location:Durban, South Africa

Posted 16 August 2012 - 11:22 PM

here is shorter version

$user = User::model()->findByAttribute(array("username"=>$username));
0

#6 User is offline   Sler 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 97
  • Joined: 17-July 12

Posted 17 August 2012 - 02:14 AM

View Postxtan, on 16 August 2012 - 12:09 PM, said:

To find a user you can try this
$model = User::model();
$user = $model->find(array(
'condition'=>'username = :user',
'params'=>array(':user'=>$user)
));
//And finally retrieve user's password
$password = $user->password;

Hope this helps you.


$password = $user->password;
it says that cannot go to a non object
0

#7 User is offline   xtan 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 13
  • Joined: 10-August 11
  • Location:Bulgaria, Sofia

Posted 17 August 2012 - 05:20 AM

View PostSler, on 17 August 2012 - 02:14 AM, said:

$password = $user->password;
it says that cannot go to a non object

Well, probably your model name is not "User" or some of the model variables are not the same, as in my example...
But as @bennouna pointed out, it would be a good idea if you hash your user's passwords before storing them in DB.
Never leave your job unfini...
0

#8 User is offline   Sler 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 97
  • Joined: 17-July 12

Posted 17 August 2012 - 10:04 AM

View Postxtan, on 17 August 2012 - 05:20 AM, said:

Well, probably your model name is not "User" or some of the model variables are not the same, as in my example...
But as @bennouna pointed out, it would be a good idea if you hash your user's passwords before storing them in DB.


For the password thing, i'm only doing this for dummy testings. it would be crop and prices, instead of username and password.. But I'll change it later..

Here's the full error.

*Trying to get property of non-object*

my model name is user

Chabx
0

#9 User is offline   Sler 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 97
  • Joined: 17-July 12

Posted 17 August 2012 - 10:10 AM

if(isset($_GET['keyword']))
14 {
15 $keyword=$_GET['keyword'];
16 $user = ''; //if i don't initialize this, there will be an error stating that $user is undefined.
17 $model = User::model();
18 $user = $model->find(array(
19 'condition'=>$keyword = ':username',
20 'params'=>array(':username'=>$user)
21 ));
22 //And finally retrieve user's password
23 $password = $user->password;

This is my full code...
It means, that I will pass keyword and equate it to :username
0

#10 User is offline   Sler 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 97
  • Joined: 17-July 12

Posted 17 August 2012 - 10:30 AM

View Postxtan, on 16 August 2012 - 12:09 PM, said:

To find a user you can try this
$model = User::model();
$user = $model->find(array(
'condition'=>'username = :user',
'params'=>array(':user'=>$user)
));
//And finally retrieve user's password
$password = $user->password;

Hope this helps you.


$user = $model->find(array(
'condition'=>$keyword = ':username',
'params'=>array(':username'=>$user)
));

$user is undefined sir...
0

#11 User is offline   xtan 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 13
  • Joined: 10-August 11
  • Location:Bulgaria, Sofia

Posted 17 August 2012 - 10:31 AM

if(isset($_GET['keyword']))
  {
  $keyword=$_GET['keyword'];
  $user = ''; //if i don't initialize this, there will be an error stating that $user is undefined.
  $model = User::model();
  $user = $model->find(array(
  'condition'=>'username = :username',
  'params'=>array(':username'=>$keyword)
  ));
  //And finally retrieve user's password
  $password = $user->password;

You have a mistake on line 19
'condition'=>$keyword = ':username',

"condition" is actually the SQL condition. So you just have to type 'username = :username' (which in SQL syntax is something like this : "SELECT * from ... WHERE <<condition>> LIMIT...").
":username" is a parameter that should be passed on to the query. So if you are searching for username = $keyword, params array will look like this
'params'=>array(':username'=>$keyword)

Hope this was helpful.
Never leave your job unfini...
0

#12 User is offline   Sler 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 97
  • Joined: 17-July 12

Posted 17 August 2012 - 10:39 AM

View Postalirz23, on 16 August 2012 - 11:22 PM, said:

here is shorter version

$user = User::model()->findByAttribute(array("username"=>$username));


I still need to compare 'keyword' and username(in model). because it will do another method if it is not found...
0

#13 User is offline   Sler 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 97
  • Joined: 17-July 12

Posted 17 August 2012 - 10:45 AM

View Postxtan, on 17 August 2012 - 10:31 AM, said:

if(isset($_GET['keyword']))
  {
  $keyword=$_GET['keyword'];
  $user = ''; //if i don't initialize this, there will be an error stating that $user is undefined.
  $model = User::model();
  $user = $model->find(array(
  'condition'=>'username = :username',
  'params'=>array(':username'=>$keyword)
  ));
  //And finally retrieve user's password
  $password = $user->password;

You have a mistake on line 19
'condition'=>$keyword = ':username',

"condition" is actually the SQL condition. So you just have to type 'username = :username' (which in SQL syntax is something like this : "SELECT * from ... WHERE <<condition>> LIMIT...").
":username" is a parameter that should be passed on to the query. So if you are searching for username = $keyword, params array will look like this
'params'=>array(':username'=>$keyword)

Hope this was helpful.



This is my new code:
$user = $model->find(array(
'condition'=>$keyword = ':username',
'params'=>array(':username'=>$keyword)
));
//And finally retrieve user's password
$password = $user->password;
echo $password;

but still it won't work.. it still says
Trying to get property of non-object
in $password = $user->password;
0

#14 User is offline   waterloomatt 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 477
  • Joined: 09-April 10

Posted 17 August 2012 - 10:38 PM

What are you trying to do? Retrieve an existing user? Retrieve a logged in user?

If you are just trying to get a user who has a keyword.

$username = 'daffy_duck';
$keyword = 'manager';

$user = User::model()->find('keyword=:keyword && username = :username', array(
    ':username' => $username, 
    ':keyword' => $keyword
));

// SELECT * FROM USER WHERE keyword =  manager && username = daffy_duck;

if ($user)
{
    echo $user->password;
}
else
{
    echo 'Doesn"t exist';
}


FYI
$keyword = 'myHouseIsRed';

$user = $model->find(array(
    'condition'=>$keyword = ':username',
    'params'=>array(':username'=>$keyword)
));

// Says SELECT * FROM USER WHERE 'myHouseIsRed' = 'myHouseIsRed'
// That's why you're accessing a null object.

0

#15 User is offline   Sler 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 97
  • Joined: 17-July 12

Posted 18 August 2012 - 09:44 AM

View Postwaterloomatt, on 17 August 2012 - 10:38 PM, said:

What are you trying to do? Retrieve an existing user? Retrieve a logged in user?

If you are just trying to get a user who has a keyword.

$username = 'daffy_duck';
$keyword = 'manager';

$user = User::model()->find('keyword=:keyword && username = :username', array(
    ':username' => $username, 
    ':keyword' => $keyword
));

// SELECT * FROM USER WHERE keyword =  manager && username = daffy_duck;

if ($user)
{
    echo $user->password;
}
else
{
    echo 'Doesn"t exist';
}


FYI
$keyword = 'myHouseIsRed';

$user = $model->find(array(
    'condition'=>$keyword = ':username',
    'params'=>array(':username'=>$keyword)
));

// Says SELECT * FROM USER WHERE 'myHouseIsRed' = 'myHouseIsRed'
// That's why you're accessing a null object.



I have a $keyword outside the database..
My database has two entities: username and password only.
Then, I have this $keyword, I would like it to compare itself to all the username

THIS SURELY SOLVED MY PROBLEM. This is my code right here.

			$user = User::model()->find('username = :keyword', array(
			':keyword' => $keyword
			));

		// SELECT * FROM USER WHERE keyword =  manager && username = daffy_duck;

			if ($user)
			{
				echo $user->password;
			}
			else
			{
				echo 'Doesn"t exist';
			}

0

#16 User is offline   Sler 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 97
  • Joined: 17-July 12

Posted 18 August 2012 - 09:51 AM

View Postwaterloomatt, on 17 August 2012 - 10:38 PM, said:

What are you trying to do? Retrieve an existing user? Retrieve a logged in user?

If you are just trying to get a user who has a keyword.

$username = 'daffy_duck';
$keyword = 'manager';

$user = User::model()->find('keyword=:keyword && username = :username', array(
    ':username' => $username, 
    ':keyword' => $keyword
));

// SELECT * FROM USER WHERE keyword =  manager && username = daffy_duck;

if ($user)
{
    echo $user->password;
}
else
{
    echo 'Doesn"t exist';
}


FYI
$keyword = 'myHouseIsRed';

$user = $model->find(array(
    'condition'=>$keyword = ':username',
    'params'=>array(':username'=>$keyword)
));

// Says SELECT * FROM USER WHERE 'myHouseIsRed' = 'myHouseIsRed'
// That's why you're accessing a null object.



Thank you so much for making me understand. :)
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