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

Commit

Permalink
Merge d2474cd into ee2e08a
Browse files Browse the repository at this point in the history
  • Loading branch information
leandrogehlen committed Aug 30, 2018
2 parents ee2e08a + d2474cd commit ea5ed20
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/SaveRelationsBehavior.php
Expand Up @@ -241,7 +241,7 @@ private function _getRelatedFks($data, $relation, $modelClass)
$fks[$modelAttribute] = $data[$modelAttribute];
} elseif ($relation->multiple && !$relation->via) {
foreach ($link as $relatedAttribute => $relatedModelAttribute) {
if (!isset($data[$relatedAttribute])) {
if (!isset($data[$relatedAttribute]) && in_array($relatedAttribute, $modelClass::primaryKey())) {
$fks[$relatedAttribute] = $this->owner->{$relatedModelAttribute};
}
}
Expand Down
40 changes: 38 additions & 2 deletions tests/SaveRelationsBehaviorTest.php
Expand Up @@ -41,6 +41,7 @@ protected function tearDown()
$db->createCommand()->dropTable('tags')->execute();
$db->createCommand()->dropTable('project_link')->execute();
$db->createCommand()->dropTable('project_contact')->execute();
$db->createCommand()->dropTable('project_image')->execute();
$db->createCommand()->dropTable('dummy')->execute();
parent::tearDown();
}
Expand Down Expand Up @@ -124,11 +125,18 @@ protected function setupDbData()
// Project Contact
$db->createCommand()->createTable('project_contact', [
'project_id' => $migration->integer()->notNull(),
'email' => $migration->text()->notNull(),
'phone' => $migration->text(),
'email' => $migration->string()->notNull(),
'phone' => $migration->string(),
'PRIMARY KEY(project_id, email)'
])->execute();

// Project Image
$db->createCommand()->createTable('project_image', [
'id' => $migration->primaryKey(),
'project_id' => $migration->integer()->notNull(),
'path' => $migration->string()->notNull()
])->execute();

// Dummy
$db->createCommand()->createTable('dummy', [
'id' => $migration->primaryKey(),
Expand Down Expand Up @@ -183,6 +191,12 @@ protected function setupDbData()
['admin@apple.com', '(123) 456–7890', 1]
])->execute();

$db->createCommand()->batchInsert('project_image', ['id', 'project_id', 'path'], [
[1, 1, '/images/macosx.png'],
[2, 1, '/images/macosx_icon.png'],
[3, 2, '/images/windows.png']
])->execute();

$db->createCommand()->batchInsert('project_user', ['project_id', 'user_id'], [
[1, 1],
[1, 4],
Expand Down Expand Up @@ -538,6 +552,28 @@ public function testLoadHasManyWithCompositeKeyShouldSucceed()
$this->assertEquals($project->contacts[1]->phone, '(987) 654–3210');
}

public function testLoadHasManyWithoutReferenceKeyShouldSucceed()
{
$project = Project::findOne(1);
$data = [
'ProjectImage' => [
[
'path' => '/images/macosx_new.png'
],
[
'id' => 2,
'path' => '/images/macosx_updated.png'
]
]
];
$project->loadRelations($data);
$this->assertTrue($project->save(), 'Project could not be saved');
$this->assertCount(2, $project->images, "Project should have 2 images");
$this->assertEquals($project->images[0]->id, 2);
$this->assertEquals($project->images[0]->path, '/images/macosx_updated.png');
$this->assertEquals($project->images[1]->path, '/images/macosx_new.png');
}

public function testAssignSingleObjectToHasManyRelationShouldSucceed()
{
$project = new Project();
Expand Down
9 changes: 9 additions & 0 deletions tests/models/Project.php
Expand Up @@ -26,6 +26,7 @@ public function behaviors()
'company',
'users',
'contacts',
'images',
'links' => ['scenario' => Link::SCENARIO_FIRST],
'projectLinks' => ['cascadeDelete' => true],
'tags' => [
Expand Down Expand Up @@ -104,6 +105,14 @@ public function getContacts()
return $this->hasMany(ProjectContact::className(), ['project_id' => 'id']);
}

/**
* @return \yii\db\ActiveQuery
*/
public function getImages()
{
return $this->hasMany(ProjectImage::className(), ['project_id' => 'id']);
}

/**
* @return \yii\db\ActiveQuery
*/
Expand Down
29 changes: 29 additions & 0 deletions tests/models/ProjectImage.php
@@ -0,0 +1,29 @@
<?php

namespace tests\models;

class ProjectImage extends \yii\db\ActiveRecord
{
/** @var int */
protected $order;

/**
* @inheritdoc
*/
public static function tableName()
{
return 'project_image';
}

/**
* @inheritdoc
*/
public function rules()
{
return [
[['project_id'], 'integer'],
[['path'], 'required'],
[['path'], 'string']
];
}
}

0 comments on commit ea5ed20

Please sign in to comment.