Tuesday, April 5, 2016

Re: How can I get the user firstname displayed, instead of the User_id

How many tables do you have?
And if a first name is a property of a user, why would you put it in the profiles table and not in the users?
Which version of Cake are you using?

If I understand you correctly, you are saying you have a user that has a profile and a user has many proposals.
If that is the case, I suggest you design the Db first, then bake the 3 models using the bake console, and then adjust the profile relationship making sure that it is a HasOne relationship as specified in the documentation.

If you are using Cake 3  edit each of the table files and pick an appropriate column as the display:
$this->displayField('username');

This is the field that will be shown in select boxes when using find('list')

In your controller where you use the tables you can contain the other relationships (if they are set up in the table.php) like as follows:
in the Users controller, the Users Table is loaded automatically so you can reference it through $this->Users
The call you will need to make to search for more than 1 user will be the find('all'):
$users = $this->Users->find('all', ['contain'=>['Profiles', 'Proposals']]);
For a single user, call the get method with the id of the user:
$user = $this->Users->get($id, ['contain'=>['Profiles', 'Proposals']]);

Once you have contained the other tables you can reference the fields as follows:
$user->first_name;
$user->profile->id;
$user->proposal->id;

Hope that helps a little...
Regards,
--Kevin

--
Sign up for our Newsletter for updates.
http://cakephp.org/newsletter/signup
 
We will soon be closing this Google Group. But don't worry, we have something better coming. Stay tuned for an updated from the CakePHP Team soon.
 
Like Us on FaceBook https://www.facebook.com/CakePHP
Follow us on Twitter http://twitter.com/CakePHP
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments: