foreach ($this->_relationsToDelete as $modelToDelete) {
try {
if (!$modelToDelete->delete()) {
throw new DbException('Could not delete the related record: ' . $modelToDelete::className() . '(' . VarDumper::dumpAsString($modelToDelete->primaryKey) . ')');
}
} catch (Exception $e) {
Yii::warning(get_class($e) . ' was thrown while deleting related records during afterDelete event: ' . $e->getMessage(), __METHOD__);
$this->_rollback();
throw $e;
}
}
}
/**
* Return array of columns to save to the junction table for a related model having a many-to-many relation.
* @param string $relationName
* @param BaseActiveRecord $model
* @return array
* @throws \RuntimeException
*/
private function _getJunctionTableColumns($relationName, $model)
{
$junctionTableColumns = [];
if (array_key_exists($relationName, $this->_relationsExtraColumns)) {
if (is_callable($this->_relationsExtraColumns[$relationName])) {
* Load existing model or create one if no key was provided and data is not empty
* Delete related models marked as to be deleted
* @param $data
* @throws Exception
* @param $fks
* @param $modelClass
* @return BaseActiveRecord
*/
*/
private function _loadOrCreateRelationModel($data, $fks, $modelClass)
public function afterDelete()
{
{
/** @var BaseActiveRecord $relationModel */
/** @var BaseActiveRecord $modelToDelete */
$relationModel = null;
foreach ($this->_relationsToDelete as $modelToDelete) {
if (!empty($fks)) {
try {
$relationModel = $modelClass::findOne($fks);
if (!$modelToDelete->delete()) {
throw new DbException('Could not delete the related record: ' . $modelToDelete::className() . '(' . VarDumper::dumpAsString($modelToDelete->primaryKey) . ')');
}
}
if (!($relationModel instanceof BaseActiveRecord) && !empty($data)) {
} catch (Exception $e) {
$relationModel = new $modelClass;
Yii::warning(get_class($e) . ' was thrown while deleting related records during afterDelete event: ' . $e->getMessage(), __METHOD__);
$this->_rollback();
throw $e;
}
}
if (($relationModel instanceof BaseActiveRecord) && is_array($data)) {
$relationModel->setAttributes($data);
}
}
return $relationModel;
}
}
/**
/**
* Get the related model foreign keys
* Populates relations with input data
* @param $data
* @param array $data
* @param $relation
* @param BaseActiveRecord $modelClass
* @return array
*/
*/
private function _getRelatedFks($data, $relation, $modelClass)
public function loadRelations($data)
{
{
$fks = [];
/** @var BaseActiveRecord $model */
if (is_array($data)) {
$model = $this->owner;
// search PK
foreach ($this->_relations as $relationName) {
foreach ($modelClass::primaryKey() as $modelAttribute) {
/** @var ActiveQuery $relation */
if (array_key_exists($modelAttribute, $data) && !empty($data[$modelAttribute])) {
$relation = $model->getRelation($relationName);
$fks[$modelAttribute] = $data[$modelAttribute];
$modelClass = $relation->modelClass;
} else {
/** @var ActiveQuery $relationalModel */
$fks = [];
$relationalModel = new $modelClass;
break;
$formName = $relationalModel->formName();
}
if (array_key_exists($formName, $data)) {
}
$model->{$relationName} = $data[$formName];
if (empty($fks)) {
// Get the right link definition
if ($relation->via instanceof BaseActiveRecord) {
$link = $relation->via->link;
} elseif (is_array($relation->via)) {
list($viaName, $viaQuery) = $relation->via;
$link = $viaQuery->link;
} else {
$link = $relation->link;
}
foreach ($link as $relatedAttribute => $modelAttribute) {
if (array_key_exists($modelAttribute, $data) && !empty($data[$modelAttribute])) {
public function testSaveHasOneReplaceRelatedWithNewRecord()
{
$profile = UserProfile::findOne(1);
$this->assertEquals('Steven Paul Jobs (February 24, 1955 – October 5, 2011) was an American entrepreneur, business magnate, inventor, and industrial designer. He was the chairman, chief executive officer (CEO), and co-founder of Apple Inc.; CEO and majority shareholder of Pixar; a member of The Walt Disney Company\'s board of directors following its acquisition of Pixar; and the founder, chairman, and CEO of NeXT.', $profile->bio, "Profile bio is wrong");
$data = [
'User' => [
'username' => 'Someone Else',
'company_id' => 1
]
];
$profile->loadRelations($data);
$this->assertEquals('Someone Else', $profile->user->username, "User name should be 'Someone Else'");
$this->assertTrue($profile->user->isNewRecord, "User should be a new record");
$profile->save();
$this->assertTrue($profile->save(), 'Profile could not be saved');
$this->assertEquals('Someone Else', $profile->user->username, "User name should be 'Someone Else'");