In the controller, where you are displaying the view with the option
to add a profile, do:

1) Count the number of profiles belonging to the user!
$profileCount = $this->User->Profile->find('count', array('conditions'
=> array('Profile.user_id' => $this->Auth->user('id') ) ) );

2) Pass the profile count to the view
$this->set('profileCount', $profileCount);

In the view, do:
1) Display the add option as disabled, if the profile count is 1
2) Display the add option as enabled, if the profile count is 0.

You still have to make sure that a new profile is not added in the add
action, as users can submit your form, disregarding your disabled add
option. So check also for the number of profiles there!

Enjoy,
   John

On May 20, 2:04 pm, Paulos23 <[email protected]> wrote:
> Hi ppl,
> I have 2 tables users and profiles.I have set the associations and
> everything works fine.I use auth.user.id to pass the user_id and i
> have a user with his profile then.Now i want to check if the profile
> exists and to show a message to the user that "Profile already exists"
> when he pushes the add profile button.I am doing this but it always
> tell me that profile exists.
>
> Code:
> function add($id=null){
>                 $this->layout = 'add_profile';
>
>                 if ($this->data['Profile']['name'] != NULL) {
>                         $this->Session->setflash('Profile already exists.');
>
>                 } else {
>                 $user_id = $this->Session->read('Auth.User.id');
>                 if (!empty($this->data['Profile'])) {
>                $this->data['Profile']['class'] = 'User';
>                $this->data['Profile']['user_id'] = $user_id;
>                $this->User->Profile->create();
>                       if ($this->User->Profile->save($this->data)) {
>                                $this->Session->setflash('Profile has
> been saved.');
>                                 $this->redirect('/users');
>                        }
>                        $this->Session->setflash('Profile cannot be
> saved.Please, try again.');
>
>         }
>                 $user = $this->User->read(null, $id);
>                         $this->set(compact('user'));
>          }
>     }
>
> As you see i check if the name field is null and if it is the
> procedure is known.
> Also i want to do something similar for the delete action. e.g if
> there is not a profile to delete just warn the user with a message.
>
> any help would be appreciated!!!
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to [email protected]
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