public function testViaOrder()
 {
     $firstProject = new Project();
     $firstProject->name = 'Building';
     $firstProject->save();
     $secondProject = new Project();
     $secondProject->name = 'Logistic';
     $secondProject->save();
     $firstWorker = new Worker();
     $firstWorker->name = 'Mark';
     $firstWorker->save();
     $secondWorker = new Worker();
     $secondWorker->name = 'Alex';
     $secondWorker->save();
     ProjectMembership::objects()->getOrCreate(['project' => $firstProject, 'worker' => $firstWorker, 'position' => 1, 'curator' => $secondWorker]);
     ProjectMembership::objects()->getOrCreate(['project' => $firstProject, 'worker' => $secondWorker, 'position' => 2, 'curator' => $firstWorker]);
     $this->assertEquals([['id' => '1', 'project_id' => '1', 'worker_id' => '1', 'position' => '1', 'curator_id' => '2'], ['id' => '2', 'project_id' => '1', 'worker_id' => '2', 'position' => '2', 'curator_id' => '1']], ProjectMembership::objects()->asArray()->all());
     $this->assertEquals([['id' => '1', 'name' => 'Mark'], ['id' => '2', 'name' => 'Alex']], Worker::objects()->filter(['projects__id__in' => [$firstProject->id]])->order(['projects_through__position'])->asArray()->all());
     $this->assertEquals([['id' => '2', 'name' => 'Alex'], ['id' => '1', 'name' => 'Mark']], Worker::objects()->filter(['projects__id__in' => [$firstProject->id]])->order(['-projects_through__position'])->asArray()->all());
     $this->assertEquals([['id' => '1', 'name' => 'Mark'], ['id' => '2', 'name' => 'Alex']], Worker::objects()->order(['projects_through__position'])->asArray()->all());
     $this->assertEquals([['id' => '2', 'name' => 'Alex'], ['id' => '1', 'name' => 'Mark']], Worker::objects()->order(['-projects_through__position'])->asArray()->all());
     $this->assertEquals([['id' => '2', 'name' => 'Alex']], Worker::objects()->filter(['projects_through__curator' => $firstWorker])->asArray()->all());
     $this->assertEquals([['id' => '1', 'name' => 'Mark']], Worker::objects()->filter(['projects_through__curator' => $secondWorker])->asArray()->all());
     $this->assertEquals("SELECT `tests_worker_1`.* FROM `tests_worker` `tests_worker_1` LEFT OUTER JOIN `tests_project_membership` `tests_project_membership_2` ON `tests_worker_1`.`id` = `tests_project_membership_2`.`worker_id` LEFT OUTER JOIN `tests_project` `tests_project_3` ON `tests_project_membership_2`.`project_id` = `tests_project_3`.`id` WHERE (`tests_project_membership_2`.`curator_id`='2') GROUP BY `tests_worker_1`.`id`", Worker::objects()->filter(['projects_through__curator' => $secondWorker])->allSql());
     $this->assertEquals("SELECT `tests_worker_1`.* FROM `tests_worker` `tests_worker_1` LEFT OUTER JOIN `tests_project_membership` `tests_project_membership_2` ON `tests_worker_1`.`id` = `tests_project_membership_2`.`worker_id` LEFT OUTER JOIN `tests_project` `tests_project_3` ON `tests_project_membership_2`.`project_id` = `tests_project_3`.`id` GROUP BY `tests_worker_1`.`id` ORDER BY `tests_project_membership_2`.`position`", Worker::objects()->order(['projects_through__position'])->asArray()->allSql());
     $this->assertEquals("SELECT `tests_worker_1`.* FROM `tests_worker` `tests_worker_1` LEFT OUTER JOIN `tests_project_membership` `tests_project_membership_2` ON `tests_worker_1`.`id` = `tests_project_membership_2`.`worker_id` LEFT OUTER JOIN `tests_project` `tests_project_3` ON `tests_project_membership_2`.`project_id` = `tests_project_3`.`id` WHERE (`tests_project_3`.`id` IN ('1', '2')) GROUP BY `tests_worker_1`.`id` ORDER BY `tests_project_membership_2`.`position` DESC", Worker::objects()->filter(['projects__id__in' => [$firstProject->id, $secondProject->id]])->order(['-projects_through__position'])->allSql());
 }
 public static function getFields()
 {
     return ['project' => ['class' => ForeignField::className(), 'modelClass' => Project::className()], 'worker' => ['class' => ForeignField::className(), 'modelClass' => Worker::className()], 'position' => ['class' => IntField::className()], 'curator' => ['class' => ForeignField::className(), 'modelClass' => Worker::className()]];
 }
예제 #3
0
 public static function getFields()
 {
     return ['name' => ['class' => CharField::className()], 'projects' => ['class' => ManyToManyField::className(), 'modelClass' => Project::className(), 'through' => ProjectMembership::className()]];
 }
 public function testLoadRelationsShouldSucceed()
 {
     $project = Project::findOne(1);
     $data = ['Company' => ['name' => 'YiiSoft'], 'ProjectLink' => [['language' => 'en', 'name' => 'yii', 'link' => 'http://www.yiiframework.com'], ['language' => 'fr', 'name' => 'yii', 'link' => 'http://www.yiiframework.fr']]];
     $project->loadRelations($data);
     $this->assertTrue($project->save(), 'Project could not be saved');
     $this->assertEquals('YiiSoft', $project->company->name, "Company name should be YiiSoft");
     $this->assertCount(2, $project->projectLinks, "Project should have 2 links");
 }