【问题标题】:How to update table with column set NULL in codeigniter如何在codeigniter中使用列集NULL更新表
【发布时间】:2015-09-29 10:55:32
【问题描述】:

我在更新列时遇到问题,将数据设置为 NULL,我有一个表 user,每一列都是 id_parent 列,用户可以删除父级并可以添加父级,所以如果 user 有一个 @ 987654324@, id_parent 将设置为用户的parent id,但用户可以删除父级的数据,所以如果user 删除父级的数据,id_parent 列将设置为 NULL。那么如何在数据库中将数据设置为空,而不是" ",而是NULL。 这是我的user 表。

user

id_user | name | address | id_parent

【问题讨论】:

    标签: php mysql codeigniter activerecord null


    【解决方案1】:

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

    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
      

    正确引用和形成:#1、#5、#6(推荐)
    符合逻辑,但未引用:#2、#7(不推荐)
    不恰当的渲染语法:#3、#4、#8、#9(不要使用)


    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
      

    正确引用和形成:#1(推荐)
    符合逻辑,但未引用:#4(不推荐)
    不恰当的渲染语法:#2、#3、#5、#6、#7、#8、#9(不要使用)

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        【解决方案3】:

        您可以在“模型”中尝试此代码,希望它会起作用:

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

        【讨论】:

          【解决方案4】:

          您的用户表必须在id_parent 字段中将属性设置为is_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;');
          

          【讨论】:

          • 所以我可以使用 alter table/?在codeigniter中使用活动记录是一样的吗?
          猜你喜欢
          • 1970-01-01
          • 2015-06-10
          • 2019-12-20
          • 2019-07-28
          • 2018-06-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-10-26
          相关资源
          最近更新 更多