代码之家  ›  专栏  ›  技术社区  ›  Goran Jurić

应在何时更新

  •  1
  • Goran Jurić  · 技术社区  · 15 年前

    我试图使用做最简单的更新查询,但条令问题的INSERT语句,而不是更新。

        $q = Doctrine_Query::create()
            ->from('Image i')
            ->where('id = ?');
    
        $image = $q->fetchOne($articleId, Doctrine_Core::HYDRATE_RECORD);
        $image->copyright = "some text";
        $image->save();
    

    我也尝试过使用手册中的示例,但仍然插入了一条新记录:

    $userTable = Doctrine_Core::getTable('User');
    
    $user = $userTable->find(2);
    
    if ($user !== false) {
        $user->username = 'Jack Daniels';
        $user->save();
    }
    

    编辑:

    本手册示例如下:

    $user = new User();
    $user->assignIdentifier(1);
    $user->username = 'jwage';
    $user->save();
    

    有趣的是,我用这个在另一个模型上,在那里它工作正常。也许我必须获取整个数组图才能工作(我有一对多关系中的另一个模型)?

    4 回复  |  直到 15 年前
        1
  •  1
  •   Goran Jurić    15 年前

    不要,我重复一遍 不要

    如果您仔细阅读本手册,就会发现应该使用construct()方法(前面不带“\”。当使用“\uu construct()”并在内部调用parent::\uu construct()时,大多数事情都会工作,但记录状态不是它们的状态,这会导致各种问题,其中一个问题在上面已经解释过:)

    我想我们得等教条2.0:)

        2
  •  1
  •   scotts    14 年前

    这个 replace() 这个方法对我有用。

        3
  •  0
  •   Tom    15 年前

    ->update('User u')
    ->set('u.username', '?', 'Jack Daniels')
    ->where(....)
    
        4
  •  0
  •   Jubayer Arefin    13 年前
    $q = Doctrine_Query::create()
    ->from('Image')
    ->where('id = ?' $id)
    ->fetchOne();
    
    $q->copyright = "some text";
    $q->save();
    
    This should work.