Active
Project:
Drupal core
Version:
main
Component:
database system
Priority:
Normal
Category:
Feature request
Assigned:
Unassigned
Reporter:
Created:
8 Dec 2011 at 18:00 UTC
Updated:
8 Jun 2021 at 07:30 UTC
Jump to comment: Most recent
I'm thinking that there's no point to extend a query by the same class twice; but sometimes query manipulation means you don't know which extenders have been used already.
Is this something core should take care of?
Here's the approach I'm working on for Views, as a subclass there obviously:
/**
* A query extender that can only be used once to extend a query.
*/
class SelectQueryExtenderSingleton extends SelectQueryExtender {
/**
* An array of classes in the chain of extenders.
*/
public $extenderChain = array();
public function __construct(SelectQueryInterface $query, DatabaseConnection $connection) {
parent::__construct($query, $connection);
// Add this class to the extender chain.
$class = get_class($this);
$this->extenderChain[$class] = $class;
}
/**
* Extend the query with the given extender class.
*/
public function extend($extender_name) {
// The extender can be anywhere so this needs to go to the registry, which
// is surely loaded by now.
$class = $this->connection->getDriverClass($extender_name, array(), TRUE);
// If this class is already in the chain, don't extend: just return this.
if (isset($this->extenderChain[$class])) {
return $this;
}
$extender = new $class($this, $this->connection);
$extender->extenderChain += $this->extenderChain;
return $extender;
}
}
Comments
Comment #1
joachim commentedAnother method might be to set $query->ExtenderClass on the original query, and then $query->ExtenderClassNext on both the original and first extension for a subsequent extension, and so on.
This would have the advantage that you can always grab a particular extender class query in the chain if you need it.
Comment #10
andypost