Incrementing Cakephp database field by a value

后端 未结 4 727
挽巷
挽巷 2020-12-06 06:04

I have this field whose value i have to increment the field value by a specific value. I am using this

$data[\'quantity\']     = \'Order.quantity+1\';


        
相关标签:
4条回答
  • 2020-12-06 06:38

    I used updateAll in my code to increment views in an article. Therefore, every time an article is visited, I call the following function from within my view action in my articles controller:

    function incrementViewCount($id) {
        $this->updateAll(
            array('Article.viewed' => 'Article.viewed+1'),                    
            array('Article.id' => $id)
        );
    }
    

    Then in your controller…

    $this->MyModel->incrementViewCount(123);
    

    Basically similar to the tutorial suggested in the previous answer.

    0 讨论(0)
  • 2020-12-06 06:48

    You can try this code also. Works fine for simple ++/-- operations without the need of additional querying.

    https://gist.github.com/denchev/8209318

    0 讨论(0)
  • 2020-12-06 06:50

    Using saveField:

    <?php
    
    $this->Article->id = $id;
    $this->Article->saveField('viewed', (int)$this->Article->field('viewed') + 1);
    
    ?>
    
    0 讨论(0)
  • 2020-12-06 06:52

    you can use updateAll() for this

    a little googling reveals this pretty quick: http://cakephp.1045679.n5.nabble.com/Auto-Increment-A-Field-td3491697.html

    0 讨论(0)
提交回复
热议问题