ggutigod
ggutigod

Reputation: 13

cake php insert query working as update

I have a insert query but it is working like update

controller

public function home() 
{
$this->loadModel("Ratings");
$aaddRatings = $this->Ratings->addRatings($this->data['id'],$this->data['searches'],$this->data['name'],$this->data['email'],$this->data['review'],$this->data['rating']);
$this->set(compact('aaddRatings'));
}

Model

public function addRatings($id,$searches,$name,$email,$review,$rating)
{
$this->create();
$aaddRatings =$this->save(array('id'=>$id,'searches'=>$searches,'name'=>$name,'email' => $email,'review' => $review,'rating' => $rating));
return($aaddRatings);
}

Debug

SELECT COUNT(*) AS `count` FROM `milgyonu_mber`.`ratings` AS `Ratings` WHERE `Ratings`.`id` = '19'      1   1   1
2   SELECT COUNT(*) AS `count` FROM `milgyonu_mber`.`ratings` AS `Ratings` WHERE `Ratings`.`id` = '19'      1   1   1
3   SELECT COUNT(*) AS `count` FROM `milgyonu_mber`.`ratings` AS `Ratings` WHERE `Ratings`.`id` = '19'      1   1   1
4   UPDATE `milgyonu_mber`.`ratings` SET `action` = 'NO', `id` = '19', `searches` = 'Advanced Neurology & Super Speciality Hospital', `name` = 'shyam', `email` = 'test@gmail.com', `review` = 'test msg', `rating` = '2' WHERE `milgyonu_mber`.`ratings`.`id` = '19'

Upvotes: 0

Views: 2116

Answers (1)

the1dv
the1dv

Reputation: 931

That is because you have the id field set, if there is a primary key in the save array CakePHP treats it as an update of that record.

Remove that and it should be fine!

Update::

public function addRatings($id,$searches,$name,$email,$review,$rating) {
    $this->create();
    $aaddRatings =$this->save(array('id'=>$id,'searches'=>$searches,'name'=>$name,'email' => $email,'review' => $review,'rating' => $rating));
    return($aaddRatings);
}

should be:

public function addRatings($id,$searches,$name,$email,$review,$rating) {
    $this->create();
    $aaddRatings =$this->save(array('searches'=>$searches,'name'=>$name,'email' => $email,'review' => $review,'rating' => $rating));
    return($aaddRatings);
}

if you have something else set as the primary key for this model then you probably havent specified it in the model

Upvotes: 4

Related Questions