Monday, May 4, 2009

Re: hasOne relationship problem

Hi Robin.I am not an expert coz i am trying to build a similar app of yours so i ll tell you my opinion.As i saw your tables format i think you should set depentent=true in user model and the exslusive field in profile model true.The 1st one will delete the associated profile if the user has been deleted and the 2nd (i think) is what u search for.Alternatively , you can set the add profile link to invisible when a user has already a profile so that the rest CRUD functions can be shown.

2009/5/4 Robin <robin27th@gmail.com>

Hello, i'm new both to php and cake, so please excuse me if this is
another obvious noob question.

I was trying to understand how associations work by building up the
example in the manual section of cakephp.org, i was able to build it
up using bake with no much effort.

The thing is, i have a users table and a profiles table, they look
like this:

CREATE TABLE `profiles` (
 `id` int(11) NOT NULL auto_increment,
 `name` varchar(100) collate utf8_unicode_ci NOT NULL,
 `header_color` varchar(100) collate utf8_unicode_ci NOT NULL,
 `user_id` int(11) NOT NULL,
 PRIMARY KEY  (`id`),
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

CREATE TABLE `users` (
 `id` int(11) NOT NULL auto_increment,
 `first_name` varchar(100) collate utf8_unicode_ci NOT NULL,
 `last_name` varchar(100) collate utf8_unicode_ci NOT NULL,
 `username` varchar(100) collate utf8_unicode_ci NOT NULL,
 `password` varchar(100) collate utf8_unicode_ci default NULL,
 PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

using bake i was able to create the models and to define the following
associations

User:
       var $hasOne= array('Profile' =>array('className' => 'Profile',
                                               'foreignKey' =>
'user_id',
                                               'conditions' => '',
                                               'fields' => '',
                                               'order' => '',
                                               'dependent' => ''),);
Profile:
       var $belongsTo = array('User' =>array('className' => 'User',
                                               'foreignKey' =>
'user_id',
                                               'conditions' => '',
                                               'fields' => '',
                                               'order' => '',
                                               'counterCache' =>
''),);

Which seems good to me, but testing the application i was able to
create a second profile for one user.
I was guessing if thehasOneassociation should check that and return
an error, or I am missing something?
On the other hand, i builded the tables based on my own understanding
of the example, maybe they are wrong, and so the whole example.

I was thinking of adding an UNIQUE clause on the user_id so forcing
only one profile per user but doing that causes the application to
show an ugly error database message, which is correct and obviously i
can take care of it, but not seems to follow the elegant and simple
rules of the rest of the application.

Thanks in advance

Robin




--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

No comments: