I have searched the group for similar posts on what I am trying to do
without much luck. What is the best practice for implementing the
following.

I have two related tables in a db namely:

CREATE TABLE `users` (
        `id` INT(11) NOT NULL AUTO_INCREMENT,
        `parent_id` INT(11) NOT NULL default '0',
        `Username` VARCHAR(40) NOT NULL default '',
        `Password` VARCHAR(40) NOT NULL default '',
        `Lastlogin` datetime default NULL,
        PRIMARY KEY (`id`),
        UNIQUE KEY `username` (`username`)
) ENGINE=MYISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

CREATE TABLE `contact_infos` (
        `id` INT(11) NOT NULL AUTO_INCREMENT,
        `name_first` VARCHAR(40) NOT NULL default '',
        `name_last` VARCHAR(40) NOT NULL default '',
        `email` VARCHAR(40) NOT NULL default '',
        `phone_main` VARCHAR(12) NOT NULL default '',
        `phone_alt` VARCHAR(12) DEFAULT NULL,
        `fax` VARCHAR(12) DEFAULT NULL,
        `user_id` INT(11) NOT NULL default '0',
        UNIQUE KEY `user_id` (`user_id`),
        PRIMARY KEY (`id`)
) ENGINE=MYISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

So every user that is created also has contact information associated
with it.

Setting up the models in Cake I have.

<?php
class User extends AppModel {

        var $name = 'User';
        var $validate = array(
                'parent_id' => VALID_NOT_EMPTY,
                'Username' => VALID_EMAIL,
                'Password' => VALID_NOT_EMPTY,
        )
        var $belongsTo = array(
                        'Parent' =>
                                array('className' => 'User',
                                                'foreignKey' => 'parent_id',
                                                'conditions' => '',
                                                'fields' => '',
                                                'order' => '',
                                                'counterCache' => ''
                                ),

        );
        var $hasOne = array(
                        'ContactInfo' =>
                                array('className' => 'ContactInfo',
                                                'foreignKey' => 'user_id',
                                                'conditions' => '',
                                                'fields' => '',
                                                'order' => '',
                                                'dependent' => true,
                                                'exclusive' => '',
                                                'finderQuery' => '',
                                                'counterQuery' => ''
                                ),
        );
}
?>

and the contact_info model

<?php
class ContactInfo extends AppModel {
        var $name = 'ContactInfo';
        var $validate = array(
                'name_first' => VALID_NOT_EMPTY,
                'name_last' => VALID_NOT_EMPTY,
                'email' => VALID_NOT_EMPTY,
                'phone_main' => VALID_NOT_EMPTY,
                'user_id' => VALID_NOT_EMPTY,
        );
        var $belongsTo = array(
                        'User' =>
                                array('className' => 'User',
                                                'foreignKey' => 'user_id',
                                                'conditions' => '',
                                                'fields' => '',
                                                'order' => '',
                                                'counterCache' => ''
                                ),
        );
}
?>

Here lies the dilemma.

When adding a user, I want to be able to fill in both user and contact
information and be able to save it.

Method 1.
Do both 'User' and 'ContactInfo' model saves from the User controller
verifying that the user has saved before saving the ContactInfo. This
requires a modified User:Add view that has the Add form information
from the ContactInfo:Add view. [Essentially this takes over the "Add"
functionality of the ContactInfo controller thus making the "User"
controller logically muddy since it both adds a user and a contact in
the same controller.]

Method 2.
This uses the requestAction function and calls the controller method
and then renders it under the current controller (in this case User
controller). The User:Add view still needs to be modified to accept
the returned view from requestAction however, now the logic to render
and save is handled by the appropriate controllers with minimal
modification. [The problem that occurs is now one of validation
because the default redirection path for the ContactInfo controller is
to itself when something on the form is invalid. In order to get past
this, I have to redirect to the User/add controller and let validation
occur there. Again the logic becomes dirty because the ContactInfo
depends on the User controller.]

Perhaps I am looking at this the wrong way, hence the post. Does
anybody have suggestions about the reasons for one method being better
than the other or even an alternative method. I keep running across
things like this and it feels like the code is starting to feel
"hacky".

Thanks in advanced.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to cake-php@googlegroups.com
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to