PHP Класс Prado\Data\ActiveRecord\Relations\TActiveRecordHasOne

The difference of HAS_ONE from HAS_MANY is that the foreign object is singular. That is, HAS_MANY will return a collection of records while HAS_ONE returns the corresponding record. Consider the entity relationship between a Car and a Engine. +-----+ +--------+ | Car | 1 <----- 1 | Engine | +-----+ +--------+ Where each engine belongs to only one car, that is, the Engine entity has a foreign key to the Car's primary key. We may model Engine-Car object relationship as active record as follows. class CarRecord extends TActiveRecord { const TABLE='car'; public $car_id; //primary key public $colour; public $engine; //engine foreign object public static $RELATIONS=array ( 'engine' => array(self::HAS_ONE, 'EngineRecord') ); public static function finder($className=__CLASS__) { return parent::finder($className); } } class EngineRecord extends TActiveRecord { const TABLE='engine'; public $engine_id; public $capacity; public $car_id; //foreign key to cars public static function finder($className=__CLASS__) { return parent::finder($className); } } The static $RELATIONS property of CarRecord defines that the property $engine that will reference an EngineRecord instance. The car record with engine property list may be fetched as follows. $cars = CarRecord::finder()->with_engine()->findAll(); The method with_xxx() (where xxx is the relationship property name, in this case, engine) fetchs the corresponding EngineRecords using a second query (not by using a join). The with_xxx() accepts the same arguments as other finder methods of TActiveRecord, e.g. with_engine('capacity < ?', 3.8).
С версии: 3.1
Наследование: extends TActiveRecordRelation
Показать файл Открыть проект

Открытые методы

Метод Описание
getRelationForeignKeys ( ) : array
updateAssociatedRecords ( ) : boolean Updates the associated foreign objects.

Защищенные методы

Метод Описание
collectForeignObjects ( &$results ) Get the foreign key index values from the results and make calls to the database to find the corresponding foreign objects.
setObjectProperty ( $source, $properties, &$collections ) Sets the foreign objects to the given property on the source object.

Описание методов

collectForeignObjects() защищенный Метод

Get the foreign key index values from the results and make calls to the database to find the corresponding foreign objects.
protected collectForeignObjects ( &$results )

getRelationForeignKeys() публичный Метод

С версии: 3.1.2
public getRelationForeignKeys ( ) : array
Результат array foreign key field names as key and object properties as value.

setObjectProperty() защищенный Метод

Sets the foreign objects to the given property on the source object.
protected setObjectProperty ( $source, $properties, &$collections )

updateAssociatedRecords() публичный Метод

Updates the associated foreign objects.
public updateAssociatedRecords ( ) : boolean
Результат boolean true if all update are success (including if no update was required), false otherwise .