0

In my controller I have

var $uses = array('User','Customer');

then I am using read to call the users

$loggedOutCustomer = $this->User->read(null, $this->Auth->user('id'));

this gives me users and cutsomers that I can use but I want to do a sort on customers name. How can i do that in cake?

1
  • Please tag only the version you are using. And what is the relationship between User and Customer? Commented May 11, 2011 at 17:27

2 Answers 2

2

You can set the default order in your User model.

If Customer belongsTo User, User hasMany Customer.

In your User model, under the hasMany variable, find the Customer record and add Customer.name to the order array key.

Also, please remove the cakephp version tag that you are not using.

You can also add your sorting options when you're using the find() method of your model.

This can also be defined in the pagination array

Another option would be to use the Containable behavior

Sign up to request clarification or add additional context in comments.

3 Comments

Is there a solution if he wants to order by a different key in another place in his code? I.e. there should be a way to pass an order parameter..
@oerd yup, he can define it in the find() call or on the pagination (paginate) array. He can also use the Containable behavior. Which is recommended
I think you should put these comments in the answer :-)
1

Give this a try

$loggedOutCustomer = $this->User->find('first', array('conditions' => array('User.id' => $this->Auth->user('id')), 'order' => 'Customer.name ASC'));

Comments

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.