active record in codeigniter in the case of assignment by value

user3393922

This my model function. obviously it is not best practice to mix sql query and active record together. How should I write the entire function using active record?

public function foo_bar_function($id, $date, $amount) {

       $this->db->where('id', $id);
       $this->db->set('date', $date, TRUE);
       $this->db->set('amount', $amount, FALSE);
       $this->db->update('table_foo');
       $sql = "UPDATE table-foo set diff = total - " . $amount . " WHERE id = '" . $id . "'";
       $this->db->query($sql);
       }
M Khalid Junaid

Using active record you can rewrite your update query as below,Also you can use single update instead of running 2 update queries

public function foo_bar_function($id, $date, $amount) {
    $this->db->set('date', $date);
    $this->db->set('amount', $amount);
    $this->db->set('diff', 'total -'.$amount, FALSE);
    $this->db->where('id', $id);
    $this->db->update('table_foo');
}

See set() method of active record

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

CodeIgniter COUNT with active record?

分類Dev

How to enable backticks in mysql codeigniter active record

分類Dev

Codeigniter active record return single array

分類Dev

Using Active Record to update a `jsonb` value

分類Dev

How to convert RIGHT LEFT functions to codeigniter active record

分類Dev

Display date format as DD MM YYYY in active record Codeigniter

分類Dev

Codeigniter Active Record、複数のwhere条件

分類Dev

Codeigniter Active Recordの問題(ここで、複数の 'AND''OR')

分類Dev

Rails active record relationships

分類Dev

Codeiniter and Active Record

分類Dev

Sorting a Rails active record

分類Dev

Active Record STI 混乱

分類Dev

foreachは私に同じ行を4回与えます(PHP、Active Record、CodeIgniter)

分類Dev

Active Record CodeIgniterを使用してMysqlに配列の配列を保存する

分類Dev

Active Record Validation Rails 4

分類Dev

Undefined method where for Active Record?

分類Dev

Table Join in Rails Active Record

分類Dev

Ruby on Rails Active Record Query

分類Dev

How can I retrieve only the record related to the last value of a date field in this specific case using SQL?

分類Dev

CodeIgniter Active Records where in subquery

分類Dev

display record with specific date in codeigniter

分類Dev

Codeigniter updating database record list

分類Dev

Rails Active Record find every 10th record

分類Dev

Yii2 - WHERE IN with active record

分類Dev

how to store temporary attributes on an Active Record model

分類Dev

Turn an array of Active Record objects into a hash

分類Dev

Active Record Update All JSON Field

分類Dev

Rails Active Record query with multiple optional params

分類Dev

OR operator inside a Where Active Record Query

Related 関連記事

  1. 1

    CodeIgniter COUNT with active record?

  2. 2

    How to enable backticks in mysql codeigniter active record

  3. 3

    Codeigniter active record return single array

  4. 4

    Using Active Record to update a `jsonb` value

  5. 5

    How to convert RIGHT LEFT functions to codeigniter active record

  6. 6

    Display date format as DD MM YYYY in active record Codeigniter

  7. 7

    Codeigniter Active Record、複数のwhere条件

  8. 8

    Codeigniter Active Recordの問題(ここで、複数の 'AND''OR')

  9. 9

    Rails active record relationships

  10. 10

    Codeiniter and Active Record

  11. 11

    Sorting a Rails active record

  12. 12

    Active Record STI 混乱

  13. 13

    foreachは私に同じ行を4回与えます(PHP、Active Record、CodeIgniter)

  14. 14

    Active Record CodeIgniterを使用してMysqlに配列の配列を保存する

  15. 15

    Active Record Validation Rails 4

  16. 16

    Undefined method where for Active Record?

  17. 17

    Table Join in Rails Active Record

  18. 18

    Ruby on Rails Active Record Query

  19. 19

    How can I retrieve only the record related to the last value of a date field in this specific case using SQL?

  20. 20

    CodeIgniter Active Records where in subquery

  21. 21

    display record with specific date in codeigniter

  22. 22

    Codeigniter updating database record list

  23. 23

    Rails Active Record find every 10th record

  24. 24

    Yii2 - WHERE IN with active record

  25. 25

    how to store temporary attributes on an Active Record model

  26. 26

    Turn an array of Active Record objects into a hash

  27. 27

    Active Record Update All JSON Field

  28. 28

    Rails Active Record query with multiple optional params

  29. 29

    OR operator inside a Where Active Record Query

ホットタグ

アーカイブ