2017-01-24 56 views
2

QueryBuilder的更新,我有以下原始SQL查詢:與CONCAT

UPDATE mySock s1 
LEFT JOIN mySock s2 
ON s1.parentId = s2.id 
SET 
    s1.status = 1 
    s1.mylevel = (s2.mylevel + 1), 
    s1.parentString = CONCAT(s2.parentString, ':' CONCT(s1.id as char)) 
WHERE 
    s1.zz = 0; 

和創造的Symfony 3.2

public function updateParentNew($idParent) 
{ 
    return $this->createQueryBuilder('s1') 
     ->update('MyBundle:Stock', 's1') 
     ->leftJoin(''MyBundle:Stock', 's2', 'WITH', 's2.id = s1.parentId') 
     ->set('s1.zz', 1) 
     ->set('s1.leveltask', 's2.leveltask + 1') 
     ->set('s1.parentString', '?par2_string') 
     ->where('s1.zz = 0') 
     ->andWhere('s1.parentId = ?par1') 
     ->setParameter('par1', $idParent) 
     ->setParameter('par2_string', s2.parentString + ':' + (string)s1.id) 
     ->getQuery() 
     ->getSingleScalarResult(); 
} 

它不工作。 Concat值(字符串和數字)的方式是什麼?

+0

請實體'Stock'更新你的問題。 –

+0

你會得到什麼錯誤? –

回答

1

我會嘗試這樣的事:

public function updateParentNew($idParent) 
{ 
    $qb = $this->createQueryBuilder('s1') 
     ->update('MyBundle:Stock', 's1') 
     ->leftJoin('MyBundle:Stock', 's2', 'WITH', 's2.id = s1.parentId') 
    ; 

    $qb->set('s1.zz', 1) 
     ->set('s1.leveltask', $qb->expr()->sum('s2.leveltask', 1)) 
     ->set('s1.parentString', 
      $qb->expr()->concat(
       $qb->expr()->literal('s2.parentString'), 
       $qb->expr()->concat(':', $qb->expr()->literal('s1.id')) 
     )) 
     ->where('s1.zz = 0') 
     ->andWhere('s1.parentId = :par1') 
     ->setParameter('par1', $idParent) 
     ->getQuery() 
     ->getSingleScalarResult() 
    ; 

    return $qb; 
}