开发者

Symfony 2 update related table by user id

开发者 https://www.devze.com 2023-03-28 20:27 出处:网络
Having a bit of a brain dead morning. Messing about with symfony2 and I can\'t work out how to update a table by a users id. For example: I have a profile table with a user i开发者_Python百科d that is

Having a bit of a brain dead morning. Messing about with symfony2 and I can't work out how to update a table by a users id. For example: I have a profile table with a user i开发者_Python百科d that is created on registration, so I have an empty table except for a user id. I want to update the the profile table row that belongs to the logged in user. I have a one to one relationship and a getUser method. ?? I am able to get the id of current logged in user, just don't know to update relevant table.


Considering your information I assume User is owner. I don't know if you set the profile on persisting of User...

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();

That fine? Or DQL (I haven't done it this way, may have syntax errors):

$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();
0

精彩评论

暂无评论...
验证码 换一张
取 消