I have 2 tables in a database: User and Profile.
In a page i want to see all user with its profile associated by user_id.
The problem is when i update the records cakephp update only the table user and don’t the profile table.. Now i’m very confused but this is my code to update in the UsersController
$this->User->id = $this->request->data['User']['id'];
if (!$this->User->exists()) {
$this->Session->setFlash('Nessun utente trovato con questa corrispondenza');
}
if ($this->User->save($this->request->data)) {
$this->Session->setFlash('Modifica avvenute con successo');
$this->redirect(array('action'=>'index'));
}
else {
debug($this->User->invalidFields());
$this->Session->setFlash('Errore di salvataggio dati, prova di nuovo');
}
$this->redirect(array('action'=>'index'));
and this is my User model:
class User extends AppModel{
public $name = 'User';
public $hasOne = array(
'Profile' => array('className' => 'Profile',
'conditions' => '',
'dependant' => true,
'foreignKey' => 'user_id',
'associatedKey' => 'user_id'
)
);
public $validate = array(
'username' => array(
'non_vuoto' => array(
'rule'=> 'notEmpty',//non è vuoto metodo che eredito da appmodel
'message'=> 'Lo username non può essere vuoto'
),
'stringa_alfanumerica' => array(
'rule'=> 'alphaNumeric',//alpha numerico
'message'=> 'Lo username deve essere alfanumerica'
)
),
'password' => array(
'non_vuoto' => array(
'rule'=> 'notEmpty',//non è vuoto metodo che eredito da appmodel
'message'=> 'La password non può essere vuota'
),
'min_lunghezza' => array(
'rule' => array('minLength',5),
'message' => 'La password deve contenere almeno 5 caratteri'
),
'max_lunghezza' => array(
'rule' => array('maxLength',15),
'message' => 'La password deve contenere al massimo 15 caratteri'
),
'password_uguale' => array(
'rule' => 'matchPasswords',
'message' => 'Le password inserite non coincidono'
)
),
'password_confirm' => array(
'non_vuoto' => array(
'rule'=> 'notEmpty',//non è vuoto metodo che eredito da appmodel
'message'=> 'La password non può essere vuota'
)
),
'email' => array(
'email_non_valida' => array(
'rule' => 'email',
'message' => 'L\'email inserita non è valida'
),
'email_univoca' => array(
'rule' => 'isUnique',
'message' => 'Questa email inserita è già presente nel database'
)
),
'activation_key' => array(
'stringa_alfanumerica' => array(
'rule' => 'alphaNumeric',//alpha numerico
'message'=> 'La chiave di attivazione non è valida'
),
'lunghezza campo' => array(
'rule' => array('between',40,40),
'message'=> 'La chiave di attivazione non è valida'
)
),
'url' => array(
'url' => array(
'rule' => array('url',true), //invalida i protocolli http e https
'allowEmpty' => true, //dico che può essere anche vuoto
'message' => 'L\'url inserito non è valido'
)
),
'description' => array(
'max_lunghezza' => array(
'rule' => array('maxLenght',100),
'allowEmpty' => true,
'message' => 'La descrizione è troppo lunga'
)
)
);
//metodo mio dichiarato
public function matchPasswords($data){
//Il primo dato non è criptato se è nella variabile data se utilizzavo il this lui vede il campo password e quindi prendo il campo che ho diciharato
if ($data['password']==$this->data['User']['password_confirm']){
return true;
}
//mando l'errore se non coincidono gli dico invalidami quel campo
$this->invalidate('password_confirm','Le due password non coincidono');
return false;
}
//metodo automatico non necessario avviene sempre prima del salvataggio in questo caso
public function beforeSave(){
//se trova il campo password cripta
if (isset($this->data['User']['password'])){
$this->data['User']['password']=AuthComponent::password($this->data['User']['password']);
}
return true;
}
}
?>
I can’t update my user’s profile why?
i have try to create the model for the Profile like this but nothing
class Profile extends AppModel{
public $name = 'Profile';
var $belongsTo = array('User' =>
array(
'foreignKey' => 'user_id'
)
);
}
Help me please
In the topmost code block you aren’t making any references to the profile, so it is only right, that you don’t update anything.
At least you have to have something like this:
If this didn’t help, I recommend posting the whole function code like this and clarifying your problem:
Also you might want to reread the blog tutorial. I pointed you right to a good example of a simple edit function.