Skip to content
This repository has been archived by the owner on Jul 1, 2022. It is now read-only.

Commit

Permalink
Fix for issue #22: another attempt based on @mythicallage suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
Alban Jubert committed Nov 3, 2017
1 parent a34b436 commit f9d4b64
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/SaveRelationsBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ public function beforeValidate(ModelEvent $event)
if ($relation->multiple === false && !empty($model->{$relationName})) {
Yii::trace("Setting foreign keys for {$relationName}", __METHOD__);
foreach ($relation->link as $relatedAttribute => $modelAttribute) {
if ($model->{$modelAttribute} !== $model->{$relationName}->{$relatedAttribute} && !in_array($modelAttribute, $model->primaryKey())) {
if ($model->{$modelAttribute} !== $model->{$relationName}->{$relatedAttribute}) {
$model->{$modelAttribute} = $model->{$relationName}->{$relatedAttribute};
}
}
Expand All @@ -252,15 +252,18 @@ public function beforeValidate(ModelEvent $event)
/**
* For each related model, try to save it first.
* If set in the owner model, operation is done in a transactional way so if one of the models should not validate
* or be saved, a rollback will occur.
* or be saved, a rollback will occur.,
* This is done during the before validation process to be able to set the related foreign keys.
* @param BaseActiveRecord $model
* @param ModelEvent $event
* @return bool
*/
protected function saveRelatedRecords(BaseActiveRecord $model, ModelEvent $event)
{
if (($model->isNewRecord && $model->isTransactional($model::OP_INSERT)) || (!$model->isNewRecord && $model->isTransactional($model::OP_UPDATE)) || $model->isTransactional($model::OP_ALL)) {
if (($model->isNewRecord && $model->isTransactional($model::OP_INSERT))
|| (!$model->isNewRecord && $model->isTransactional($model::OP_UPDATE))
|| $model->isTransactional($model::OP_ALL)
) {
$this->_transaction = $model->getDb()->beginTransaction();
}
try {
Expand All @@ -269,9 +272,16 @@ protected function saveRelatedRecords(BaseActiveRecord $model, ModelEvent $event
$relation = $model->getRelation($relationName);
if (!empty($model->{$relationName})) {
if ($relation->multiple === false) {
// Save Has one relation new record
$pettyRelationName = Inflector::camel2words($relationName, true);
$this->saveModelRecord($model->{$relationName}, $event, $pettyRelationName, $relationName);
$relationModel = $model->{$relationName};
if (!($model::isPrimaryKey(array_values($relation->link))
&& $relationModel::isPrimaryKey(array_keys($relation->link))
&& $model->getIsNewRecord()
&& $relationModel->getIsNewRecord()
)) {
// Save Has one relation new record
$pettyRelationName = Inflector::camel2words($relationName, true);
$this->saveModelRecord($model->{$relationName}, $event, $pettyRelationName, $relationName);
}
} else {
// Save Has many relations new records
/** @var BaseActiveRecord $relationModel */
Expand Down

0 comments on commit f9d4b64

Please sign in to comment.