0

I have the following code:

print_r( $this->request->data['User'] ['username'] );
print_r( $this->User->findByRole($this->request->data['User']['username']) );

Now as you can see this is just to test it but when i try to run this i get the username correctly ( the first print_r) but the second print_r returns an empty array.

I know that there is a field called Role in my user database i also know that it is not empty. it should return employee.

So what am i doing wrong?

3
  • You're finding by role, not username - hence findByRole. i.e. You are searching the role column with the username value. You want to return the role field, not findBy it Commented Aug 12, 2013 at 11:06
  • @SmokeyPHP i want to find the field row where the username is equal to $this->request->data['User'] ['username'] Commented Aug 12, 2013 at 11:07
  • @SmokeyPHP No i want to search the User model for the row "Role" where the username is equal to that of my array in this example the username is MyClient Commented Aug 12, 2013 at 11:09

1 Answer 1

1

Use findByUsername and use the second parameter to specify the fields:

$this->User->findByUsername($this->request->data['User']['username'],array("User.role"));

Or the standard method:

$this->User->find("first",array(
    "fields" => array("User.role")
    ,"conditions" => array("User.username" => $this->request->data['User']['username'])
));
Sign up to request clarification or add additional context in comments.

2 Comments

This gives me the following error: Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'User.' in 'field list' SQL Query: SELECT DISTINCT User.``, User.id FROM system_bloglic_com_test.users AS User WHERE User.username = 'MarcEmp' LIMIT 1
@MarcRasmussen Sorry, wrongly formatted the $fields array - see updated answer (I've also added a more verbose version if needed)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.