Sling Academy
Home/PHP/PHP Doctrine: How to update a record by condition

PHP Doctrine: How to update a record by condition

Last updated: January 14, 2024

Overview

Doctrine ORM is a powerful tool for object-relational mapping in PHP applications, allowing developers to work with database records as objects. When you need to update records with certain conditions, Doctrine provides several ways to accomplish this task. In this tutorial, we’ll explore how to update a record by condition with PHP Doctrine.

Prerequisites

  • Basic understanding of PHP and Object-Oriented Programming (OOP)
  • PHP development environment with Composer
  • A working Doctrine ORM setup
  • An understanding of Doctrine Entity classes

Step-by-Step Guide

Just follow the steps below and you’ll get a deep understanding of the task.

Step 1: Setting Up Doctrine Entity Class

Before you can update a record, ensure you have the corresponding Doctrine entity class mapped to your database table. For example:

<?php

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="products")
 */
class Product
{
    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=100)
     */
    private $name;

    // ... additional fields ...

    // Getters and setters for your properties go here
}

Be sure your entity is fully configured and mapped correctly to the table in the database.

Step 2: Fetching the Object(s) to Update

To update a record, you first need to retrieve it using the Doctrine EntityManager. Here’s an example:

<?php

$entityManager = ...; // Get your EntityManager instance
$productRepository = $entityManager->getRepository(Product::class);
$product = $productRepository->findOneBy(['name' => 'Old Product Name']);

if ($product) {
    // Update the product's name
    $product->setName('New Product Name');
    
    // Persist the changes
    $entityManager->flush();
} else {
    echo 'Product not found';
}

Step 3: Bulk Update with QueryBuilder

If you need to update multiple records at once, you can use the Doctrine QueryBuilder. Below is an example to update all products with a certain condition:

<?php

$queryBuilder = $entityManager->createQueryBuilder();

$query = $queryBuilder
    ->update(Product::class, 'p')
    ->set('p.name', ':newName')
    ->where('p.id = :id')
    ->setParameter('newName', 'Updated Product Name')
    ->setParameter('id', $productId)
    ->getQuery();

$query->execute();

This updates the ‘name’ field of a product with a specific ‘id’. The same approach can be adapted to other conditions.

Step 4: Using Native SQL for Complex Updates

For more complex update operations, you may opt to use native SQL with Doctrine’s DBAL (Database Abstraction Layer). Here’s an example:

<?php

$connection = $entityManager->getConnection();
$platform = $connection->getDatabasePlatform();

$sql = 'UPDATE products SET name = ? WHERE id = ?';
$stmt = $connection->prepare($sql);
$stmt->bindValue(1, 'Updated Product Name');
$stmt->bindValue(2, $productId);
$stmt->execute();

This method should be used with caution, as it bypasses the ORM layer and does not consider any lifecycle events or entity state changes.

Step 5: Handling Transactions

It’s important to handle database transactions properly, especially when performing write operations. In Doctrine, you can wrap your updates in a transaction like so:

<?php

$entityManager->beginTransaction();

try {
    // Perform the update operations ...

    // If everything is fine, commit the transaction
    $entityManager->commit();
} catch (Exception $e) {
    // If something went wrong, rollback
    $entityManager->rollback();
    throw $e;
}

Conclusion

Updating records by condition using Doctrine in PHP requires careful handling of the ORM and transactional operations. Always ensure to use the correct methods suited for your requirements, whether updating a single object or performing bulk updates. Maintaining good practices in doing so will result in stable and maintainable code.

Next Article: PHP Doctrine: How to Delete a Record by Condition

Previous Article: PHP Doctrine: How to Safely Update Data Schema in Production

Series: Symfony & Doctrine Tutotirlas

PHP

You May Also Like

  • Pandas DataFrame.value_counts() method: Explained with examples
  • Constructor Property Promotion in PHP: Tutorial & Examples
  • Understanding mixed types in PHP (5 examples)
  • Union Types in PHP: A practical guide (5 examples)
  • PHP: How to implement type checking in a function (PHP 8+)
  • Symfony + Doctrine: Implementing cursor-based pagination
  • Laravel + Eloquent: How to Group Data by Multiple Columns
  • PHP: How to convert CSV data to HTML tables
  • Using ‘never’ return type in PHP (PHP 8.1+)
  • Nullable (Optional) Types in PHP: A practical guide (5 examples)
  • Explore Attributes (Annotations) in Modern PHP (5 examples)
  • An introduction to WeakMap in PHP (6 examples)
  • Type Declarations for Class Properties in PHP (5 examples)
  • Static Return Type in PHP: Explained with examples
  • PHP: Using DocBlock comments to annotate variables
  • PHP: How to ping a server/website and get the response time
  • PHP: 3 Ways to Get City/Country from IP Address
  • PHP: How to find the mode(s) of an array (4 examples)
  • PHP: Calculate standard deviation & variance of an array