Accessing Models

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));



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.

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

here is shorter version

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

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

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

$user = $model->find(array(

		'condition'=>$keyword = ':username',


		'params'=>array(':username'=>[b]$user[/b])


		));

$user is undefined sir…




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…

This is my new code:

$user = $model->find(array(

		'condition'=&gt;&#036;keyword = ':username',


		'params'=&gt;array(':username'=&gt;&#036;keyword)


		));


		//And finally retrieve user's password


		&#036;password = &#036;user-&gt;password;


		echo &#036;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.



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';

			}



Thank you so much for making me understand. :)