代码之家  ›  专栏  ›  技术社区  ›  ajile

我怎样才能在条令中的表中设置NULL

  •  10
  • ajile  · 技术社区  · 15 年前

    store_section id , parent_id label ),我想更改一些行,设置parent\u id=null。

    $record = $table->getTable()->find( $id );
    $record->parent_id = null;
    $record->save();
    

    但这不是工作。我怎样才能在条令中的表中设置NULL,在上面的例子中,parent\u id变为=0(not=NULL)?

    谢谢你的回答!

    6 回复  |  直到 15 年前
        1
  •  12
  •   KSolo    14 年前

    我会尝试以下方法之一:

    1:

    $record = $table->getTable()->find( $id );
    $record->parent_id = new Doctrine_Null();
    $record->save();
    

    2:

    $record = $table->getTable()->find( $id );
    $record->parent_id = "NULL";
    $record->save();
    

    我没有测试这些,但我确实记得以前有过类似的问题,只是想不起来我是如何解决的。希望这有帮助!

        2
  •  4
  •   Sergey Kharchishin    13 年前

    例如:

    Doctrine_Query::create()->update('User u')->set('u.name', 'NULL')->where('u.id = ?',$id);
    
        3
  •  3
  •   Mike Purcell    9 年前

    Doctrine_Null 只有两种方法,其中一种似乎是 __toString

    $record = $table->getTable()->find( $id );
    $record->parent_id = (string) new Doctrine_Null;
    $record->save(); 
    

    但老实说,没有理由 无效 只是提取一个空字符串 '' . 我只能假设它只在这种情况下起作用,因为parent\u id没有强制 NULL

    将值设置为“NULL”

    'NULL' !== null
    

    试一试,如果您在一行中插入'NULL',而另一行是“自然NULL”,您将两行都从表中拉出并执行 var_dump(serialize()) 在每一页上,您将看到一个是自然空值,另一个实际上是字符串。

    $record = $table->getTable()->find( $id );
    $record->parent_id = new Doctrine_Expression('NULL');
    $record->save();  
    
        4
  •  1
  •   oaugustus    14 年前

    在本例中,当字段是关系时。我完成了这项任务:

    $record->Parent = null;
    $record->save();
    

    父级上方是关系名称。

        5
  •  0
  •   yvoyer    6 年前

    在Doctrine2中,使用查询生成器,还可以直接更新数据库中的值。

        $qb = // $this->connection->createQueryBuilder(); or $this->em->createQueryBuilder();
        $qb->update('store_section', 's')
            ->set('s.parent_id', ':parent_id')
            ->andWhere('s.id', ':id'));
        $qb->setParameter('parent_id', null);
        $qb->setParameter('id', $id);
        $qb->execute();
    
        6
  •  -1
  •   Sergey Kharchishin    13 年前
    $record->setParentId(null);
    $record->save();
    
    推荐文章