public getRelationHandler ( $criteria = null ) : |
||
return | record relationship handler instnace. |
public function getRelationHandler($criteria = null)
{
if (!$this->hasRecordRelation()) {
throw new TActiveRecordException('ar_undefined_relation_prop', $this->_property, get_class($this->_record), 'RELATIONS');
}
if ($criteria === null) {
$criteria = new TActiveRecordCriteria($this->getCondition(), $this->getParameters());
}
switch ($this->getRelationType()) {
case TActiveRecord::HAS_MANY:
return new TActiveRecordHasMany($this, $criteria);
case TActiveRecord::MANY_TO_MANY:
return new TActiveRecordHasManyAssociation($this, $criteria);
case TActiveRecord::HAS_ONE:
return new TActiveRecordHasOne($this, $criteria);
case TActiveRecord::BELONGS_TO:
return new TActiveRecordBelongsTo($this, $criteria);
default:
throw new TActiveRecordException('ar_invalid_relationship');
}
}
/** * @return TActiveRecordRelationCommand */ public function updateAssociatedRecords($updateBelongsTo = false) { $success = true; foreach ($this->_record->getRecordRelations() as $data) { list($property, $relation) = $data; $belongsTo = $relation[0] == TActiveRecord::BELONGS_TO; if ($updateBelongsTo && $belongsTo || !$updateBelongsTo && !$belongsTo) { $obj = $this->getSourceRecord(); if (!$this->isEmptyFkObject($obj->getColumnValue($property))) { $context = new TActiveRecordRelationContext($this->getSourceRecord(), $property, $relation); $success = $context->getRelationHandler()->updateAssociatedRecords() && $success; } } } return $success; }