Hi great coders,

     I am having a problem trying to get my model associations in a
plugin to come through.

    I have a plugin called users and I have a users_controller and a
user_profiles_controller and thier associated models.  Between these
two models, I have them associated as, user has one user profile and
the user profile belongs to a user.

     In my controller, since I want to save the profile information at
the same time as I create the user, I have to declare the $uses
array.  This is fine but, since I am in a plugin, I have to declare
these like so:

public $uses = array('users.User', 'users.UserProfile');  This works
perfectly and I can create a user and a user profile and it saves just
fine.

The problem is when I try to find the user.  In my model association I
have to have:
$belongsTo = 'users.User'; and $hasOne = 'users.UserProfile'; or else
it will say that it cannot find the correct table.

Now, with this set, I get a giant SQL statement with an error:

Query: SELECT `User`.`id`, `User`.`email`, `User`.`password`,
`User`.`group_id`, `User`.`created`, `User`.`updated`,
`User`.`modified`, `users`.`UserProfile`.`id`,
`users`.`UserProfile`.`first_name`, `users`.`UserProfile`.`last_name`,
`users`.`UserProfile`.`address`, `users`.`UserProfile`.`city`,
`users`.`UserProfile`.`state`, `users`.`UserProfile`.`zip`,
`users`.`UserProfile`.`province`, `users`.`UserProfile`.`country`,
`users`.`UserProfile`.`home_telephone`,
`users`.`UserProfile`.`cell_phone`, `users`.`UserProfile`.`fax`,
`users`.`UserProfile`.`user_id` FROM `users` AS `User` LEFT JOIN
`user_profiles` AS `users`.`UserProfile` ON
(`users`.`UserProfile`.user_id = `User`.`id`) WHERE 1 = 1

1064: You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near '.`UserProfile` ON (`users`.`UserProfile`.user_id = `User`.`id`)
WHERE 1 = 1' at line 1

To me, it looks like the error is in the statement where it says
users.UserProfile ON users.UserProfile.user_id

So, I am at a loss, can't find anything here on the group, or the
tickets and IRC isn't very helpful today.

So, here is the code I have, maybe a second set of eyes will see
something I dont.

Remember, this is in a plugin.


USERS_CONTROLLER - /app/plugins/users/controllers/users_controller.php
-----------------------------------
class UsersController extends UsersAppController {
        public $components = array('Auth');
        public $uses = array('users.User', 'users.UserProfile');

        public function beforeFilter() {
                parent::beforeFilter();
                $this->Auth->loginAction = '/login';
                $this->Auth->logoutAction = '/logout';
                $this->Auth->allowedActions = array('index', 'create', 'read',
'update', 'delete');
        }

        public function index() {

        }

        public function create() {
                if(!empty($this->data)) {
                        $this->data['User']['password'] = 
$this->Auth->password($this-
>data['User']['password']);
                        $this->User->create($this->data);
                        $this->User->save($this->data);
                        $user_id = $this->User->id;
                        $this->data['UserProfile']['id'] = $user_id;
                        $this->data['UserProfile']['user_id'] = $user_id;
                        $this->UserProfile->create($this->data);
                        $this->UserProfile->save($this->data);
                }
                $this->set('fields', $this->User->getColumnTypes());
        }

        public function read($id = 1) {
                $this->set('user', $this->User->findById($id));
        }

        public function update($id = 1) {
                $this->set('user', $this->User->findById($id));
        }

        public function delete() {

        }
}

USER_MODEL - /app/plugins/users/models/user.php
-----------------------
class User extends UsersAppModel {
        public $useDbConfig = 'users';
        public $hasOne = 'users.UserProfile';
}

USER_PROFILE_MODEL - /app/plugins/users/models/user_profile.php
--------------------------------------
class UserProfile extends UsersAppModel {
        public $useDbConfig = 'users';
        public $belongsTo = 'users.User';
}

--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to