Browse Source

Fix for issue #22: another attempt based on @mythicallage suggestion

tags/1.3.2
Alban Jubert 7 years ago
parent
commit
f9d4b6410b
  1. 16
      src/SaveRelationsBehavior.php

16
src/SaveRelationsBehavior.php

@ -238,7 +238,7 @@ class SaveRelationsBehavior extends Behavior
if ($relation->multiple === false && !empty($model->{$relationName})) { if ($relation->multiple === false && !empty($model->{$relationName})) {
Yii::trace("Setting foreign keys for {$relationName}", __METHOD__); Yii::trace("Setting foreign keys for {$relationName}", __METHOD__);
foreach ($relation->link as $relatedAttribute => $modelAttribute) { 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}; $model->{$modelAttribute} = $model->{$relationName}->{$relatedAttribute};
} }
} }
@ -252,7 +252,7 @@ class SaveRelationsBehavior extends Behavior
/** /**
* For each related model, try to save it first. * 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 * 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. * This is done during the before validation process to be able to set the related foreign keys.
* @param BaseActiveRecord $model * @param BaseActiveRecord $model
* @param ModelEvent $event * @param ModelEvent $event
@ -260,7 +260,10 @@ class SaveRelationsBehavior extends Behavior
*/ */
protected function saveRelatedRecords(BaseActiveRecord $model, ModelEvent $event) 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(); $this->_transaction = $model->getDb()->beginTransaction();
} }
try { try {
@ -269,9 +272,16 @@ class SaveRelationsBehavior extends Behavior
$relation = $model->getRelation($relationName); $relation = $model->getRelation($relationName);
if (!empty($model->{$relationName})) { if (!empty($model->{$relationName})) {
if ($relation->multiple === false) { if ($relation->multiple === false) {
$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 // Save Has one relation new record
$pettyRelationName = Inflector::camel2words($relationName, true); $pettyRelationName = Inflector::camel2words($relationName, true);
$this->saveModelRecord($model->{$relationName}, $event, $pettyRelationName, $relationName); $this->saveModelRecord($model->{$relationName}, $event, $pettyRelationName, $relationName);
}
} else { } else {
// Save Has many relations new records // Save Has many relations new records
/** @var BaseActiveRecord $relationModel */ /** @var BaseActiveRecord $relationModel */

Loading…
Cancel
Save