【问题标题】:Symfony 2 update related table by user idSymfony 2 按用户 ID 更新相关表
【发布时间】:2011-08-18 15:10:10
【问题描述】:

早上有点脑残。与 symfony2 混在一起,我不知道如何通过用户 ID 更新表格。例如:我有一个配置文件表,其中包含在注册时创建的用户 ID,所以我有一个空表,除了用户 ID。我想更新属于登录用户的配置文件表行。我有一对一的关系和一个 getUser 方法。 ??我能够获取当前登录用户的 id,只是不知道更新相关表。

【问题讨论】:

    标签: doctrine-orm symfony


    【解决方案1】:

    考虑到您的信息,我假设用户是所有者。我不知道您是否将配置文件设置为持久用户...

    if (is_null($user->getProfile()) {
      $profile = new \Foo\BarBundle\Entity\Profile();
      $em->persist($profile);
      $user->setProfile($profile);
    } else $profile = $user->getProfile();
    
    $profile->setBarBaz('barbaz');
    $profile->setFooBaz('foobaz');
    $em->flush();
    

    这样好吗? 或者DQL(我没做过,可能有语法错误):

    $qb = $em->createQueryBuilder();
    $result = $qb->update('Foo\BarBundle\Entity\Profile', 'p')
      ->set('barBaz', 'barbaz')
      ->set('fooBaz', 'foobaz')
      ->where('p.user_id = ?1')->setParameter(1, $user->getId())
      ->getQuery()
      ->getResult();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-15
      • 1970-01-01
      • 2012-10-18
      相关资源
      最近更新 更多