Model::has_one_or_many()protectedYoast 1.0

Internal method to construct the queries for both the has_one and has_many methods. These two types of association are identical; the only difference is whether find_one or find_many is used to complete the method chain.

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

Хуков нет.


ORM. Instance of the ORM.


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

Код Model::has_one_or_many() Yoast 20.0

protected function has_one_or_many( $associated_class_name, $foreign_key_name = null, $foreign_key_name_in_current_models_table = null ) {
	$base_table_name  = static::get_table_name_for_class( \get_class( $this ) );
	$foreign_key_name = static::build_foreign_key_name( $foreign_key_name, $base_table_name );

	 * Value of foreign_table.{$foreign_key_name} we're looking for. Where foreign_table is the actual
	 * database table in the associated model.
	if ( $foreign_key_name_in_current_models_table === null ) {
		// Matches foreign_table.{$foreign_key_name} with the value of "{$this->table}.{$this->id()}".
		$where_value = $this->id();
	else {
		// Matches foreign_table.{$foreign_key_name} with "{$this->table}.{$foreign_key_name_in_current_models_table}".
		$where_value = $this->{$foreign_key_name_in_current_models_table};

	return static::factory( $associated_class_name )->where( $foreign_key_name, $where_value );