Question

OKay so i have two tables Employee and User

my employee model looks like this:

    class Employee extends  AppModel{
    public $name = 'Employee';
    public $primaryKey = "employee_id";
    public $actsAs = array('Containable');

    public $belongsTo = array(
        'User' => array(
            'className' => 'User',
            'dependent' => false,
            'foreignKey' => 'user_id'
        )
    );

}

And my user model looks like this:

    App::uses('AuthComponent', 'Controller/Component');
class User extends AppModel {

// ...

    public function beforeSave($options = array()) {
        if (isset($this->data[$this->alias]['password'])) {
            $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
        }
        return true;
    }


    public $validate = array(
        'username' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'A username is required'
            )
        ),
        'password' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'A password is required'
            )
        ),
        'role' => array(
            'valid' => array(
                'rule' => array('inList', array('employee', 'client')),
                'message' => 'Please enter a valid role',
                'allowEmpty' => false
            )
        )
    );
}

In my employee controller i have an action that allows employees to add other employees the action looks like this:

    public function add() {
    if ($this->request->is('post')) {
        $this->Employee->User->create();
        if ($this->Employee->User->save($this->request->data)) {
            $this->Session->setFlash(__('The user has been saved'));
            $this->redirect(array('action' => 'index'));

        } else {
            $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
        }
    }
}

My employee table looks like this

employee_id user_id

Now whenever i add a user the user is correctly added in my user table and a row is also added in my employee table however there are two mistakes in the employee table:

  1. The employee_id is an auto increment this does not happen and it seems it keeps overriting 1. (so that every user i try to create is employee_id = 1)

  2. the user_id is always 0 however in the user table the user_id is for example 21.

Can anyone tell me why this is happening and how i can fix it?

update

My add action in my employee controller now looks like this:

   public function add() {
    if ($this->request->is('post')) {
        $this->Employee->User->create();
        if ($this->Employee->User->saveAll($this->request->data)) {
            $this->Session->setFlash(__('The user has been saved'));
            $this->redirect(array('action' => 'index'));

        } else {
            $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
        }
    }
}

ive added a hasMany to my user model:

public $hasMany = array(
    'Employee' =>array(
        'className' => 'Employee',
        'dependent' => true,
        'foreignKey' => 'user_id'
    )
);

Still no change

Was it helpful?

Solution

A few issues...

1) Your primary key for employees table should be id, not employee_id. Primary keys are always named id, according to cakephp conventions - http://book.cakephp.org/2.0/en/getting-started/cakephp-conventions.html

2) Just as you've got a belongsTo in your Employee model, you should also add a hasOne relationship to your user model - see http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#hasone

3) In order to save a record, along with it's related data, the method you want is saveAll - check out the documentation here: http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-saveall-array-data-null-array-options-array

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top