use Yoast\WP\SEO\Config\Migration_Status;
* URL: http://github.com/j4mie/idiorm/
* A single-class super-simple database abstraction layer for PHP.
* Provides (nearly) zero-configuration object-relational mapping
* and a fluent interface for building basic, commonly-used queries.
* Copyright (c) 2010, Jamie Matthews
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* The methods documented below are magic methods that conform to PSR-1.
* This documentation exposes these methods to doc generators and IDEs.
* @see http://www.php-fig.org/psr/psr-1/
class ORM implements \ArrayAccess {
* --- CLASS CONSTANTS ---
const CONDITION_FRAGMENT = 0;
const CONDITION_VALUES = 1;
* --- INSTANCE PROPERTIES ---
* Holds the class name. Wrapped find_one and find_many classes will return an instance or instances of this class.
* Holds the name of the table the current ORM instance is associated with.
* Holds the alias for the table to be used in SELECT queries.
protected $table_alias = null;
* Values to be bound to the query.
* Columns to select in the result.
protected $result_columns = [ '*' ];
* Are we using the default result column or have these been manually changed?
protected $using_default_result_columns = true;
* Holds the join sources.
protected $join_sources = [];
* Should the query include a DISTINCT keyword?
protected $distinct = false;
protected $is_raw_query = false;
protected $raw_query = '';
* The raw query parameters.
protected $raw_parameters = [];
* Array of WHERE clauses.
protected $where_conditions = [];
protected $offset = null;
protected $order_by = [];
protected $group_by = [];
protected $having_conditions = [];
* The data for a hydrated instance of the class.
* Lifetime of the object.
protected $dirty_fields = [];
* Fields that are to be inserted in the DB raw.
protected $expr_fields = [];
* Is this a new object (has create() been called)?
protected $is_new = false;
* Name of the column to use as the primary key for
* this instance only. Overrides the config settings.
protected $instance_id_column = null;
* Factory method, return an instance of this class bound to the supplied
* A repeat of content in parent::for_table, so that created class is ORM.
* @param string $table_name The table to create instance for.
* @return ORM Instance of the ORM.
public static function for_table( $table_name ) {
return new static( $table_name, [] );
* Executes a raw query as a wrapper for wpdb::query.
* Useful for queries that can't be accomplished through Idiorm,
* particularly those using engine-specific features.
* @example raw_execute('INSERT OR REPLACE INTO `widget` (`id`, `name`) SELECT `id`, `name` FROM `other_table`')
* @example raw_execute('SELECT `name`, AVG(`order`) FROM `customer` GROUP BY `name` HAVING AVG(`order`) > 10')
* @param string $query The raw SQL query.
* @param array $parameters Optional bound parameters.
public static function raw_execute( $query, $parameters = [] ) {
return self::execute( $query, $parameters );
* Internal helper method for executing statements.
* @param string $query The query.
* @param array $parameters An array of parameters to be bound in to the query.
* @return bool|int Response of wpdb::query
protected static function execute( $query, $parameters = [] ) {
* The global WordPress database variable.
$show_errors = $wpdb->show_errors;
if ( YoastSEO()->classes->get( Migration_Status::class )->get_error( 'free' ) ) {
$wpdb->show_errors = false;
$parameters = \array_filter(
static function( $parameter ) {
return $parameter !== null;
if ( ! empty( $parameters ) ) {
$query = $wpdb->prepare( $query, $parameters );
$result = $wpdb->query( $query );
$wpdb->show_errors = $show_errors;
* --- INSTANCE METHODS ---
* "Private" constructor; shouldn't be called directly.
* Use the ORM::for_table factory method instead.
* @param string $table_name Table name.
* @param array $data Data to populate table.
protected function __construct( $table_name, $data = [] ) {
$this->table_name = $table_name;
* Sets the name of the class which the wrapped methods should return instances of.
* @param string $class_name The classname to set.
public function set_class_name( $class_name ) {
$this->class_name = $class_name;
* Creates a new, empty instance of the class. Used to add a new row to your database. May optionally be passed an
* associative array of data to populate the instance. If so, all fields will be flagged as dirty so all will be
* saved to the database when save() is called.
* @param array|null $data Data to populate table.
public function create( $data = null ) {
if ( ! \is_null( $data ) ) {
$this->hydrate( $data )->force_all_dirty();
return $this->create_model_instance( $this );
* Specifies the ID column to use for this instance or array of instances only.
* This overrides the id_column and id_column_overrides settings.
* This is mostly useful for libraries built on top of Idiorm, and will not normally be used in manually built
* queries. If you don't know why you would want to use this, you should probably just ignore it.
* @param string $id_column The ID column.
public function use_id_column( $id_column ) {
$this->instance_id_column = $id_column;
* Creates an ORM instance from the given row (an associative array of data fetched from the database).
* @param array $row A row from the database.
protected function create_instance_from_row( $row ) {
$instance = self::for_table( $this->table_name );
$instance->use_id_column( $this->instance_id_column );
$instance->hydrate( $row );
return $this->create_model_instance( $instance );
* Tells the ORM that you are expecting a single result back from your query, and execute it. Will return a single
* instance of the ORM class, or false if no rows were returned. As a shortcut, you may supply an ID as a parameter
* to this method. This will perform a primary key lookup on the table.
* @param int|null $id An (optional) ID.
public function find_one( $id = null ) {
if ( ! \is_null( $id ) ) {
$this->where_id_is( $id );
return $this->create_instance_from_row( $rows[0] );
* Tells the ORM that you are expecting multiple results from your query, and execute it. Will return an array of
* instances of the ORM class, or an empty array if no rows were returned.
public function find_many() {
return \array_map( [ $this, 'create_instance_from_row' ], $rows );
* Creates an instance of the model class associated with this wrapper and populate it with the supplied Idiorm
* @param ORM $orm The ORM used by model.
* @return bool|Model Instance of the model class.
protected function create_model_instance( $orm ) {
* An instance of Model is being made.
$model = new $this->class_name();
* Tells the ORM that you are expecting multiple results from your query, and execute it. Will return an array, or
* an empty array if no rows were returned.
* @return array The query results.
public function find_array() {
* Tells the ORM that you wish to execute a COUNT query.
* @param string $column The table column.
* @return float|int An integer representing the number of rows returned.
public function count( $column = '*' ) {
return $this->call_aggregate_db_function( __FUNCTION__, $column );
* Tells the ORM that you wish to execute a MAX query.
* @param string $column The table column.
* @return float|int The max value of the chosen column.
public function max( $column ) {
return $this->call_aggregate_db_function( __FUNCTION__, $column );
* Tells the ORM that you wish to execute a MIN query.
* @param string $column The table column.
* @return float|int The min value of the chosen column.
public function min( $column ) {
return $this->call_aggregate_db_function( __FUNCTION__, $column );
* Tells the ORM that you wish to execute a AVG query.
* @param string $column The table column.
* @return float|int The average value of the chosen column.
public function avg( $column ) {
return $this->call_aggregate_db_function( __FUNCTION__, $column );
* Tells the ORM that you wish to execute a SUM query.
* @param string $column The table column.
* @return float|int The sum of the chosen column.
public function sum( $column ) {
return $this->call_aggregate_db_function( __FUNCTION__, $column );
* Returns the select query as SQL.
* @return string The select query in SQL.