Bonjour,
J'ai 2 table liées "recharges" et "montant_recharges". Recharges.montant_recharge_id identifie la laison entre les 2 tables.
Lorsque je supprime un element de montant_recharge même si celui ci est utilisé dans la Table recharge cake php me supprime la fois le montant dans la table montnant_recharges mais egalement l'ensemble des entrées dans "recharges" qui contienent le montant_recharge supprimé ce qui n'est pas normal.

Par contre si je fais la suppression depuis phpmyadmin j'ai bien une erreur de violation de contrainte.

Comment résoudre ce problème
Table "Recharges"

CREATE TABLE recharges (
id INT(11) NOT NULL AUTO_INCREMENT,
name VARCHAR(45) NULL DEFAULT NULL,
created DATETIME NULL DEFAULT NULL,
modified DATETIME NULL DEFAULT NULL,
type\_recharge\_id INT(11) NOT NULL,
date\_achat DATE NULL DEFAULT NULL,
montant\_recharge\_id INT(11) NOT NULL,
sn VARCHAR(45) NULL DEFAULT NULL,
date\_expiration DATE NULL DEFAULT NULL,
carte\_sim TEXT NULL,
validite\_recharge\_id INT(11) NOT NULL,
date\_consommation DATE NULL DEFAULT NULL,
acheteur\_id INT(11) NULL DEFAULT NULL,
visible TINYINT(4) NOT NULL DEFAULT '0',
conso\_suj\_id INT(10) NULL DEFAULT NULL,
creator\_id INT(10) NULL DEFAULT NULL,
last\_user\_id INT(10) NULL DEFAULT NULL,
PRIMARY KEY (id),
INDEX fk\_recharges\_type\_recharges1 (type\_recharge\_id),
INDEX fk\_recharges\_montant\_recharges1 (montant\_recharge\_id),
INDEX fk\_recharges\_validite\_recharges1 (validite\_recharge\_id),
INDEX fk\_acheteur\_recharges (acheteur\_id),
CONSTRAINT fk\_acheteur\_recharges FOREIGN KEY (acheteur\_id) REFERENCES whos (id),
CONSTRAINT fk\_montant\_recharges FOREIGN KEY (montant\_recharge\_id) REFERENCES montant\_recharges (id) ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT fk\_type\_recharges\_recharges FOREIGN KEY (type\_recharge\_id) REFERENCES type\_recharges (id),
CONSTRAINT fk\_validite\_recharge\_recharges FOREIGN KEY (validite\_recharge\_id) REFERENCES validite\_recharges (id)
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB
AUTO_INCREMENT=50;

"Montant_recharges" Table

CREATE TABLE montant\_recharges (
id INT(11) NOT NULL AUTO_INCREMENT,
name FLOAT NULL DEFAULT NULL,
created DATE NULL DEFAULT NULL,
modified DATE NULL DEFAULT NULL,
creator\_id INT(11) NULL DEFAULT NULL,
last\_user\_id INT(11) NULL DEFAULT NULL,
PRIMARY KEY (id)
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB
AUTO_INCREMENT=16;

rechargeModel
public $belongsTo = array(
'MontantRecharge' => array(
'className' => 'MontantRecharge',
'foreignkey' => 'montant_recharge_id',

'conditions' => '',
'fields' => '',
'order' =>''
));

montant_recharges Model

public $hasMany = array(

'Recharge' => array(
'className' => 'Recharge',
'foreignKey' => 'montant_recharge_id',
'conditions' => '',
'order' => '',

'limit' => '',
'dependent'=> true
)
);

MontantRechargesController
function admin_delete( $id ){
if($this->RequestHandler->isAjax()){
$this->layout = null;
if($this->{$this->modelClass}->delete( $id )){
$this->Session->setFlash( __('MontantRecharge_Delete_OK'),'notif',array('type' => 'success'));
$this->Result'status'] = 1;
}else{
$this->Session->setFlash( __('MontantRecharge_Delete_KO'),'notif',array('type' => 'error'));
$this->Result'status'] = 0;
}
echo json_encode($this->Result);
exit();

}else{
if($this->{$this->modelClass}->delete( $id )){
$this->Session->setFlash( __('MontantRecharge_Delete_OK'),'notif',array('type' => 'success'));
}else{
$this->Session->setFlash( __('MontantRecharge_Delete_KO'),'notif',array('type' => 'error'));
}
$this->redirect( $this->referer() );
}
}

When I delete a montant_recharge even if it is use in the recharge table the all row in recharge is deleted.

What is expected is that i can't delete a montant_recharge if it is used in another table.

When i do the query directly in mysql it work
If i do delete from montant_recharge where id=xx
i can't the message

Error

SQL Error (1451): Cannot delete or update a parent row: a foreign key constraint fails (fournitures.recharges, CONSTRAINT fk\_montant\_recharges FOREIGN KEY (montant\_recharge\_id) REFERENCES montant\_recharges (id) ON DELETE NO ACTION ON UPDATE NO ACTION)

OK

1 réponse


cosinusweb@gmail.com
Auteur
Réponse acceptée

Bon j'ai trouvé sur un autre Forum, pour info cela vient de la clé 'dependent'=>true qu'il faut laisser à false
"dependent: When dependent is set to true, recursive model deletion is possible. In this example, Comment records will be deleted when their associated User record has been deleted."