简体   繁体   中英

How can I check in my database to see if a value of a column is being flushed more than once?

The Symfony2 Documentation doesn't seem to address this problem the 'symfony way'. As in I want to find a solution using Doctrine to query the field.

I want to write code to say… if (value is being added into database and the value already exists) then simply add '1' to the 'quantity' field.

Doctrine doesn't have a built in way for doing this as it seems like it's custom logic, I'd do something like this

$item = // .. Get Item From Repository
$newQuantity = 10;

if ($item->getQuantity() === $newQuantity) {
    $item->setQuantity($item->getQuantity() + 1);
} else {
    $item->setQuantity($newQuantity);
}

$entityManager->flush();
$inputData = x; // your input data

$em = $this->getDoctrine()->getManager();
$entity = newYourEntityName();

$entity = $em->getRepository('corresponding entity)->findOneBy($inputData);

if( !empty($entity) )
{
 $entity->setwhateveritis($entity->setwhateveritis++);
}
else
{
 $entity->setwhateveritis($inputData);
}
$em->persist($entity);
$em->flush();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM