Friday, May 1, 2009

Re: user,profile association

On Fri, May 1, 2009 at 1:56 PM, Paulos23 <paulitostheone@gmail.com> wrote:
>
> Hi peeps,I have mush trouble in creating a user with a unique
> profile.In particular i have created the relations between the 2
> tables (user,profiles) and then i wanted to put some crud
> functionality in each one.The crud for user is working well,but i am
> facing problems with the profile's crud.First of all,i want to make an
> association from user to profile with the user_id but i cant pass it
> to profile.Moreover,i want to create an addition relation with the
> profile_id to the user so that the user can only make 1 profile.
You need a simple hasOne relation (
http://book.cakephp.org/view/78/Associations-Linking-Models-Together )
You don't need other any additional relation, IMHO...
> To help you understand i have troubles in add,and edit functions of
> the profiles_controller.Can you give me a solution?
the simpler solution is bake the model controller and view.. and then
modify... because there are a lot of error..
> <?php
> class UsersController extends AppController {
>        var $name = 'Users';
>        var $helpers = array('Html', 'Form' );
Html and Form are included by default
>        var $uses = array('User', 'Profile');
if you set a relation you don't need to set $uses
>        function index() {
>        $this->set('users', $this->User->find('all'));
>     }
> function view($id = null){
here you should put a check on $id
>                $this->User->id = $id;
>        $this->set('user', $this->User->read());
>        }
>
>
>        function add() {
>                if (!empty($this->data)) {
>                        if      ($this->User->save($this->data)){
>                                $this->Session->setflash('The User has been saved.');
>                                $this->redirect(array('action' => 'index'));
>                                }
>
>                }
>        }
>
>        function delete($id) {
>                $this->User->del($id);
>                $this->Session->setflash('The User with id: '.$id.' has been
> deleted.');
>                $this->redirect('/users');
>        }
>
>
>        function edit($id = null) {
>                $this->User->id = $id;
>                if (empty($this->data)) {
>                        $this->data = $this->User->read();
>                } else {
>                        if ($this->User->save($this->data)) {
>                        $this->Session->setflash('Your personal details have been
> updated!');
>                        $this->redirect('/users');
>                        }
>                }
>        }
>
> }
> ?>
>
>
> controllers/profiles_controller
>
> <?php
> class ProfilesController extends AppController {
>        var $name = 'Profiles';
>        var $helpers = array('Html', 'Form' );
>        var $uses = array('User', 'Profile');
> function add() {
>                if (!empty($this->data)) {
>                        $user = $this->User->save($this->data);
>                        if(!empty($user)){
>                                $this->data['Profile']['user_id'] = $this->User->id;
>                                $this->User->Profile->save($this->data);
>                                $this->data['User']['profile_id'] = $this->User->Profile->id;
>                                $this->User->save($this->data);
>                                $this->redirect('/users');
>
>                }
>                }
>        }
> function view($id = null){
>        $this->Profile->id = $id;
>        $this->set('profile', $this->Profile->read());
>    }
>
>        function edit($id = null) {
>                $this->Profile->id = $id;
>                if (empty($this->data)) {
>                        $this->data = $this->Profile->read();
>                } else {
>                        if ($this->User->Profile->save($this->data)) {
>                        $this->Session->setflash('Your profile details have been
> updated!');
>                        $this->redirect('/users');
>                        }
>                }
>
>        }
>        function delete($id) {
>                $this->Profile->del($id);
>                $this->Session->setflash('The Profile with id: '.$id.' has been
> deleted.');
>                $this->redirect('/users');
>        }
> }
> ?>
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
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: