Yoast\WP\Lib

Model::has_many_through()protectedYoast 1.0

Helper method to manage many-to-many relationships via an intermediate model. See README for a full explanation of the parameters.

Метод класса: Model{}

Хуков нет.

Возвращает

ORM. Instance of the ORM.

Использование

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->has_many_through( $associated_class_name, $join_class_name, $key_to_base_table, $key_to_associated_table, $key_in_base_table, $key_in_associated_table );
$associated_class_name(строка) (обязательный)
The associated class name.
$join_class_name(строка|null)
The class name to join.
По умолчанию: null
$key_to_base_table(строка|null)
The key to the the current models table.
По умолчанию: null
$key_to_associated_table(строка|null)
The key to the associated table.
По умолчанию: null
$key_in_base_table(строка|null)
The key in the current models table.
По умолчанию: null
$key_in_associated_table(строка|null)
The key in the associated table.
По умолчанию: null

Код Model::has_many_through() Yoast 24.0

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 ) {
	$base_class_name = static::class;

	/*
	 * The class name of the join model, if not supplied, is formed by
	 * concatenating the names of the base class and the associated class,
	 * in alphabetical order.
	 */
	if ( $join_class_name === null ) {
		$base_model      = \explode( '\\', $base_class_name );
		$base_model_name = \end( $base_model );
		if ( \strpos( $base_model_name, static::$auto_prefix_models ) === 0 ) {
			$base_model_name = \substr( $base_model_name, \strlen( static::$auto_prefix_models ), \strlen( $base_model_name ) );
		}
		// Paris wasn't checking the name settings for the associated class.
		$associated_model      = \explode( '\\', $associated_class_name );
		$associated_model_name = \end( $associated_model );
		if ( \strpos( $associated_model_name, static::$auto_prefix_models ) === 0 ) {
			$associated_model_name = \substr( $associated_model_name, \strlen( static::$auto_prefix_models ), \strlen( $associated_model_name ) );
		}
		$class_names = [ $base_model_name, $associated_model_name ];
		\sort( $class_names, \SORT_STRING );
		$join_class_name = \implode( '', $class_names );
	}

	// Get table names for each class.
	$base_table_name       = static::get_table_name_for_class( $base_class_name );
	$associated_table_name = static::get_table_name_for_class( static::$auto_prefix_models . $associated_class_name );
	$join_table_name       = static::get_table_name_for_class( static::$auto_prefix_models . $join_class_name );

	// Get ID column names.
	$base_table_id_column       = ( $key_in_base_table === null ) ? static::get_id_column_name( $base_class_name ) : $key_in_base_table;
	$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;

	// Get the column names for each side of the join table.
	$key_to_base_table       = static::build_foreign_key_name( $key_to_base_table, $base_table_name );
	$key_to_associated_table = static::build_foreign_key_name( $key_to_associated_table, $associated_table_name );

	/* phpcs:ignore Squiz.PHP.CommentedOutCode.Found -- Reason: This is commented out code.
		"   SELECT {$associated_table_name}.*
			FROM {$associated_table_name} JOIN {$join_table_name}
				ON {$associated_table_name}.{$associated_table_id_column} = {$join_table_name}.{$key_to_associated_table}
			WHERE {$join_table_name}.{$key_to_base_table} = {$this->$base_table_id_column} ;"
	*/

	return static::factory( $associated_class_name )
		->select( "{$associated_table_name}.*" )
		->join(
			$join_table_name,
			[
				"{$associated_table_name}.{$associated_table_id_column}",
				'=',
				"{$join_table_name}.{$key_to_associated_table}",
			]
		)
		->where( "{$join_table_name}.{$key_to_base_table}", $this->{$base_table_id_column} );
}