Php 如何在codeigniter中使用列集NULL更新表

Php 如何在codeigniter中使用列集NULL更新表,php,mysql,codeigniter,activerecord,null,Php,Mysql,Codeigniter,Activerecord,Null,我有一个更新列的问题,将数据设置为NULL,我有一个表user,每个列都是id\u parent列,用户可以删除父项,也可以添加父项,因此如果user有parent,id\u parent将设置用户的父项id,但用户可以删除父项的数据,因此,如果userdelete parent的数据,id\u parent列将设置为NULL。所以,如何在数据库中将数据设置为null,而不是”,而是null。 这是我的user表 user id_user | name | address | id_paren

我有一个更新列的问题,将数据设置为NULL,我有一个表
user
,每个列都是
id\u parent
列,用户可以删除父项,也可以添加父项,因此如果
user
parent
id\u parent
将设置用户的
父项id
,但用户可以删除父项的数据,因此,如果
user
delete parent的数据,
id\u parent
列将设置为NULL。所以,如何在数据库中将数据设置为null,而不是
,而是
null
。 这是我的
user

user

id_user | name | address | id_parent

您的用户表必须在
id\u parent
字段中将属性设置为is\u null。否则你不能

查询应该类似于

Update table user set id_parent = null where id_user = X
可以使用此查询将该列设置为空:

ALTER TABLE user MODIFY id_parent int(11) null;
请为codeigniter尝试以下操作:

$fields = array(
                        'id_parent' => array(
                                                         'name' => 'id_parent',
                                                         'type' => 'INT',
                                                ),
);
$this->dbforge->modify_column('user', $fields);
或者简单地说:

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

您可以在“model”中尝试此代码,希望它能正常工作:

public function update_std_marks($id_user) {

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

使用此选项可设置空列。这将起作用,不要忘记在活动记录上设置的第三个参数上添加false

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

我很好奇,所以我想看看CodeIgniter的活动记录在处理空值时如何处理不同的语法变化。我将为
where()
set()
方法调用绘制几个测试用例

电池的
where()
测试:

  • 正确引用和格式:#1、#5、#6(推荐
    逻辑形式,但未引用:#2,#7(不推荐
    不适当的呈现语法:#3、#4、#8、#9(不使用


    set()电池的测试:

  • 正确引用和格式:#1(推荐
    逻辑形成,但未引用:#4(不推荐

    不正确的呈现语法:#2、#3、#5、#6、#7、#8、#9(不要使用

    ,这样我就可以使用alter table/?在codeigniter中使用活动记录也是一样的吗?
    ->where('col', null)                  // `col` IS NULL
    
    ->where('col', null, false)           // col IS NULL
    
    ->where('col', 'NULL')                // `col` = 'NULL'
    
    ->where('col', 'NULL', false)         // col =  NULL
    
    ->where('col IS NULL')                // `col` IS NULL
    
    ->where('col IS NULL', null)          // `col` IS NULL
    
    ->where('col IS NULL', null, false)   // col IS NULL
    
    ->where('col', 'IS NULL')             // `col` 'IS NULL'
    
    ->where('col', 'IS NULL', false)      // col = IS NULL
    
    ->set('col', null)                  // `col` = NULL
    
    ->set('col', null, false)           // col =
    
    ->set('col', 'NULL')                // `col` = 'NULL'
    
    ->set('col', 'NULL', false)         // col = NULL
    
    ->set('col IS NULL')                // `col IS` `NULL` = ''
    
    ->set('col IS NULL', null)          // `col IS` `NULL` = NULL
    
    ->set('col IS NULL', null, false)   // col IS NULL =
    
    ->set('col', 'IS NULL')             // `col` = 'IS NULL'
    
    ->set('col', 'IS NULL', false)      // col = IS NULL