Edit File by line
/home/barbar84/www/wp-conte.../plugins/wordpres.../lib
File: model.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace Yoast\WP\Lib;
[2] Fix | Delete
[3] Fix | Delete
use JsonSerializable;
[4] Fix | Delete
[5] Fix | Delete
/**
[6] Fix | Delete
* Make Model compatible with WordPress.
[7] Fix | Delete
*
[8] Fix | Delete
* Model base class. Your model objects should extend
[9] Fix | Delete
* this class. A minimal subclass would look like:
[10] Fix | Delete
*
[11] Fix | Delete
* class Widget extends Model {
[12] Fix | Delete
* }
[13] Fix | Delete
*/
[14] Fix | Delete
class Model implements JsonSerializable {
[15] Fix | Delete
[16] Fix | Delete
/**
[17] Fix | Delete
* Default ID column for all models. Can be overridden by adding
[18] Fix | Delete
* a public static $id_column property to your model classes.
[19] Fix | Delete
*
[20] Fix | Delete
* @var string
[21] Fix | Delete
*/
[22] Fix | Delete
const DEFAULT_ID_COLUMN = 'id';
[23] Fix | Delete
[24] Fix | Delete
/**
[25] Fix | Delete
* Default foreign key suffix used by relationship methods.
[26] Fix | Delete
*
[27] Fix | Delete
* @var string
[28] Fix | Delete
*/
[29] Fix | Delete
const DEFAULT_FOREIGN_KEY_SUFFIX = '_id';
[30] Fix | Delete
[31] Fix | Delete
/**
[32] Fix | Delete
* Set a prefix for model names. This can be a namespace or any other
[33] Fix | Delete
* abitrary prefix such as the PEAR naming convention.
[34] Fix | Delete
*
[35] Fix | Delete
* @example Model::$auto_prefix_models = 'MyProject_MyModels_'; //PEAR
[36] Fix | Delete
* @example Model::$auto_prefix_models = '\MyProject\MyModels\'; //Namespaces
[37] Fix | Delete
*
[38] Fix | Delete
* @var string
[39] Fix | Delete
*/
[40] Fix | Delete
public static $auto_prefix_models = '\Yoast\WP\SEO\Models\\';
[41] Fix | Delete
[42] Fix | Delete
/**
[43] Fix | Delete
* Set true to to ignore namespace information when computing table names
[44] Fix | Delete
* from class names.
[45] Fix | Delete
*
[46] Fix | Delete
* @example Model::$short_table_names = true;
[47] Fix | Delete
* @example Model::$short_table_names = false; // default
[48] Fix | Delete
*
[49] Fix | Delete
* @var bool
[50] Fix | Delete
*/
[51] Fix | Delete
public static $short_table_names = false;
[52] Fix | Delete
[53] Fix | Delete
/**
[54] Fix | Delete
* The ORM instance used by this model instance to communicate with the database.
[55] Fix | Delete
*
[56] Fix | Delete
* @var ORM
[57] Fix | Delete
*/
[58] Fix | Delete
public $orm;
[59] Fix | Delete
[60] Fix | Delete
/**
[61] Fix | Delete
* The table name for the implemented Model.
[62] Fix | Delete
*
[63] Fix | Delete
* @var string
[64] Fix | Delete
*/
[65] Fix | Delete
public static $table;
[66] Fix | Delete
[67] Fix | Delete
/**
[68] Fix | Delete
* Whether or not this model uses timestamps.
[69] Fix | Delete
*
[70] Fix | Delete
* @var bool
[71] Fix | Delete
*/
[72] Fix | Delete
protected $uses_timestamps = false;
[73] Fix | Delete
[74] Fix | Delete
/**
[75] Fix | Delete
* Which columns contain boolean values.
[76] Fix | Delete
*
[77] Fix | Delete
* @var array
[78] Fix | Delete
*/
[79] Fix | Delete
protected $boolean_columns = [];
[80] Fix | Delete
[81] Fix | Delete
/**
[82] Fix | Delete
* Which columns contain int values.
[83] Fix | Delete
*
[84] Fix | Delete
* @var array
[85] Fix | Delete
*/
[86] Fix | Delete
protected $int_columns = [];
[87] Fix | Delete
[88] Fix | Delete
/**
[89] Fix | Delete
* Which columns contain float values.
[90] Fix | Delete
*
[91] Fix | Delete
* @var array
[92] Fix | Delete
*/
[93] Fix | Delete
protected $float_columns = [];
[94] Fix | Delete
[95] Fix | Delete
/**
[96] Fix | Delete
* Hacks around the Model to provide WordPress prefix to tables.
[97] Fix | Delete
*
[98] Fix | Delete
* @param string $class_name Type of Model to load.
[99] Fix | Delete
* @param bool $yoast_prefix Optional. True to prefix the table name with the Yoast prefix.
[100] Fix | Delete
*
[101] Fix | Delete
* @return ORM Wrapper to use.
[102] Fix | Delete
*/
[103] Fix | Delete
public static function of_type( $class_name, $yoast_prefix = true ) {
[104] Fix | Delete
// Prepend namespace to the class name.
[105] Fix | Delete
$class = static::$auto_prefix_models . $class_name;
[106] Fix | Delete
[107] Fix | Delete
// Set the class variable to the custom value based on the WPDB prefix.
[108] Fix | Delete
$class::$table = static::get_table_name( $class_name, $yoast_prefix );
[109] Fix | Delete
[110] Fix | Delete
return static::factory( $class_name, null );
[111] Fix | Delete
}
[112] Fix | Delete
[113] Fix | Delete
/**
[114] Fix | Delete
* Creates a model without the Yoast prefix.
[115] Fix | Delete
*
[116] Fix | Delete
* @param string $class_name Type of Model to load.
[117] Fix | Delete
*
[118] Fix | Delete
* @return ORM
[119] Fix | Delete
*/
[120] Fix | Delete
public static function of_wp_type( $class_name ) {
[121] Fix | Delete
return static::of_type( $class_name, false );
[122] Fix | Delete
}
[123] Fix | Delete
[124] Fix | Delete
/**
[125] Fix | Delete
* Exposes method to get the table name to use.
[126] Fix | Delete
*
[127] Fix | Delete
* @param string $table_name Simple table name.
[128] Fix | Delete
* @param bool $yoast_prefix Optional. True to prefix the table name with the Yoast prefix.
[129] Fix | Delete
*
[130] Fix | Delete
* @return string Prepared full table name.
[131] Fix | Delete
*/
[132] Fix | Delete
public static function get_table_name( $table_name, $yoast_prefix = true ) {
[133] Fix | Delete
global $wpdb;
[134] Fix | Delete
[135] Fix | Delete
// Allow the use of WordPress internal tables.
[136] Fix | Delete
if ( $yoast_prefix ) {
[137] Fix | Delete
$table_name = 'yoast_' . $table_name;
[138] Fix | Delete
}
[139] Fix | Delete
[140] Fix | Delete
return $wpdb->prefix . \strtolower( $table_name );
[141] Fix | Delete
}
[142] Fix | Delete
[143] Fix | Delete
/**
[144] Fix | Delete
* Sets the table name for the given class name.
[145] Fix | Delete
*
[146] Fix | Delete
* @param string $class_name The class to set the table name for.
[147] Fix | Delete
*
[148] Fix | Delete
* @return void
[149] Fix | Delete
*/
[150] Fix | Delete
protected function set_table_name( $class_name ) {
[151] Fix | Delete
// Prepend namespace to the class name.
[152] Fix | Delete
$class = static::$auto_prefix_models . $class_name;
[153] Fix | Delete
[154] Fix | Delete
$class::$table = static::get_table_name( $class_name );
[155] Fix | Delete
}
[156] Fix | Delete
[157] Fix | Delete
/**
[158] Fix | Delete
* Retrieve the value of a static property on a class. If the
[159] Fix | Delete
* class or the property does not exist, returns the default
[160] Fix | Delete
* value supplied as the third argument (which defaults to null).
[161] Fix | Delete
*
[162] Fix | Delete
* @param string $class_name The target class name.
[163] Fix | Delete
* @param string $property The property to get the value for.
[164] Fix | Delete
* @param string|null $default Default value when property does not exist.
[165] Fix | Delete
*
[166] Fix | Delete
* @return string The value of the property.
[167] Fix | Delete
*/
[168] Fix | Delete
protected static function get_static_property( $class_name, $property, $default = null ) {
[169] Fix | Delete
if ( ! \class_exists( $class_name ) || ! \property_exists( $class_name, $property ) ) {
[170] Fix | Delete
return $default;
[171] Fix | Delete
}
[172] Fix | Delete
[173] Fix | Delete
$properties = \get_class_vars( $class_name );
[174] Fix | Delete
[175] Fix | Delete
return $properties[ $property ];
[176] Fix | Delete
}
[177] Fix | Delete
[178] Fix | Delete
/**
[179] Fix | Delete
* Static method to get a table name given a class name.
[180] Fix | Delete
* If the supplied class has a public static property
[181] Fix | Delete
* named $table, the value of this property will be
[182] Fix | Delete
* returned.
[183] Fix | Delete
*
[184] Fix | Delete
* If not, the class name will be converted using
[185] Fix | Delete
* the class_name_to_table_name() method.
[186] Fix | Delete
*
[187] Fix | Delete
* If Model::$short_table_names == true or public static
[188] Fix | Delete
* property $table_use_short_name == true then $class_name passed
[189] Fix | Delete
* to class_name_to_table_name() is stripped of namespace information.
[190] Fix | Delete
*
[191] Fix | Delete
* @param string $class_name The class name to get the table name for.
[192] Fix | Delete
*
[193] Fix | Delete
* @return string The table name.
[194] Fix | Delete
*/
[195] Fix | Delete
protected static function get_table_name_for_class( $class_name ) {
[196] Fix | Delete
$specified_table_name = static::get_static_property( $class_name, 'table' );
[197] Fix | Delete
$use_short_class_name = static::use_short_table_name( $class_name );
[198] Fix | Delete
if ( $use_short_class_name ) {
[199] Fix | Delete
$exploded_class_name = \explode( '\\', $class_name );
[200] Fix | Delete
$class_name = \end( $exploded_class_name );
[201] Fix | Delete
}
[202] Fix | Delete
[203] Fix | Delete
if ( $specified_table_name === null ) {
[204] Fix | Delete
return static::class_name_to_table_name( $class_name );
[205] Fix | Delete
}
[206] Fix | Delete
[207] Fix | Delete
return $specified_table_name;
[208] Fix | Delete
}
[209] Fix | Delete
[210] Fix | Delete
/**
[211] Fix | Delete
* Should short table names, disregarding class namespaces, be computed?
[212] Fix | Delete
*
[213] Fix | Delete
* $class_property overrides $global_option, unless $class_property is null.
[214] Fix | Delete
*
[215] Fix | Delete
* @param string $class_name The class name to get short name for.
[216] Fix | Delete
*
[217] Fix | Delete
* @return bool True when short table name should be used.
[218] Fix | Delete
*/
[219] Fix | Delete
protected static function use_short_table_name( $class_name ) {
[220] Fix | Delete
$class_property = static::get_static_property( $class_name, 'table_use_short_name' );
[221] Fix | Delete
[222] Fix | Delete
if ( $class_property === null ) {
[223] Fix | Delete
return static::$short_table_names;
[224] Fix | Delete
}
[225] Fix | Delete
[226] Fix | Delete
return $class_property;
[227] Fix | Delete
}
[228] Fix | Delete
[229] Fix | Delete
/**
[230] Fix | Delete
* Convert a namespace to the standard PEAR underscore format.
[231] Fix | Delete
*
[232] Fix | Delete
* Then convert a class name in CapWords to a table name in
[233] Fix | Delete
* lowercase_with_underscores.
[234] Fix | Delete
*
[235] Fix | Delete
* Finally strip doubled up underscores.
[236] Fix | Delete
*
[237] Fix | Delete
* For example, CarTyre would be converted to car_tyre. And
[238] Fix | Delete
* Project\Models\CarTyre would be project_models_car_tyre.
[239] Fix | Delete
*
[240] Fix | Delete
* @param string $class_name The class name to get the table name for.
[241] Fix | Delete
*
[242] Fix | Delete
* @return string The table name.
[243] Fix | Delete
*/
[244] Fix | Delete
protected static function class_name_to_table_name( $class_name ) {
[245] Fix | Delete
$find = [
[246] Fix | Delete
'/\\\\/',
[247] Fix | Delete
'/(?<=[a-z])([A-Z])/',
[248] Fix | Delete
'/__/',
[249] Fix | Delete
];
[250] Fix | Delete
$replacements = [
[251] Fix | Delete
'_',
[252] Fix | Delete
'_$1',
[253] Fix | Delete
'_',
[254] Fix | Delete
];
[255] Fix | Delete
[256] Fix | Delete
$class_name = \ltrim( $class_name, '\\' );
[257] Fix | Delete
$class_name = \preg_replace( $find, $replacements, $class_name );
[258] Fix | Delete
[259] Fix | Delete
return \strtolower( $class_name );
[260] Fix | Delete
}
[261] Fix | Delete
[262] Fix | Delete
/**
[263] Fix | Delete
* Return the ID column name to use for this class. If it is
[264] Fix | Delete
* not set on the class, returns null.
[265] Fix | Delete
*
[266] Fix | Delete
* @param string $class_name The class name to get the ID column for.
[267] Fix | Delete
*
[268] Fix | Delete
* @return string|null The ID column name.
[269] Fix | Delete
*/
[270] Fix | Delete
protected static function get_id_column_name( $class_name ) {
[271] Fix | Delete
return static::get_static_property( $class_name, 'id_column', static::DEFAULT_ID_COLUMN );
[272] Fix | Delete
}
[273] Fix | Delete
[274] Fix | Delete
/**
[275] Fix | Delete
* Build a foreign key based on a table name. If the first argument
[276] Fix | Delete
* (the specified foreign key column name) is null, returns the second
[277] Fix | Delete
* argument (the name of the table) with the default foreign key column
[278] Fix | Delete
* suffix appended.
[279] Fix | Delete
*
[280] Fix | Delete
* @param string $specified_foreign_key_name The keyname to build.
[281] Fix | Delete
* @param string $table_name The table name to build the key name for.
[282] Fix | Delete
*
[283] Fix | Delete
* @return string The built foreign key name.
[284] Fix | Delete
*/
[285] Fix | Delete
protected static function build_foreign_key_name( $specified_foreign_key_name, $table_name ) {
[286] Fix | Delete
if ( $specified_foreign_key_name !== null ) {
[287] Fix | Delete
return $specified_foreign_key_name;
[288] Fix | Delete
}
[289] Fix | Delete
[290] Fix | Delete
return $table_name . static::DEFAULT_FOREIGN_KEY_SUFFIX;
[291] Fix | Delete
}
[292] Fix | Delete
[293] Fix | Delete
/**
[294] Fix | Delete
* Factory method used to acquire instances of the given class.
[295] Fix | Delete
* The class name should be supplied as a string, and the class
[296] Fix | Delete
* should already have been loaded by PHP (or a suitable autoloader
[297] Fix | Delete
* should exist). This method actually returns a wrapped ORM object
[298] Fix | Delete
* which allows a database query to be built. The wrapped ORM object is
[299] Fix | Delete
* responsible for returning instances of the correct class when
[300] Fix | Delete
* its find_one or find_many methods are called.
[301] Fix | Delete
*
[302] Fix | Delete
* @param string $class_name The target class name.
[303] Fix | Delete
*
[304] Fix | Delete
* @return ORM Instance of the ORM wrapper.
[305] Fix | Delete
*/
[306] Fix | Delete
public static function factory( $class_name ) {
[307] Fix | Delete
$class_name = static::$auto_prefix_models . $class_name;
[308] Fix | Delete
$table_name = static::get_table_name_for_class( $class_name );
[309] Fix | Delete
$wrapper = ORM::for_table( $table_name );
[310] Fix | Delete
$wrapper->set_class_name( $class_name );
[311] Fix | Delete
$wrapper->use_id_column( static::get_id_column_name( $class_name ) );
[312] Fix | Delete
[313] Fix | Delete
return $wrapper;
[314] Fix | Delete
}
[315] Fix | Delete
[316] Fix | Delete
/**
[317] Fix | Delete
* Internal method to construct the queries for both the has_one and
[318] Fix | Delete
* has_many methods. These two types of association are identical; the
[319] Fix | Delete
* only difference is whether find_one or find_many is used to complete
[320] Fix | Delete
* the method chain.
[321] Fix | Delete
*
[322] Fix | Delete
* @param string $associated_class_name The associated class name.
[323] Fix | Delete
* @param string|null $foreign_key_name The foreign key name in the associated table.
[324] Fix | Delete
* @param string|null $foreign_key_name_in_current_models_table The foreign key in the current models table.
[325] Fix | Delete
*
[326] Fix | Delete
* @return ORM Instance of the ORM.
[327] Fix | Delete
*
[328] Fix | Delete
* @throws \Exception When ID of current model has a null value.
[329] Fix | Delete
*/
[330] Fix | Delete
protected function has_one_or_many( $associated_class_name, $foreign_key_name = null, $foreign_key_name_in_current_models_table = null ) {
[331] Fix | Delete
$base_table_name = static::get_table_name_for_class( \get_class( $this ) );
[332] Fix | Delete
$foreign_key_name = static::build_foreign_key_name( $foreign_key_name, $base_table_name );
[333] Fix | Delete
[334] Fix | Delete
/*
[335] Fix | Delete
* Value of foreign_table.{$foreign_key_name} we're looking for. Where foreign_table is the actual
[336] Fix | Delete
* database table in the associated model.
[337] Fix | Delete
*/
[338] Fix | Delete
if ( $foreign_key_name_in_current_models_table === null ) {
[339] Fix | Delete
// Matches foreign_table.{$foreign_key_name} with the value of "{$this->table}.{$this->id()}".
[340] Fix | Delete
$where_value = $this->id();
[341] Fix | Delete
}
[342] Fix | Delete
else {
[343] Fix | Delete
// Matches foreign_table.{$foreign_key_name} with "{$this->table}.{$foreign_key_name_in_current_models_table}".
[344] Fix | Delete
$where_value = $this->{$foreign_key_name_in_current_models_table};
[345] Fix | Delete
}
[346] Fix | Delete
[347] Fix | Delete
return static::factory( $associated_class_name )->where( $foreign_key_name, $where_value );
[348] Fix | Delete
}
[349] Fix | Delete
[350] Fix | Delete
/**
[351] Fix | Delete
* Helper method to manage one-to-one relations where the foreign
[352] Fix | Delete
* key is on the associated table.
[353] Fix | Delete
*
[354] Fix | Delete
* @param string $associated_class_name The associated class name.
[355] Fix | Delete
* @param string|null $foreign_key_name The foreign key name in the associated table.
[356] Fix | Delete
* @param string|null $foreign_key_name_in_current_models_table The foreign key in the current models table.
[357] Fix | Delete
*
[358] Fix | Delete
* @return ORM Instance of the ORM.
[359] Fix | Delete
*
[360] Fix | Delete
* @throws \Exception When ID of current model has a null value.
[361] Fix | Delete
*/
[362] Fix | Delete
protected function has_one( $associated_class_name, $foreign_key_name = null, $foreign_key_name_in_current_models_table = null ) {
[363] Fix | Delete
return $this->has_one_or_many( $associated_class_name, $foreign_key_name, $foreign_key_name_in_current_models_table );
[364] Fix | Delete
}
[365] Fix | Delete
[366] Fix | Delete
/**
[367] Fix | Delete
* Helper method to manage one-to-many relations where the foreign
[368] Fix | Delete
* key is on the associated table.
[369] Fix | Delete
*
[370] Fix | Delete
* @param string $associated_class_name The associated class name.
[371] Fix | Delete
* @param string|null $foreign_key_name The foreign key name in the associated table.
[372] Fix | Delete
* @param string|null $foreign_key_name_in_current_models_table The foreign key in the current models table.
[373] Fix | Delete
*
[374] Fix | Delete
* @return ORM Instance of the ORM.
[375] Fix | Delete
*
[376] Fix | Delete
* @throws \Exception When ID has a null value.
[377] Fix | Delete
*/
[378] Fix | Delete
protected function has_many( $associated_class_name, $foreign_key_name = null, $foreign_key_name_in_current_models_table = null ) {
[379] Fix | Delete
$this->set_table_name( $associated_class_name );
[380] Fix | Delete
[381] Fix | Delete
return $this->has_one_or_many( $associated_class_name, $foreign_key_name, $foreign_key_name_in_current_models_table );
[382] Fix | Delete
}
[383] Fix | Delete
[384] Fix | Delete
/**
[385] Fix | Delete
* Helper method to manage one-to-one and one-to-many relations where
[386] Fix | Delete
* the foreign key is on the base table.
[387] Fix | Delete
*
[388] Fix | Delete
* @param string $associated_class_name The associated class name.
[389] Fix | Delete
* @param string|null $foreign_key_name The foreign key in the current models table.
[390] Fix | Delete
* @param string|null $foreign_key_name_in_associated_models_table The foreign key in the associated table.
[391] Fix | Delete
*
[392] Fix | Delete
* @return $this|null Instance of the foreign model.
[393] Fix | Delete
*/
[394] Fix | Delete
protected function belongs_to( $associated_class_name, $foreign_key_name = null, $foreign_key_name_in_associated_models_table = null ) {
[395] Fix | Delete
$this->set_table_name( $associated_class_name );
[396] Fix | Delete
[397] Fix | Delete
$associated_table_name = static::get_table_name_for_class( static::$auto_prefix_models . $associated_class_name );
[398] Fix | Delete
$foreign_key_name = static::build_foreign_key_name( $foreign_key_name, $associated_table_name );
[399] Fix | Delete
$associated_object_id = $this->{$foreign_key_name};
[400] Fix | Delete
$desired_record = null;
[401] Fix | Delete
if ( $foreign_key_name_in_associated_models_table === null ) {
[402] Fix | Delete
/*
[403] Fix | Delete
* Comparison: "{$associated_table_name}.primary_key = {$associated_object_id}".
[404] Fix | Delete
*
[405] Fix | Delete
* NOTE: primary_key is a placeholder for the actual primary key column's name in $associated_table_name.
[406] Fix | Delete
*/
[407] Fix | Delete
return static::factory( $associated_class_name )->where_id_is( $associated_object_id );
[408] Fix | Delete
}
[409] Fix | Delete
[410] Fix | Delete
// Comparison: "{$associated_table_name}.{$foreign_key_name_in_associated_models_table} = {$associated_object_id}".
[411] Fix | Delete
return static::factory( $associated_class_name )
[412] Fix | Delete
->where( $foreign_key_name_in_associated_models_table, $associated_object_id );
[413] Fix | Delete
}
[414] Fix | Delete
[415] Fix | Delete
/**
[416] Fix | Delete
* Helper method to manage many-to-many relationships via an intermediate model. See
[417] Fix | Delete
* README for a full explanation of the parameters.
[418] Fix | Delete
*
[419] Fix | Delete
* @param string $associated_class_name The associated class name.
[420] Fix | Delete
* @param string|null $join_class_name The class name to join.
[421] Fix | Delete
* @param string|null $key_to_base_table The key to the the current models table.
[422] Fix | Delete
* @param string|null $key_to_associated_table The key to the associated table.
[423] Fix | Delete
* @param string|null $key_in_base_table The key in the current models table.
[424] Fix | Delete
* @param string|null $key_in_associated_table The key in the associated table.
[425] Fix | Delete
*
[426] Fix | Delete
* @return ORM Instance of the ORM.
[427] Fix | Delete
*/
[428] Fix | Delete
protected function has_many_through( $associated_class_name, $join_class_name = null, $key_to_base_table = null, $key_to_associated_table = null, $key_in_base_table = null, $key_in_associated_table = null ) {
[429] Fix | Delete
$base_class_name = \get_class( $this );
[430] Fix | Delete
[431] Fix | Delete
/*
[432] Fix | Delete
* The class name of the join model, if not supplied, is formed by
[433] Fix | Delete
* concatenating the names of the base class and the associated class,
[434] Fix | Delete
* in alphabetical order.
[435] Fix | Delete
*/
[436] Fix | Delete
if ( $join_class_name === null ) {
[437] Fix | Delete
$base_model = \explode( '\\', $base_class_name );
[438] Fix | Delete
$base_model_name = \end( $base_model );
[439] Fix | Delete
if ( \strpos( $base_model_name, static::$auto_prefix_models ) === 0 ) {
[440] Fix | Delete
$base_model_name = \substr( $base_model_name, \strlen( static::$auto_prefix_models ), \strlen( $base_model_name ) );
[441] Fix | Delete
}
[442] Fix | Delete
// Paris wasn't checking the name settings for the associated class.
[443] Fix | Delete
$associated_model = \explode( '\\', $associated_class_name );
[444] Fix | Delete
$associated_model_name = \end( $associated_model );
[445] Fix | Delete
if ( \strpos( $associated_model_name, static::$auto_prefix_models ) === 0 ) {
[446] Fix | Delete
$associated_model_name = \substr( $associated_model_name, \strlen( static::$auto_prefix_models ), \strlen( $associated_model_name ) );
[447] Fix | Delete
}
[448] Fix | Delete
$class_names = [ $base_model_name, $associated_model_name ];
[449] Fix | Delete
\sort( $class_names, \SORT_STRING );
[450] Fix | Delete
$join_class_name = \implode( '', $class_names );
[451] Fix | Delete
}
[452] Fix | Delete
[453] Fix | Delete
// Get table names for each class.
[454] Fix | Delete
$base_table_name = static::get_table_name_for_class( $base_class_name );
[455] Fix | Delete
$associated_table_name = static::get_table_name_for_class( static::$auto_prefix_models . $associated_class_name );
[456] Fix | Delete
$join_table_name = static::get_table_name_for_class( static::$auto_prefix_models . $join_class_name );
[457] Fix | Delete
[458] Fix | Delete
// Get ID column names.
[459] Fix | Delete
$base_table_id_column = ( $key_in_base_table === null ) ? static::get_id_column_name( $base_class_name ) : $key_in_base_table;
[460] Fix | Delete
$associated_table_id_column = ( $key_in_associated_table === null ) ? static::get_id_column_name( static::$auto_prefix_models . $associated_class_name ) : $key_in_associated_table;
[461] Fix | Delete
[462] Fix | Delete
// Get the column names for each side of the join table.
[463] Fix | Delete
$key_to_base_table = static::build_foreign_key_name( $key_to_base_table, $base_table_name );
[464] Fix | Delete
$key_to_associated_table = static::build_foreign_key_name( $key_to_associated_table, $associated_table_name );
[465] Fix | Delete
[466] Fix | Delete
/* phpcs:ignore Squiz.PHP.CommentedOutCode.Found -- Reason: This is commented out code.
[467] Fix | Delete
" SELECT {$associated_table_name}.*
[468] Fix | Delete
FROM {$associated_table_name} JOIN {$join_table_name}
[469] Fix | Delete
ON {$associated_table_name}.{$associated_table_id_column} = {$join_table_name}.{$key_to_associated_table}
[470] Fix | Delete
WHERE {$join_table_name}.{$key_to_base_table} = {$this->$base_table_id_column} ;"
[471] Fix | Delete
*/
[472] Fix | Delete
[473] Fix | Delete
return static::factory( $associated_class_name )
[474] Fix | Delete
->select( "{$associated_table_name}.*" )
[475] Fix | Delete
->join(
[476] Fix | Delete
$join_table_name,
[477] Fix | Delete
[
[478] Fix | Delete
"{$associated_table_name}.{$associated_table_id_column}",
[479] Fix | Delete
'=',
[480] Fix | Delete
"{$join_table_name}.{$key_to_associated_table}",
[481] Fix | Delete
]
[482] Fix | Delete
)
[483] Fix | Delete
->where( "{$join_table_name}.{$key_to_base_table}", $this->{$base_table_id_column} );
[484] Fix | Delete
}
[485] Fix | Delete
[486] Fix | Delete
/**
[487] Fix | Delete
* Set the wrapped ORM instance associated with this Model instance.
[488] Fix | Delete
*
[489] Fix | Delete
* @param ORM $orm The ORM instance to set.
[490] Fix | Delete
*
[491] Fix | Delete
* @return void
[492] Fix | Delete
*/
[493] Fix | Delete
public function set_orm( $orm ) {
[494] Fix | Delete
$this->orm = $orm;
[495] Fix | Delete
}
[496] Fix | Delete
[497] Fix | Delete
/**
[498] Fix | Delete
* Magic getter method, allows $model->property access to data.
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function