简体   繁体   English

如何在codeigniter中更新列设置为NULL的表

[英]How to update table with column set NULL in codeigniter

i have a problem with update column with set data to NULL, i have a table user with each one column is id_parent column, user can delete parent and can add parent, so if user have a parent , id_parent will set with user's parent id , but user can delete parent's data, so if user delete parent's data, id_parent column, will set to NULL.我在将数据设置为 NULL 时更新列有问题,我有一个表user ,每一列都是id_parent列,用户可以删除父级并可以添加父级,因此如果userparent ,则id_parent将设置为用户的parent id ,但是用户可以删除父的数据,所以如果user删除父的数据, id_parent列将设置为NULL。 so how to set data to null in database not " " but NULL .那么如何在数据库中将数据设置为 null 而不是" "而是NULL here's my user table.这是我的user表。

user

id_user | name | address | id_parent

使用它来设置空列。它会起作用,不要忘记在活动记录的第三个参数 SET 上添加 false

$this->db->set('id_parent', 'NULL', false);

you can try this code in 'model', hope it'll work:您可以在“模型”中尝试此代码,希望它会起作用:

public function update_std_marks($id_user) {

    $this->db->set('id_parent', null);
    $this->db->where('id_user', $id_user);
    $this->db->update('user');
}

I was curious so I wanted to see how CodeIgniter's Active Record would handle different variations of syntax when working with a null value.我很好奇,所以我想看看 CodeIgniter 的 Active Record 在处理空值时如何处理不同的语法变体。 I'll map out several test cases for where() and set() method calls.我将为where()set()方法调用绘制几个测试用例。

Battery of where() tests: where()测试电池:

  1.  ->where('col', null) // `col` IS NULL
  2.  ->where('col', null, false) // col IS NULL
  3.  ->where('col', 'NULL') // `col` = 'NULL'
  4.  ->where('col', 'NULL', false) // col = NULL
  5.  ->where('col IS NULL') // `col` IS NULL
  6.  ->where('col IS NULL', null) // `col` IS NULL
  7.  ->where('col IS NULL', null, false) // col IS NULL
  8.  ->where('col', 'IS NULL') // `col` 'IS NULL'
  9.  ->where('col', 'IS NULL', false) // col = IS NULL

Properly quoted and formed: #1, #5, #6 ( recommended )正确引用和格式:#1、#5、#6(推荐
Logically formed, but not quoted: #2, #7 ( not recommended )逻辑形成,但未引用:#2、#7(不推荐
Inappropriate rendered syntax: #3, #4, #8, #9 ( do not use )不适当的渲染语法:#3、#4、#8、#9(不要使用


Battery of set() tests: set()测试的电池:

  1.  ->set('col', null) // `col` = NULL
  2.  ->set('col', null, false) // col =
  3.  ->set('col', 'NULL') // `col` = 'NULL'
  4.  ->set('col', 'NULL', false) // col = NULL
  5.  ->set('col IS NULL') // `col IS` `NULL` = ''
  6.  ->set('col IS NULL', null) // `col IS` `NULL` = NULL
  7.  ->set('col IS NULL', null, false) // col IS NULL =
  8.  ->set('col', 'IS NULL') // `col` = 'IS NULL'
  9.  ->set('col', 'IS NULL', false) // col = IS NULL

Properly quoted and formed: #1 ( recommended )正确引用和格式:#1(推荐
Logically formed, but not quoted: #4 ( not recommended )逻辑形成,但未引用:#4(不推荐
Inappropriate rendered syntax: #2, #3, #5, #6, #7, #8, #9 ( do not use )不恰当的渲染语法:#2、#3、#5、#6、#7、#8、#9(不要使用

Your user table must have the attribute set to is_null in the id_parent field.您的用户表的id_parent字段中的属性必须设置为 is_null。 Otherwise you can't.否则你不能。

The query should be something like查询应该是这样的

Update table user set id_parent = null where id_user = X

You can set that column nullable with this query:您可以使用此查询将该列设置为可为空:

ALTER TABLE user MODIFY id_parent int(11) null;

Try this for codeigniter:试试这个代码点火器:

$fields = array(
                        'id_parent' => array(
                                                         'name' => 'id_parent',
                                                         'type' => 'INT',
                                                ),
);
$this->dbforge->modify_column('user', $fields);

or simply:或者干脆:

$this->db->query('ALTER TABLE user MODIFY id_parent int(11) null;');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM