fromResultset() public static method

Create and get from a Resultset
public static fromResultset ( Phalcon\Mvc\Model\Resultset\Simple $subject ) : Phalcon\Mvc\Model\Resultset\Simple
$subject Phalcon\Mvc\Model\Resultset\Simple
return Phalcon\Mvc\Model\Resultset\Simple
Beispiel #1
0
 public function testBelongsTo()
 {
     $rawly = Bug::findFirstById(1);
     $rawly->robot;
     $eagerly = Loader::fromModel(Bug::findFirstById(1), 'Robot');
     $this->assertTrue(property_exists($eagerly, 'robot'));
     $this->assertInstanceOf('EagerLoadingTestModel\\Robot', $eagerly->robot);
     $this->assertEquals($rawly->robot->readAttribute('id'), $eagerly->robot->readAttribute('id'));
     // Reverse
     $rawly = Robot::findFirstById(2);
     $rawly->bugs = $this->resultSetToEagerLoadingEquivalent($rawly->bugs);
     $eagerly = Loader::fromModel(Robot::findFirstById(2), 'Bugs');
     $this->assertTrue(property_exists($eagerly, 'bugs'));
     $this->assertContainsOnlyInstancesOf('EagerLoadingTestModel\\Bug', $eagerly->bugs);
     $getIds = function ($obj) {
         return $obj->readAttribute('id');
     };
     $this->assertEquals(array_map($getIds, $rawly->bugs), array_map($getIds, $eagerly->bugs));
     $this->assertEmpty(Loader::fromModel(Robot::findFirstById(1), 'Bugs')->bugs);
     // Test from multiple
     $rawly = $this->resultSetToEagerLoadingEquivalent(Bug::find(array('limit' => 10)));
     foreach ($rawly as $bug) {
         $bug->robot;
     }
     $eagerly = Loader::fromResultset(Bug::find(array('limit' => 10)), 'Robot');
     $this->assertTrue(is_array($eagerly));
     $this->assertTrue(array_reduce($eagerly, function ($res, $bug) {
         return $res && property_exists($bug, 'robot');
     }, true));
     $getIds = function ($obj) {
         return property_exists($obj, 'robot') && isset($obj->robot) ? $obj->robot->readAttribute('id') : null;
     };
     $this->assertEquals(array_map($getIds, $rawly), array_map($getIds, $eagerly));
 }