You simply have to make sure you have the relationships set up
properly so that Cake will find the related data. Typically you would
merely need to reference the related User for a Task inside your
display loop like:
$user = $task['User'];
And then if your relationship is a single user, you simply do:
echo $user['username'];
On the other hand if it's a hasMany or HABTM relationship, you will
need to display a drop-down, which Cake does relatively automagically
for you. Cake tries to use a field called 'name' to display in a drop
down, and failing that will reuse the id field for a related table.
To fix this you set the $displayField value in your model, Cake is
smart enough to use that field when it builds lists, so add the
following to your User model:
var $displayField = 'username';
On Dec 20, 9:33 am, vikas <[email protected]> wrote:
> Hello friends..
>
> once again need help..
>
> I have a users table and projects table in my database.
>
> which has a field given below.
>
> Table: users
>
> `id` int(11) NOT NULL auto_increment,
> `username` varchar(100) NOT NULL,
> `password` varchar(100) NOT NULL,
> `email` varchar(255) default NULL,
> `user_role_id` int(11) NOT NULL,
> `first_name` varchar(255) default NULL,
> `last_name` varchar(255) default NULL,
> `created` datetime NOT NULL,
> `modified` datetime NOT NULL,
> `status` tinyint(1) default NULL,
> PRIMARY KEY (`id`)
>
> Table: tasks
>
> `id` int(11) NOT NULL auto_increment,
> `project_id` int(11) default NULL,
> `task_type_id` int(11) default NULL,
> `task_status_id` int(11) default NULL,
> `user_id` int(11) default NULL,
> `name` varchar(255) default NULL,
> `details` text,
> `status` tinyint(4) NOT NULL,
> `created` datetime NOT NULL,
> `modified` datetime NOT NULL,
>
> now in my view/index of tasks, instead of user_id I want to display
> username.
>
> How it is possible.?
>
> My index() coding for tasks_controller.php is:
>
> function index() {
> $this->set('tasks',$this->Task->find('all'));
>
> }
>
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---