0

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` = '[email protected]', `review` = 'test msg', `rating` = '2' WHERE `milgyonu_mber`.`ratings`.`id` = '19'
1
  • could you print the 'save' script, to be able to analyze the 'insert' query? Because if you check your Debug console line 4, there is an update, not insert. Commented Feb 10, 2015 at 7:19

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

5 Comments

@but i dint set id field as primary key
but id need to set some manual value in id field so what should i do?
Why do you? what is your primary key on that table?
there are no primary key in this field but this table is work same like comments
ok thanks now i added another field as primary key and now its working thanks alot :) Voycey

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.