使用concat更新QueryBuilder

时间:2017-01-24 13:19:01

标签: join sql-update symfony query-builder

我有以下原始SQL查询:

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值的方法是什么(字符串和数字)?

1 个答案:

答案 0 :(得分:2)

我会尝试这样的事情:

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;
}