I have a problem with sessions. The user can have a company or a town. For the town, I can do with Auth.Town posted the information about the city (town_id of the table users = id of the table towns). It's the same thing for companies (company_id = id), the problem is that I can not create sessions: Auth.Company, there is a connection problem, as you can see in my array I have an id value that is 3 and name is test, this corresponds to the id of the company 3 table when I should have the information of the id 16. What is it going on? An error in my models?
[Auth] => Array
(
[User] => Array
(
[id] => 96
[email] => [email protected]
[avatar] =>
[firstname] => Brian
[lastname] => Ferui
[created] => 2014-09-22 11:05:55
[modified] => 2014-09-22 11:05:55
[role] => user
[town_id] => 0
[company_id] => 16
)
[Company] => Array
(
[id] => 3
[name] => test
[legal_form] =>
)
)
Model User :
<?php
class User extends AppModel{
public $actsAs = array(
'Upload' => array(
'fields' => array(
'avatar' => 'img/avatars/:id1000/:id'
)
)
);
public $hasOne = array(
'Company' => array(
'className' => 'Company',
'foreignKey' => 'id',
'dependent' => true
),
'Town' => array(
'className' => 'Town',
'foreignKey' => 'id',
'dependent' => true
)
);
public $recursive = -1;
public $order = 'User.id DESC';
public $validate = array(
'email' => array(
'rule' => 'isUnique',
'allowEmpty' => false,
'message' => "Ce adresse e-mail est déja prise ou vide..."
),
'password' => array(
array(
'rule' => 'notEmpty',
'allowEmpty' => false,
'message' => 'Le mot de passe ne peux pas être vide...'
),
array(
'rule' => array('minLength', 4),
'message' => 'Le mot de passe doit avoir au moins 4 caractères...'
)
),
'firstname' => array(
array(
'rule' => 'notEmpty',
'message' => 'Le prénom ne peux pas être vide...'
)
),
'lastname' => array(
array(
'rule' => 'notEmpty',
'message' => 'Le nom ne peux pas être vide...'
)
),
'avatar_file' => array(
array(
'rule' => array('fileExtension', array('jpg','png')),
'message' => 'Extension invalide...'
)
)
);
public function beforeSave($options = array()){
if(!empty($this->data['User']['password'])){
$this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
}
return true;
}
}
Model Company :
<?php
class Company extends AppModel{
public $actsAs = array(
'Upload' => array(
'fields' => array(
'logo' => 'img/logos/:id1000/:id'
)
)
);
public $hasMany = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'company_id',
'dependent' => false
));
public $validate = array(
'name' => array(
array(
'rule' => 'notEmpty',
'message' => 'Le nom de l\'entreprise ne peux pas être vide...'
)
),
'logo_file' => array(
array(
'rule' => array('fileExtension', array('jpg','png')),
'message' => 'Extension invalide...'
)
)
);
}
?>
function login in UsersController
public function login() {
if ($this->request->is('post')) {
if (isset($this->request->data['UserLogin'])) {
$this->request->data['User'] = $this->request->data['UserLogin'];
}
if ($this->Auth->login()) {
if ($this->Auth->user('town_id') > '0') {
$towns = array('name', 'country', 'localisation', 'statut');
foreach($towns as $column) {
$this->Session->write(
'Auth.Town.' . $column,
$this->User->Town->field($column)
);
}
}
if ($this->Auth->user('company_id') > '0') {
$companies = array('name', 'legal_form');
foreach($companies as $column) {
$this->Session->write(
'Auth.Company.' . $column,
$this->User->Company->field($column)
);
}
}
return $this->redirect($this->Auth->redirect());
}else{
$this->Session->setFlash("L'adresse électronique ou votre mot de passe ne correspond pas","notif",array('type'=>'error'));
}
}
}
What's weird is that it works for "Town" but not for "Company." For Company, "Associated save" works in my signup function, so the connection is functional, I can not recovered values for the sessions, why, I do not know, thank you ...