Browse Source

Better support for hasOne relations saving (#22 and #23)

tags/1.4.0
Alban Jubert 7 years ago
parent
commit
b74af4153d
  1. 8
      src/SaveRelationsBehavior.php
  2. 29
      tests/SaveRelationsBehaviorTest.php
  3. 23
      tests/models/Company.php
  4. 12
      tests/models/User.php

8
src/SaveRelationsBehavior.php

@ -279,11 +279,9 @@ class SaveRelationsBehavior extends Behavior
if (!empty($model->{$relationName})) {
if ($relation->multiple === false) {
$relationModel = $model->{$relationName};
if (!($model::isPrimaryKey(array_values($relation->link))
&& $relationModel::isPrimaryKey(array_keys($relation->link))
&& $model->getIsNewRecord()
&& $relationModel->getIsNewRecord()
)) {
$p1 = $model->isPrimaryKey(array_keys($relation->link));
$p2 = $relationModel::isPrimaryKey(array_values($relation->link));
if ($relationModel->getIsNewRecord() && $p1 && !$p2) {
// Save Has one relation new record
$pettyRelationName = Inflector::camel2words($relationName, true);
$this->saveModelRecord($model->{$relationName}, $event, $pettyRelationName, $relationName);

29
tests/SaveRelationsBehaviorTest.php

@ -61,6 +61,7 @@ class SaveRelationsBehaviorTest extends \PHPUnit_Framework_TestCase
// User
$db->createCommand()->createTable('user', [
'id' => $migration->primaryKey(),
'company_id' => $migration->integer()->notNull(),
'username' => $migration->string()->notNull()->unique()
])->execute();
@ -133,11 +134,11 @@ class SaveRelationsBehaviorTest extends \PHPUnit_Framework_TestCase
[3, 'Google'],
])->execute();
$db->createCommand()->batchInsert('user', ['id', 'username'], [
[1, 'Steve Jobs'],
[2, 'Bill Gates'],
[3, 'Tim Cook'],
[4, 'Jonathan Ive']
$db->createCommand()->batchInsert('user', ['id', 'username', 'company_id'], [
[1, 'Steve Jobs', 1],
[2, 'Bill Gates', 2],
[3, 'Tim Cook', 1],
[4, 'Jonathan Ive', 1]
])->execute();
$db->createCommand()->batchInsert('project', ['id', 'name', 'company_id'], [
@ -312,6 +313,7 @@ class SaveRelationsBehaviorTest extends \PHPUnit_Framework_TestCase
$this->assertCount(1, $project->users, 'Project should have 1 user before save');
$user = new User();
$user->username = "Steve Balmer";
$user->company_id = 2;
$project->users = array_merge($project->users, [$user]); // Add a fresh new user
$this->assertCount(2, $project->users, 'Project should have 2 users after assignment');
$this->assertTrue($project->save(), 'Project could not be saved');
@ -322,7 +324,7 @@ class SaveRelationsBehaviorTest extends \PHPUnit_Framework_TestCase
{
$project = Project::findOne(2);
$this->assertCount(1, $project->users, 'Project should have 1 user before save');
$user = ['username' => "Steve Balmer"];
$user = ['username' => "Steve Balmer", 'company_id' => 2];
$project->users = array_merge($project->users, [$user]); // Add a fresh new user
$this->assertCount(2, $project->users, 'Project should have 2 users after assignment');
$this->assertTrue($project->save(), 'Project could not be saved');
@ -643,6 +645,9 @@ class SaveRelationsBehaviorTest extends \PHPUnit_Framework_TestCase
{
$user = new User();
$user->username = 'Dummy More';
$user->company = [
'name' => 'ACME'
];
$user->userProfile = [
'bio' => "Some great bio"
];
@ -651,4 +656,16 @@ class SaveRelationsBehaviorTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($user->id, $user->userProfile->user_id);
}
public function testSaveCompanyWithUser()
{
$project = new Project();
$project->name = "Cartoon";
$company = new Company();
$company->name = 'ACME';
$user = new User();
$user->username = "Bugs Bunny";
$company->users = $user;
$project->company = $company;
$this->assertTrue($project->save(), 'Project could not be saved');
}
}

23
tests/models/Company.php

@ -2,6 +2,8 @@
namespace tests\models;
use lhs\Yii2SaveRelationsBehavior\SaveRelationsBehavior;
class Company extends \yii\db\ActiveRecord
{
@ -16,6 +18,19 @@ class Company extends \yii\db\ActiveRecord
/**
* @inheritdoc
*/
public function behaviors()
{
return [
'saveRelations' => [
'class' => SaveRelationsBehavior::className(),
'relations' => ['users']
],
];
}
/**
* @inheritdoc
*/
public function rules()
{
return [
@ -24,4 +39,12 @@ class Company extends \yii\db\ActiveRecord
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getUsers()
{
return $this->hasMany(User::className(), ['company_id' => 'id']);
}
}

12
tests/models/User.php

@ -22,7 +22,7 @@ class User extends \yii\db\ActiveRecord
return [
'saveRelations' => [
'class' => SaveRelationsBehavior::className(),
'relations' => ['userProfile']
'relations' => ['userProfile', 'company']
],
];
}
@ -33,8 +33,10 @@ class User extends \yii\db\ActiveRecord
public function rules()
{
return [
['company_id', 'integer'],
['username', 'required'],
['username', 'unique', 'targetClass' => '\tests\models\User'],
[['company_id'], 'exist', 'skipOnError' => true, 'targetClass' => Company::className(), 'targetAttribute' => ['company_id' => 'id']],
];
}
@ -46,4 +48,12 @@ class User extends \yii\db\ActiveRecord
return $this->hasOne(UserProfile::className(), ['user_id' => 'id']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getCompany()
{
return $this->hasOne(Company::className(), ['id' => 'company_id']);
}
}

Loading…
Cancel
Save