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
$user = ModelName::model()->findByPk($userId);
$userList = ModelName::model()->findAll();
$userList = ModelName::model()->findByAttributes(array('attributeName' => $attributeValue));
xtan
(Relations Team)
August 16, 2012, 5:09pm
3
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.
abennouna
(Abennouna)
August 16, 2012, 6:10pm
4
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.
alirz23
(Ali Raza)
August 17, 2012, 4:22am
5
here is shorter version
$user = User::model()->findByAttribute(array("username"=>$username));
xtan:
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
xtan
(Relations Team)
August 17, 2012, 10:20am
7
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.
xtan:
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
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
xtan:
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'=>[b]$user[/b])
));
$user is undefined sir…
xtan
(Relations Team)
August 17, 2012, 3:31pm
11
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.
I still need to compare ‘keyword’ and username(in model). because it will do another method if it is not found…
xtan:
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;
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.
waterloomatt:
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';
}
waterloomatt:
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.