art sir
art sir

Reputation: 1

Cakephp validate from another table when adding to a table

I know how to do this in theory but i'm not sure how to execute.

I have two tables, i'm doing wedding registrations

User table [id][user name][limit][attending]

Guest [id][user_id][name]

The limit is how many guests they can have, and the user_id is the foreign key

this is my add function in the View controller,

public function add($id = null) {

        if ($this->request->is('post')) {
            $this->Guest->create();
            if(//sudo code $['User']['limit'] >=  $['Guest']*Count)
            {

                $this->Session->setFlash(__('error you can\'t add anymore'));
                return $this->redirect(array('controller' => 'Users', 'action' => 'view' , $id));

            } else {

                if ($this->Guest->save($this->request->data)) {
                    $this->Session->setFlash(__('Guest added'));
                    return $this->redirect(array('controller' => 'Users', 'action' => 'view' , $id));
                } else {
                    $this->Session->setFlash(__('Invalid name entered'));
                    return $this->redirect(array('controller' => 'Users', 'action' => 'view' , $id));

                }
            }
        }

//or maybe i'm approaching this all wrong i should be doing this in the model?

Upvotes: 0

Views: 596

Answers (2)

Kamil Kosiński
Kamil Kosiński

Reputation: 296

Use validation for this.

Guest belongs to User.

Guest.php

public $validate = array(
    //other fields
    'user_id' => array(
        //other rules for user_id field
        'limit' => array(
            'rule' => 'checkGuestLimit'
            'message' => 'Some error message'
        )
    )
);

public function checkGuestLimit() {
    $userLimit = $this->User->find('first', array(
        'conditions' => array(
            'User.id' => $this->data['Guest']['user_id']
        ),
        'fields' => array(
            'User.limit'
        )
    ));
    $guestCount = $this->find('count', array(
        'conditions' => array(
            'Guest.user_id' => $this->data['Guest']['user_id']
        )
    ));
    if ($guestCount >= $userLimit['User']['limit']) {
        return false;
    }
    return true;
}

Upvotes: 1

Fazal Rasel
Fazal Rasel

Reputation: 4526

public function add($id = null) {

        if ($this->request->is('post')) {

            $user_id = $this->Auth->user('id');
            $guest_count = $this->Guest->find('count', array(
                                                            'conditions' => 
                                                                array('user_id' => )
                                                            )
                                                        );

            $guest_limit = $this->Auth->user('limit');
            if($guest_count >= $guest_limit)
            {

                $this->Session->setFlash(__("error you can't add anymore"));
                return $this->redirect(array('controller' => 'Users', 'action' => 'view' , $id));

            } else {
                $this->Guest->create();
                if ($this->Guest->save($this->request->data)) {
                    $this->Session->setFlash(__('Guest added'));
                    return $this->redirect(array('controller' => 'Users', 'action' => 'view' , $id));
                } else {
                    $this->Session->setFlash(__('Invalid name entered'));
                    return $this->redirect(array('controller' => 'Users', 'action' => 'view' , $id));

                }
            }
        }
}

Upvotes: 0

Related Questions