-1

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();
}
DarkBee
  • 16,592
  • 6
  • 46
  • 58
sam
  • 127
  • 1
  • 5
  • 2
    _"i want to concat the new value with old value with a comma seperation"_ - you should rather not want that to begin with. [Is storing a delimited list in a database column really that bad?](https://stackoverflow.com/q/3653462/1427878) (short answer: Yes) – CBroe Nov 07 '22 at 13:15
  • yeah, I got your point but this database is for testing purpose. So it is fine to have the delimeter in between the values. – sam Nov 07 '22 at 13:47

1 Answers1

1

Why you do not something like:

$foo = $fruitRepo->find($id);
/** @var string $fruits */
$fruits = $foo->getFruits();

$foo->setFruits($fruits . ',' . $newFruit);
$fruitRepo->save($foo); // this method persist and flush