Robin Rodricks
Robin Rodricks

Reputation: 113956

Modify a row with CakePHP

I'm using CakePHP to show a frontend GUI for a MySQL database table. I've used bake to auto generate the screens and I currently have a fully functioning app with View, Edit and Delete buttons per row. I want to add a button per row, called Accept, which should set IsAccepted = 1 on the SQL row.

I've managed to add an Accept button per row as follows:

 echo $this->Html->link(__('Accept'), array('action' => 'accept', $product['Product']['ID']))

But the code in ProductController.php does not work:

 public function accept($id = null){
     ...
     $this->Product->IsAccepted = 1; // does not work, silently fails
 }

What am I doing wrong? How do I properly edit a row using a per-row button?

Upvotes: 0

Views: 168

Answers (5)

Anubhav
Anubhav

Reputation: 1625

Just run updateAll query from accept function as shown below:

public function accept($id = null){

  if(!empty($id)){
   $this->Product->updateAll(
    array('Product.is_accepted' => 1),
    array('Product.id' => $id)
   );
  }

}

Hope this will help you...

For reference: http://book.cakephp.org/2.0/en/models/saving-your-data.html

Upvotes: 0

Indrajeet Singh
Indrajeet Singh

Reputation: 2989

    **Try this.....**
<?php
public function accept($id = null) {
    $this->autoRender = false; // if action has not view.
    if ($this->Product->exists($id)) {

        $this->Product->id = $id;
        if ($this->Product->save(array('is_accepted' => 1))) {
            $this->Session->setFlash(__('The product has been accepted!'));
            return $this->redirect(array('action' => 'index'));
        }
    }
}
?>

Upvotes: 0

Robin Rodricks
Robin Rodricks

Reputation: 113956

Thanks to cornelb I found the answer! This is the final code I used to modify a row, with a per-row button.

  • Modifies the row when the per-row button is pressed (works just like a POST/AJAX button)
  • A flash message that says "Accepted!" shows if saving succeeds
  • Redirects back to the listing page (appears to never leave the listing)

This is the code that goes in ProductController.php (or whatever controller class you have):

public function accept($id = null) {
    if ($this->Product->exists($id)) {

        // save the row
        // you absolutely need to fill the 'id' slot, even if its not your primary key!!
        // this ensures that the row is EDITED, and not INSERTED!
        if($this->Product->save(array('id' => $id, 'ID' => $id, 'IsApproved' => 1, 'ApprovedDate' => date('Y-m-d H:i:s', time())))){

                // show a "flash" message
                // (not Adobe Flash, just a message that shows on top of the list)
                $this->Session->setFlash(__('The product has been accepted!'));

                // this action does not have a view so no need to render
                $this->autoRender = false;

                // redirect to index view
                return $this->redirect(array('action' => 'index'));
        }
    }
}

Upvotes: 0

Ross
Ross

Reputation: 17967

// assuming cake 2.1+
public function accept($id = null){
    if($this->Product->exists($id)) {
        $this->Product->saveField('is_accepted', 1);
        // success.. 
    }

    // else throw not found exception... 
}

Upvotes: 0

cornelb
cornelb

Reputation: 6066

public function accept($id = null){
     $this->Product->save(array('id' => $id, 'is_accepted' => 1));
}

Upvotes: 1

Related Questions