Yoast\WP\Lib

Model::belongs_to()protectedYoast 1.0

Helper method to manage one-to-one and one-to-many relations where the foreign key is on the base table.

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

Хуков нет.

Возвращает

$this|null. Instance of the foreign model.

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

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

Код Model::belongs_to() Yoast 24.0

protected function belongs_to( $associated_class_name, $foreign_key_name = null, $foreign_key_name_in_associated_models_table = null ) {
	$this->set_table_name( $associated_class_name );

	$associated_table_name = static::get_table_name_for_class( static::$auto_prefix_models . $associated_class_name );
	$foreign_key_name      = static::build_foreign_key_name( $foreign_key_name, $associated_table_name );
	$associated_object_id  = $this->{$foreign_key_name};

	if ( $foreign_key_name_in_associated_models_table === null ) {
		/*
		 * Comparison: "{$associated_table_name}.primary_key = {$associated_object_id}".
		 *
		 * NOTE: primary_key is a placeholder for the actual primary key column's name in $associated_table_name.
		 */
		return static::factory( $associated_class_name )->where_id_is( $associated_object_id );
	}

	// Comparison: "{$associated_table_name}.{$foreign_key_name_in_associated_models_table} = {$associated_object_id}".
	return static::factory( $associated_class_name )
		->where( $foreign_key_name_in_associated_models_table, $associated_object_id );
}