I am trying to update a column value using symfony query builder. The issue is I dont want the old value to be overwritten, i want to concat the new value with old value with a comma seperation, like below
id fruits
------------------
1 orange,apple
here is my query which is just update the old value, fruits column is of type longtext
public function updateById($id,$fruit) {
return $this->createQueryBuilder('c')
->update()
->set('c.fruits', ':fruits')
->where('c.id LIKE :id')
->setParameter('id', $id)
->setParameter('fruits', $fruit)
->getQuery()
->getArrayResult();
}
My Answer:
>createQueryBuilder()
->update('App\Entity\Fruit', 'f')
->set('f.fruits', 'CONCAT(f.fruits, :fruit)')
->where('f.id = :id')
->setParameter('id', $id)
->setParameter('fruit', ','.$fruit)
->getQuery()
->execute();
}
This query will concatenate the new fruit value with the old fruit value in the 'fruits' column of the 'Fruit' entity for the specified id.
Rate this post
4 of 5 based on 5548 votesComments