Re: hasOne relationship problem
Looking through cake/libs/model/model.php I don't see anywhere in saveAll where a check is done for a hasone association. (May have missed it). I don't see how it could do this by default in any case. It could recognize the hasone association but then how would it verify uniqueness. It couldn't use the ID since that is unique for every record. And it doesn't know which field to use to check uniqueness. Since username should be unique I would say that you should add a validate for isUnique for the username. This will prevent mulitiple users with the same name and therefore enforce one profile per user. in the User model add this: var $validate = array( 'username' = => array( 'rule' => 'isUnique', 'message' => 'The user name has already been taken', 'required' => true)); On May 9, 3:22 pm, Robin wrote: > Anyone ... pls a little help will be appreciated in the following > part.. > > > , 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? > > Thnx > > On May 5, 5:51 pm, Robin wrote: > > > Thanks Paulos for your advice. I could certainly add some manual > > checking before adding a child record. The question I am having in my > > mind is that should duplicate check be done by thehasOnerelationship > > or is it possible. Am I missing anything here? > > > Robin > > > On May 5, 2:27 am, paulos nikolo wrote: > > > > 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 > > > > > 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
Re: hasOne relationship problem
Anyone ... pls a little help will be appreciated in the following part.. > , 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? Thnx On May 5, 5:51 pm, Robin wrote: > Thanks Paulos for your advice. I could certainly add some manual > checking before adding a child record. The question I am having in my > mind is that should duplicate check be done by thehasOnerelationship > or is it possible. Am I missing anything here? > > Robin > > On May 5, 2:27 am, paulos nikolo wrote: > > > > > 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 > > > > 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+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: hasOne relationship problem
Thanks Paulos for your advice. I could certainly add some manual checking before adding a child record. The question I am having in my mind is that should duplicate check be done by the hasOne relationship or is it possible. Am I missing anything here? Robin On May 5, 2:27 am, paulos nikolo wrote: > 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 > > > > > 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+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
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 > > 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+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---