Commit 5dbc49ae authored by catch's avatar catch
Browse files

task: #3625895 Deprecate and remove Query::__wakeup() and ::__sleep()

By: mcdruid
By: longwave
By: ghost of drupal past
By: mondrake
(cherry picked from commit c305d653)
parent cb1b6c0c
Loading
Loading
Loading
Loading
Loading
+10 −9
Changes for core/lib/Drupal/Core/Database/Query/Query.php: 10 added lines, 9 removed lines.
Original line number Diff line number Diff line
@@ -2,7 +2,6 @@

namespace Drupal\Core\Database\Query;

use Drupal\Core\Database\Database;
use Drupal\Core\Database\Connection;

/**
@@ -81,19 +80,21 @@ public function __construct(Connection $connection, $options) {
  }

  /**
   * Implements the magic __sleep function to disconnect from the database.
   * Prevents serialization of query objects.
   *
   * @throws \LogicException
   */
  public function __sleep(): array {
    $keys = get_object_vars($this);
    unset($keys['connection']);
    return array_keys($keys);
  public function __serialize(): array {
    throw new \LogicException('Database query objects are not serializable. This probably means you are serializing an object that has a reference to a query object. Adjust your code so that is not necessary, for example by storing the query arguments or results instead.');
  }

  /**
   * Implements the magic __wakeup function to reconnect to the database.
   * Prevents unserialization of query objects.
   *
   * @throws \LogicException
   */
  public function __wakeup(): void {
    $this->connection = Database::getConnection($this->connectionTarget, $this->connectionKey);
  public function __unserialize(array $data): void {
    throw new \LogicException('Database query objects cannot be unserialized.');
  }

  /**
+14 −8
Changes for core/tests/Drupal/KernelTests/Core/Database/SerializeQueryTest.php: 14 added lines, 8 removed lines.
Original line number Diff line number Diff line
@@ -15,17 +15,23 @@
class SerializeQueryTest extends DatabaseTestBase {

  /**
   * Confirms that a query can be serialized and unserialized.
   * Confirms that a query cannot be serialized.
   */
  public function testSerializeQuery(): void {
    $query = $this->connection->select('test');
    $query->addField('test', 'age');
    $query->condition('name', 'Ringo');
    // If this doesn't work, it will throw an exception, so no need for an
    // assertion.
    $query = unserialize(serialize($query));
    $results = $query->execute()->fetchCol();
    $this->assertEquals(28, $results[0], 'Query properly executed after unserialization.');
    $this->expectException(\LogicException::class);
    $this->expectExceptionMessage('Database query objects are not serializable');
    serialize($query);
  }

  /**
   * Confirms that a query cannot be unserialized.
   */
  public function testUnserializeQuery(): void {
    $class = get_class($this->connection->select('test'));
    $this->expectException(\LogicException::class);
    $this->expectExceptionMessage('Database query objects cannot be unserialized');
    unserialize(sprintf('O:%d:"%s":0:{}', strlen($class), $class));
  }

}