How do I update 1 field in CakePHP?

Viewed 60535

I want a function to alter one field, "is_featured" to 1(true) of Event model of given ID, to mark an event as "Featured".

class EventsController extends AppController{
function feature($id){}
}
7 Answers

In cakephp 3 to update your data:

  1. Include in your Controller:

    use Cake\ORM\TableRegistry;

  2. In your action:

    $articlesTable = TableRegistry::get('Articles');
    $article = $articlesTable->get(12); // Return article with id 12
    
    $article->title = 'CakePHP is THE best PHP framework!';
    $articlesTable->save($article);
    

Source: http://book.cakephp.org/3.0/en/orm/saving-data.html

Related