예제 #1
0
 public function testSerialize()
 {
     $this->_prepareDI();
     $robot = Robots::findFirst();
     $serialized = serialize($robot);
     $robot = unserialize($serialized);
     $this->assertTrue($robot->save());
 }
예제 #2
0
 public function testSerialize()
 {
     require 'unit-tests/config.db.php';
     if (empty($configMysql)) {
         $this->markTestSkipped('Test skipped');
         return;
     }
     $this->_prepareDI();
     $robot = Robots::findFirst();
     $serialized = serialize($robot);
     $robot = unserialize($serialized);
     $this->assertTrue($robot->save());
 }
 public static function update($id, $data)
 {
     $response = new Response();
     try {
         $data = json_decode($data, true);
         /** @var Robots $robot */
         $robot = Robots::findFirst($id);
         $robot->setAttributes($data);
         $response->data = $robot->update();
     } catch (Exception $e) {
         $response->setException($e);
     } finally {
         return $response->toArray();
     }
 }
예제 #4
0
 public function testJsonSerialize()
 {
     require 'unit-tests/config.db.php';
     if (empty($configMysql)) {
         $this->markTestSkipped('Test skipped');
         return;
     }
     $di = $this->_getDI();
     // Single model object json serialization
     $robot = Robots::findFirst();
     $json = json_encode($robot);
     $this->assertTrue(is_string($json));
     $this->assertTrue(strlen($json) > 10);
     // make sure result is not "{ }"
     $array = json_decode($json, true);
     $this->assertEquals($robot->toArray(), $array);
     // Result-set serialization
     $robots = Robots::find();
     $json = json_encode($robots);
     $this->assertTrue(is_string($json));
     $this->assertTrue(strlen($json) > 50);
     // make sure result is not "{ }"
     $array = json_decode($json, true);
     $this->assertEquals($robots->toArray(), $array);
     // Single row serialization
     $result = $di->get('modelsManager')->executeQuery('SELECT id FROM Robots LIMIT 1');
     $this->assertEquals(get_class($result), 'Phalcon\\Mvc\\Model\\Resultset\\Simple');
     foreach ($result as $row) {
         $this->assertEquals(get_class($row), 'Phalcon\\Mvc\\Model\\Row');
         $this->assertEquals($row->id, $robot->id);
         $json = json_encode($row);
         $this->assertTrue(is_string($json));
         $this->assertTrue(strlen($json) > 5);
         // make sure result is not "{ }"
         $array = json_decode($json, true);
         $this->assertEquals($row->toArray(), $array);
     }
 }
<?php

//What's the first robot in robots table?
$robot = Robots::findFirst();
echo "The robot name is ", $robot->name;
//What's the first mechanical robot in robots table?
$robot = Robots::findFirst("type='mechanical'");
echo "The first mechanical robot name is ", $robot->name;
//Get first virtual robot ordered by name
$robot = Robots::findFirst(array("type='virtual'", "order" => "name"));
echo "The first virtual robot name is ", $robot->name;
예제 #6
0
 public function _executeTestsNormal($di)
 {
     //Normal foreign keys
     $robotsParts = new RobotsParts();
     $robotsParts->robots_id = 1;
     $robotsParts->parts_id = 100;
     $this->assertFalse($robotsParts->save());
     $messages = array(0 => Phalcon\Mvc\Model\Message::__set_state(array('_type' => 'ConstraintViolation', '_message' => 'Value of field "parts_id" does not exist on referenced table', '_field' => 'parts_id')));
     $this->assertEquals($robotsParts->getMessages(), $messages);
     $robotsParts->robots_id = 100;
     $robotsParts->parts_id = 1;
     $this->assertFalse($robotsParts->save());
     $messages = array(0 => Phalcon\Mvc\Model\Message::__set_state(array('_type' => 'ConstraintViolation', '_message' => 'The robot code does not exist', '_field' => 'robots_id')));
     $this->assertEquals($robotsParts->getMessages(), $messages);
     //Reverse foreign keys
     $robot = Robots::findFirst();
     $this->assertNotEquals($robot, false);
     $this->assertFalse($robot->delete());
     $messages = array(0 => Phalcon\Mvc\Model\Message::__set_state(array('_type' => 'ConstraintViolation', '_message' => 'Record is referenced by model RobotsParts', '_field' => 'id')));
     $this->assertEquals($robot->getMessages(), $messages);
     $part = Parts::findFirst();
     $this->assertNotEquals($part, false);
     $this->assertFalse($part->delete());
     $messages = array(0 => Phalcon\Mvc\Model\Message::__set_state(array('_type' => 'ConstraintViolation', '_message' => 'Parts cannot be deleted because is referenced by a Robot', '_field' => 'id')));
     $this->assertEquals($part->getMessages(), $messages);
 }
<?php

$robot = Robots::findFirst("id=100");
$robot->delete();
foreach (Robots::find("type = 'mechanical'") as $robot) {
    $robot->delete();
}
예제 #8
0
<?php

// What's the first robot in robots collection?
$robot = Robots::findFirst();
echo "The robot name is ", $robot->name, "\n";
// What's the first mechanical robot in robots collection?
$robot = Robots::findFirst(array(array("type" => "mechanical")));
echo "The first mechanical robot name is ", $robot->name, "\n";
예제 #9
0
 public function queryAction()
 {
     $robot = Robots::findFirst();
     $this->view->setVar("name", $robot->name);
 }
예제 #10
0
 protected function issue1801($di)
 {
     Phalcon\Mvc\Model::setup(array('columnRenaming' => false));
     $robot = Robots::findFirst(1);
     $di->get('modelsMetadata')->reset();
     $robot = Robots::findFirst(1);
     $this->assertTrue(true);
 }
예제 #11
0
 public function testMetadataRedis()
 {
     require __DIR__ . '/config.db.php';
     if (empty($configMysql)) {
         $this->markTestSkipped('Test skipped');
         return;
     }
     if (!extension_loaded('redis')) {
         $this->markTestSkipped('Warning: redis extension is not loaded');
         return;
     }
     $di = $this->_getDI();
     $di->set('modelsMetadata', function () {
         return new Phalcon\Mvc\Model\Metadata\Redis(array("host" => "localhost", "port" => "6379"));
     });
     $metaData = $di->getShared('modelsMetadata');
     $metaData->reset();
     $this->assertTrue($metaData->isEmpty());
     Robots::findFirst();
     $this->assertEquals($metaData->read("meta-robots-robots"), $this->_data['meta-robots-robots']);
     $this->assertEquals($metaData->read("map-robots"), $this->_data['map-robots']);
     $this->assertFalse($metaData->isEmpty());
     $metaData->reset();
     $this->assertTrue($metaData->isEmpty());
     Robots::findFirst();
 }
예제 #12
0
 protected function _executeTestsNormal($di)
 {
     $this->_prepareDb($di->getShared('db'));
     //Count tests
     $this->assertEquals(People::count(), Personas::count());
     $params = array();
     $this->assertEquals(People::count($params), Personas::count($params));
     $params = array("estado='I'");
     $this->assertEquals(People::count($params), Personas::count($params));
     $params = "estado='I'";
     $this->assertEquals(People::count($params), Personas::count($params));
     $params = array("conditions" => "estado='I'");
     $this->assertEquals(People::count($params), Personas::count($params));
     //Find first
     $people = People::findFirst();
     $this->assertTrue(is_object($people));
     $this->assertEquals(get_class($people), 'People');
     $persona = Personas::findFirst();
     $this->assertEquals($people->nombres, $persona->nombres);
     $this->assertEquals($people->estado, $persona->estado);
     $people = People::findFirst("estado='I'");
     $this->assertTrue(is_object($people));
     $persona = Personas::findFirst("estado='I'");
     $this->assertTrue(is_object($persona));
     $this->assertEquals($people->nombres, $persona->nombres);
     $this->assertEquals($people->estado, $persona->estado);
     $people = People::findFirst(array("estado='I'"));
     $persona = Personas::findFirst(array("estado='I'"));
     $this->assertEquals($people->nombres, $persona->nombres);
     $this->assertEquals($people->estado, $persona->estado);
     $params = array("conditions" => "estado='I'");
     $people = People::findFirst($params);
     $persona = Personas::findFirst($params);
     $this->assertEquals($people->nombres, $persona->nombres);
     $this->assertEquals($people->estado, $persona->estado);
     $params = array("conditions" => "estado='A'", "order" => "nombres");
     $people = People::findFirst($params);
     $persona = Personas::findFirst($params);
     $this->assertEquals($people->nombres, $persona->nombres);
     $this->assertEquals($people->estado, $persona->estado);
     $params = array("estado='A'", "order" => "nombres DESC", "limit" => 30);
     $people = People::findFirst($params);
     $persona = Personas::findFirst($params);
     $this->assertEquals($people->nombres, $persona->nombres);
     $this->assertEquals($people->estado, $persona->estado);
     $params = array("estado=?1", "bind" => array(1 => 'A'), "order" => "nombres DESC", "limit" => 30);
     $people = People::findFirst($params);
     $persona = Personas::findFirst($params);
     $this->assertEquals($people->nombres, $persona->nombres);
     $this->assertEquals($people->estado, $persona->estado);
     $params = array("estado=:estado:", "bind" => array("estado" => 'A'), "order" => "nombres DESC", "limit" => 30);
     $people = People::findFirst($params);
     $persona = Personas::findFirst($params);
     $this->assertEquals($people->nombres, $persona->nombres);
     $this->assertEquals($people->estado, $persona->estado);
     $robot = Robots::findFirst(1);
     $this->assertEquals(get_class($robot), 'Robots');
     //Find tests
     $personas = Personas::find();
     $people = People::find();
     $this->assertEquals(count($personas), count($people));
     $personas = Personas::find("estado='I'");
     $people = People::find("estado='I'");
     $this->assertEquals(count($personas), count($people));
     $personas = Personas::find(array("estado='I'"));
     $people = People::find(array("estado='I'"));
     $this->assertEquals(count($personas), count($people));
     $personas = Personas::find(array("estado='A'", "order" => "nombres"));
     $people = People::find(array("estado='A'", "order" => "nombres"));
     $this->assertEquals(count($personas), count($people));
     $personas = Personas::find(array("estado='A'", "order" => "nombres", "limit" => 100));
     $people = People::find(array("estado='A'", "order" => "nombres", "limit" => 100));
     $this->assertEquals(count($personas), count($people));
     $params = array("estado=?1", "bind" => array(1 => "A"), "order" => "nombres", "limit" => 100);
     $personas = Personas::find($params);
     $people = People::find($params);
     $this->assertEquals(count($personas), count($people));
     $params = array("estado=:estado:", "bind" => array("estado" => "A"), "order" => "nombres", "limit" => 100);
     $personas = Personas::find($params);
     $people = People::find($params);
     $this->assertEquals(count($personas), count($people));
     $number = 0;
     $peoples = Personas::find(array("conditions" => "estado='A'", "order" => "nombres", "limit" => 20));
     foreach ($peoples as $people) {
         $number++;
     }
     $this->assertEquals($number, 20);
     $persona = new Personas($di);
     $persona->cedula = 'CELL' . mt_rand(0, 999999);
     $this->assertFalse($persona->save());
     //Messages
     $this->assertEquals(count($persona->getMessages()), 4);
     $messages = array(0 => ModelMessage::__set_state(array('_type' => 'PresenceOf', '_message' => 'tipo_documento_id is required', '_field' => 'tipo_documento_id')), 1 => ModelMessage::__set_state(array('_type' => 'PresenceOf', '_message' => 'nombres is required', '_field' => 'nombres')), 2 => ModelMessage::__set_state(array('_type' => 'PresenceOf', '_message' => 'cupo is required', '_field' => 'cupo')), 3 => ModelMessage::__set_state(array('_type' => 'PresenceOf', '_message' => 'estado is required', '_field' => 'estado')));
     $this->assertEquals($persona->getMessages(), $messages);
     //Save
     $persona = new Personas($di);
     $persona->cedula = 'CELL' . mt_rand(0, 999999);
     $persona->tipo_documento_id = 1;
     $persona->nombres = 'LOST';
     $persona->telefono = '1';
     $persona->cupo = 20000;
     $persona->estado = 'A';
     $this->assertTrue($persona->save());
     $persona = new Personas($di);
     $persona->cedula = 'CELL' . mt_rand(0, 999999);
     $persona->tipo_documento_id = 1;
     $persona->nombres = 'LOST LOST';
     $persona->telefono = '2';
     $persona->cupo = 0;
     $persona->estado = 'X';
     $this->assertTrue($persona->save());
     //Check correct save
     $persona = Personas::findFirst(array("estado='X'"));
     $this->assertNotEquals($persona, false);
     $this->assertEquals($persona->nombres, 'LOST LOST');
     $this->assertEquals($persona->estado, 'X');
     //Update
     $persona->cupo = 150000;
     $persona->telefono = '123';
     $this->assertTrue($persona->update());
     //Checking correct update
     $persona = Personas::findFirst(array("estado='X'"));
     $this->assertNotEquals($persona, false);
     $this->assertEquals($persona->cupo, 150000);
     $this->assertEquals($persona->telefono, '123');
     //Update
     $this->assertTrue($persona->update(array('nombres' => 'LOST UPDATE', 'telefono' => '2121')));
     //Checking correct update
     $persona = Personas::findFirst(array("estado='X'"));
     $this->assertNotEquals($persona, false);
     $this->assertEquals($persona->nombres, 'LOST UPDATE');
     $this->assertEquals($persona->telefono, '2121');
     //Create
     $persona = new Personas($di);
     $persona->cedula = 'CELL' . mt_rand(0, 999999);
     $persona->tipo_documento_id = 1;
     $persona->nombres = 'LOST CREATE';
     $persona->telefono = '1';
     $persona->cupo = 21000;
     $persona->estado = 'A';
     $this->assertTrue($persona->create());
     $persona = new Personas($di);
     $this->assertTrue($persona->create(array('cedula' => 'CELL' . mt_rand(0, 999999), 'tipo_documento_id' => 1, 'nombres' => 'LOST CREATE', 'telefono' => '1', 'cupo' => 21000, 'estado' => 'A')));
     //Grouping
     $difEstados = People::count(array("distinct" => "estado"));
     $this->assertEquals($difEstados, 3);
     $group = People::count(array("group" => "estado"));
     $this->assertEquals(count($group), 3);
     //Deleting
     $before = People::count();
     $this->assertTrue($persona->delete());
     $this->assertEquals($before - 1, People::count());
     //Assign
     $persona = new Personas();
     $persona->assign(array('tipo_documento_id' => 1, 'nombres' => 'LOST CREATE', 'telefono' => '1', 'cupo' => 21000, 'estado' => 'A', 'notField' => 'SOME VALUE'));
     $expected = array('cedula' => NULL, 'tipo_documento_id' => 1, 'nombres' => 'LOST CREATE', 'telefono' => '1', 'direccion' => NULL, 'email' => NULL, 'fecha_nacimiento' => NULL, 'ciudad_id' => NULL, 'creado_at' => NULL, 'cupo' => 21000, 'estado' => 'A');
     $this->assertEquals($persona->toArray(), $expected);
     //Refresh
     $persona = Personas::findFirst();
     $personaData = $persona->toArray();
     $persona->assign(array('tipo_documento_id' => 1, 'nombres' => 'LOST CREATE', 'telefono' => '1', 'cupo' => 21000, 'estado' => 'A', 'notField' => 'SOME VALUE'));
     $persona->refresh();
     $this->assertEquals($personaData, $persona->toArray());
     // Issue 1314
     $parts = new Parts2();
     $parts->save();
 }
예제 #13
0
<?php

$robot = Robots::findFirst(11);
if ($robot != false) {
    if ($robot->delete() == false) {
        echo "Sorry, we can't delete the robot right now: \n";
        foreach ($robot->getMessages() as $message) {
            echo $message, "\n";
        }
    } else {
        echo "The robot was deleted successfully!";
    }
}
<?php

//How many robots are there?
$robots = Robots::find();
echo "There are ", count($robots), "\n";
//How many mechanical robots are there?
$robots = Robots::find(array(array("type" => "mechanical")));
echo "There are ", count($robots), "\n";
//Get and print virtual robots ordered by name
$robots = Robots::findFirst(array(array("type" => "virtual"), "order" => array("name" => 1)));
foreach ($robots as $robot) {
    echo $robot->name, "\n";
}
//Get first 100 virtual robots ordered by name
$robots = Robots::find(array(array("type" => "virtual"), "order" => array("name" => 1), "limit" => 100));
foreach ($robots as $robot) {
    echo $robot->name, "\n";
}
예제 #15
0
<?php

$robot = Robots::findFirst(2);
// Robots model has a 1-n (hasMany)
// relationship to RobotsParts, then
$robotsParts = RobotsParts::find("robots_id = '" . $robot->id . "'");
// Only parts that match conditions
$robotsParts = RobotsParts::find("robots_id = '" . $robot->id . "' AND created_at = '2012-03-15'");
$robotPart = RobotsParts::findFirst(1);
// RobotsParts model has a n-1 (belongsTo)
// relationship to RobotsParts then
$robot = Robots::findFirst("id = '" . $robotPart->robots_id . "'");
예제 #16
0
 public function testMetadataFiles()
 {
     require 'unit-tests/config.db.php';
     if (empty($configMysql)) {
         $this->markTestSkipped('Test skipped');
         return;
     }
     $di = $this->_getDI();
     $di->set('modelsMetadata', function () {
         return new Phalcon\Mvc\Model\Metadata\Files(array('metaDataDir' => 'unit-tests/cache/'));
     });
     $metaData = $di->getShared('modelsMetadata');
     $metaData->reset();
     $this->assertTrue($metaData->isEmpty());
     Robots::findFirst();
     $this->assertEquals(require 'unit-tests/cache/meta-robots-robots.php', $this->_data['meta-robots-robots']);
     $this->assertEquals(require 'unit-tests/cache/map-robots.php', $this->_data['map-robots']);
     $this->assertFalse($metaData->isEmpty());
     $metaData->reset();
     $this->assertTrue($metaData->isEmpty());
     Robots::findFirst();
 }
<?php

//What's the first robot in the robots table?
$robot = Robots::findFirst();
echo "The robot name is ", $robot->name, "\n";
//What's the first mechanical robot in robots table?
$robot = Robots::findFirst(array(array("type" => "mechanical")));
echo "The first mechanical robot name is ", $robot->name, "\n";
//Get first virtual robot ordered by name
$robot = Robots::findFirst(array(array("type" => "mechanical"), "order" => array("name" => 1)));
echo "The first virtual robot name is ", $robot->name, "\n";
예제 #18
0
<?php

// First robot where type = "mechanical" and year = "1999"
$robot = Robots::findFirst(array("type" => "mechanical", "year" => "1999"));
// All virtual robots ordered by name downward
$robots = Robots::find(array("conditions" => array("type" => "virtual"), "sort" => array("name" => -1)));
예제 #19
0
 public function _testSelectExecute($di)
 {
     $manager = $di->getShared('modelsManager');
     $robots = $manager->executeQuery('SELECT * FROM Robots');
     $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Resultset\\Simple', $robots);
     $this->assertEquals(count($robots), 3);
     $this->assertInstanceOf('Robots', $robots[0]);
     $robots = $manager->executeQuery('SELECT * FROM Robots ORDER BY 1');
     $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Resultset\\Simple', $robots);
     $this->assertEquals(count($robots), 3);
     $this->assertInstanceOf('Robots', $robots[0]);
     $this->assertEquals($robots[0]->id, 1);
     $robots = $manager->executeQuery('SELECT * FROM Robots ORDER BY id');
     $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Resultset\\Simple', $robots);
     $this->assertEquals(count($robots), 3);
     $this->assertInstanceOf('Robots', $robots[0]);
     $this->assertEquals($robots[0]->id, 1);
     $robots = $manager->executeQuery('SELECT * FROM Robots ORDER BY Robots.id');
     $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Resultset\\Simple', $robots);
     $this->assertEquals(count($robots), 3);
     $this->assertInstanceOf('Robots', $robots[0]);
     $this->assertEquals($robots[0]->id, 1);
     $robots = $manager->executeQuery('SELECT Robots.* FROM Robots');
     $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Resultset\\Simple', $robots);
     $this->assertInstanceOf('Robots', $robots[0]);
     $this->assertEquals(count($robots), 3);
     $robots = $manager->executeQuery('SELECT r.* FROM Robots r');
     $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Resultset\\Simple', $robots);
     $this->assertInstanceOf('Robots', $robots[0]);
     $this->assertEquals(count($robots), 3);
     $robots = $manager->executeQuery('SELECT * FROM Robots r');
     $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Resultset\\Simple', $robots);
     $this->assertInstanceOf('Robots', $robots[0]);
     $this->assertEquals(count($robots), 3);
     $robots = $manager->executeQuery('SELECT * FROM Robots AS r');
     $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Resultset\\Simple', $robots);
     $this->assertInstanceOf('Robots', $robots[0]);
     $this->assertEquals(count($robots), 3);
     $robots = $manager->executeQuery('SELECT * FROM Robots AS r');
     $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Resultset\\Simple', $robots);
     $this->assertInstanceOf('Robots', $robots[0]);
     $this->assertEquals(count($robots), 3);
     $result = $manager->executeQuery('SELECT id, name FROM Robots');
     $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Resultset\\Simple', $result);
     $this->assertEquals(count($result), 3);
     $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Row', $result[0]);
     $result = $manager->executeQuery('SELECT Robots.name FROM Robots');
     $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Resultset\\Simple', $result);
     $this->assertEquals(count($result), 3);
     $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Row', $result[0]);
     $result = $manager->executeQuery('SELECT LENGTH(name) AS the_length FROM Robots');
     $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Resultset\\Simple', $result);
     $this->assertEquals(count($result), 3);
     $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Row', $result[0]);
     $this->assertTrue(isset($result[0]->the_length));
     $this->assertEquals($result[0]->the_length, 8);
     $result = $manager->executeQuery('SELECT LENGTH(Robots.name) AS the_length FROM Robots');
     $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Resultset\\Simple', $result);
     $this->assertEquals(count($result), 3);
     $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Row', $result[0]);
     $this->assertTrue(isset($result[0]->the_length));
     $this->assertEquals($result[0]->the_length, 8);
     $result = $manager->executeQuery('SELECT Robots.id+1 AS nextId FROM Robots WHERE id = 1');
     $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Resultset\\Simple', $result);
     $this->assertEquals(count($result), 1);
     $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Row', $result[0]);
     $this->assertTrue(isset($result[0]->nextId));
     $this->assertEquals($result[0]->nextId, 2);
     $result = $manager->executeQuery('SELECT Robots.id+1 AS nextId FROM Robots WHERE id = ?0', array(0 => 1));
     $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Resultset\\Simple', $result);
     $this->assertEquals(count($result), 1);
     $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Row', $result[0]);
     $this->assertTrue(isset($result[0]->nextId));
     $this->assertEquals($result[0]->nextId, 2);
     $result = $manager->executeQuery('SELECT Robots.id+1 AS nextId FROM Robots WHERE id = :id:', array('id' => 1));
     $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Resultset\\Simple', $result);
     $this->assertEquals(count($result), 1);
     $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Row', $result[0]);
     $this->assertTrue(isset($result[0]->nextId));
     $this->assertEquals($result[0]->nextId, 2);
     $result = $manager->executeQuery('SELECT Robots.id+1 AS nextId FROM Robots WHERE id = "1"');
     $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Resultset\\Simple', $result);
     $this->assertEquals(count($result), 1);
     $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Row', $result[0]);
     $this->assertTrue(isset($result[0]->nextId));
     $this->assertEquals($result[0]->nextId, 2);
     $result = $manager->executeQuery('SELECT r.year FROM Robots r WHERE TRIM(name) != "Robotina"');
     $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Resultset\\Simple', $result);
     $this->assertEquals(count($result), 2);
     $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Row', $result[0]);
     $result = $manager->executeQuery('SELECT Robots.id+1 AS nextId FROM Robots ORDER BY id');
     $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Resultset\\Simple', $result);
     $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Row', $result[0]);
     $this->assertEquals(count($result), 3);
     $this->assertTrue(isset($result[0]->nextId));
     $this->assertEquals($result[0]->nextId, 2);
     $result = $manager->executeQuery('SELECT Robots.id+1 AS nextId FROM Robots ORDER BY id LIMIT 2');
     $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Resultset\\Simple', $result);
     $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Row', $result[0]);
     $this->assertEquals(count($result), 2);
     $this->assertTrue(isset($result[0]->nextId));
     $this->assertEquals($result[0]->nextId, 2);
     $result = $manager->executeQuery('SELECT Robots.id+1 AS nextId FROM Robots ORDER BY id DESC LIMIT 2');
     $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Resultset\\Simple', $result);
     $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Row', $result[0]);
     $this->assertEquals(count($result), 2);
     $this->assertTrue(isset($result[0]->nextId));
     $this->assertEquals($result[0]->nextId, 4);
     $result = $manager->executeQuery('SELECT r.name FROM Robots r ORDER BY r.name DESC LIMIT 2');
     $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Resultset\\Simple', $result);
     $this->assertEquals(count($result), 2);
     $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Row', $result[0]);
     $this->assertTrue(isset($result[0]->name));
     $this->assertEquals($result[0]->name, 'Terminator');
     $result = $manager->executeQuery('SELECT name le_name FROM Robots ORDER BY name ASC LIMIT 4');
     $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Resultset\\Simple', $result);
     $this->assertEquals(count($result), 3);
     $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Row', $result[0]);
     $this->assertTrue(isset($result[0]->le_name));
     $this->assertEquals($result[0]->le_name, 'Astro Boy');
     $result = $manager->executeQuery('SELECT r.name le_name FROM Robots r ORDER BY r.name ASC LIMIT 4');
     $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Resultset\\Simple', $result);
     $this->assertEquals(count($result), 3);
     $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Row', $result[0]);
     $this->assertTrue(isset($result[0]->le_name));
     $this->assertEquals($result[0]->le_name, 'Astro Boy');
     $result = $manager->executeQuery('SELECT r.name le_name FROM Robots r ORDER BY r.name ASC LIMIT 1,2');
     $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Resultset\\Simple', $result);
     $this->assertEquals(count($result), 2);
     $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Row', $result[0]);
     $this->assertTrue(isset($result[0]->le_name));
     $this->assertEquals($result[0]->le_name, 'Robotina');
     $result = $manager->executeQuery('SELECT r.name le_name FROM Robots r ORDER BY r.name ASC LIMIT 2 OFFSET 1');
     $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Resultset\\Simple', $result);
     $this->assertEquals(count($result), 2);
     $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Row', $result[0]);
     $this->assertTrue(isset($result[0]->le_name));
     $this->assertEquals($result[0]->le_name, 'Robotina');
     $result = $manager->executeQuery('SELECT r.type, COUNT(*) number FROM Robots r GROUP BY 1 ORDER BY r.type ASC');
     $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Resultset\\Simple', $result);
     $this->assertEquals(count($result), 2);
     $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Row', $result[0]);
     $this->assertEquals($result[1]->number, 2);
     $result = $manager->executeQuery('SELECT r.type, SUM(r.year-1000) age FROM Robots r GROUP BY 1 ORDER BY 2 DESC');
     $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Resultset\\Simple', $result);
     $this->assertEquals(count($result), 2);
     $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Row', $result[0]);
     $this->assertEquals($result[0]->age, 1924);
     $result = $manager->executeQuery('SELECT r.type, COUNT(*) number FROM Robots r GROUP BY 1 HAVING COUNT(*) = 2');
     $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Resultset\\Simple', $result);
     $this->assertEquals(count($result), 1);
     $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Row', $result[0]);
     $this->assertEquals($result[0]->number, 2);
     $result = $manager->executeQuery('SELECT r.type, COUNT(*) number FROM Robots r GROUP BY 1 HAVING COUNT(*) < 2');
     $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Resultset\\Simple', $result);
     $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Row', $result[0]);
     $this->assertEquals(count($result), 1);
     $this->assertEquals($result[0]->number, 1);
     //Cast function
     //TODO: CHAR in postgresql is acually a single char, but I can't specify a length
     //for the field type like CHAR(10) because phalcon phql doesn't support it
     $cast_type = "CHAR";
     if ($di->get("db") instanceof Phalcon\Db\Adapter\Pdo\Postgresql) {
         $cast_type = "TEXT";
     }
     $result = $manager->executeQuery("SELECT CAST(year AS {$cast_type}) test FROM Robots LIMIT 1");
     $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Resultset\\Simple', $result);
     $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Row', $result[0]);
     $this->assertEquals(Robots::findFirst()->year, $result[0]->test);
     //Convert using... is supported only by mysql (and it's in the sql standards, bad postgresql/sqlite!)
     if ($di->get("db") instanceof Phalcon\Db\Adapter\Pdo\Mysql) {
         $result = $manager->executeQuery("SELECT CONVERT(year USING utf8) test FROM Robots LIMIT 1");
         $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Resultset\\Simple', $result);
         $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Row', $result[0]);
         $this->assertEquals(Robots::findFirst()->year, $result[0]->test);
     }
     //Nested Cast
     $result = $manager->executeQuery("SELECT CAST(CAST(year AS {$cast_type}) AS DECIMAL) test FROM Robots LIMIT 1");
     $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Resultset\\Simple', $result);
     $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Row', $result[0]);
     $this->assertEquals(Robots::findFirst()->year, $result[0]->test);
     $result = $manager->executeQuery('SELECT r.id, r.* FROM Robots r');
     $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Resultset\\Complex', $result);
     $this->assertNotEquals(gettype($result[0]->id), 'object');
     $this->assertEquals(gettype($result[0]->r), 'object');
     $this->assertEquals(count($result), 3);
     $this->assertEquals($result[0]->id, 1);
     $result = $manager->executeQuery('SELECT Robots.*, RobotsParts.* FROM Robots JOIN RobotsParts ORDER BY Robots.id, RobotsParts.id');
     $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Resultset\\Complex', $result);
     $this->assertEquals(gettype($result[0]->robots), 'object');
     $this->assertEquals(get_class($result[0]->robots), 'Robots');
     $this->assertEquals(gettype($result[0]->robotsParts), 'object');
     $this->assertEquals(get_class($result[0]->robotsParts), 'RobotsParts');
     $this->assertEquals(count($result), 3);
     $this->assertEquals($result[0]->robots->id, 1);
     $this->assertEquals($result[0]->robotsParts->id, 1);
     $this->assertEquals($result[1]->robots->id, 1);
     $this->assertEquals($result[1]->robotsParts->id, 2);
     $result = $manager->executeQuery('SELECT Robots.*, RobotsParts.* FROM Robots JOIN RobotsParts ON Robots.id = RobotsParts.robots_id ORDER BY Robots.id, RobotsParts.id');
     $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Resultset\\Complex', $result);
     $this->assertEquals(gettype($result[0]->robots), 'object');
     $this->assertEquals(get_class($result[0]->robots), 'Robots');
     $this->assertEquals(gettype($result[0]->robotsParts), 'object');
     $this->assertEquals(get_class($result[0]->robotsParts), 'RobotsParts');
     $this->assertEquals(count($result), 3);
     $this->assertEquals($result[0]->robots->id, 1);
     $this->assertEquals($result[0]->robotsParts->id, 1);
     $this->assertEquals($result[1]->robots->id, 1);
     $this->assertEquals($result[1]->robotsParts->id, 2);
     $result = $manager->executeQuery('SELECT r.*, p.* FROM Robots r JOIN RobotsParts p ON r.id = p.robots_id ORDER BY r.id, p.id');
     $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Resultset\\Complex', $result);
     $this->assertEquals(gettype($result[0]->r), 'object');
     $this->assertEquals(get_class($result[0]->r), 'Robots');
     $this->assertEquals(gettype($result[0]->p), 'object');
     $this->assertEquals(get_class($result[0]->p), 'RobotsParts');
     $this->assertEquals(count($result), 3);
     $this->assertEquals($result[0]->r->id, 1);
     $this->assertEquals($result[0]->p->id, 1);
     $this->assertEquals($result[1]->r->id, 1);
     $this->assertEquals($result[1]->p->id, 2);
     //Joins with namespaces
     $result = $manager->executeQuery('SELECT Some\\Robots.*, Some\\RobotsParts.* FROM Some\\Robots JOIN Some\\RobotsParts ON Some\\Robots.id = Some\\RobotsParts.robots_id ORDER BY Some\\Robots.id, Some\\RobotsParts.id');
     $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Resultset\\Complex', $result);
     $this->assertEquals(gettype($result[0]->{'some\\Robots'}), 'object');
     $this->assertEquals(get_class($result[0]->{'some\\Robots'}), 'Some\\Robots');
     $this->assertEquals(gettype($result[0]->{'some\\RobotsParts'}), 'object');
     $this->assertEquals(get_class($result[0]->{'some\\RobotsParts'}), 'Some\\RobotsParts');
     $this->assertEquals(count($result), 3);
     $this->assertEquals($result[0]->{'some\\Robots'}->id, 1);
     $this->assertEquals($result[0]->{'some\\RobotsParts'}->id, 1);
     $this->assertEquals($result[1]->{'some\\Robots'}->id, 1);
     $this->assertEquals($result[1]->{'some\\RobotsParts'}->id, 2);
     //Joins with namespaces and aliases
     $result = $manager->executeQuery('SELECT r.*, p.* FROM Some\\Robots r JOIN Some\\RobotsParts p ON r.id = p.robots_id ORDER BY r.id, p.id');
     $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Resultset\\Complex', $result);
     $this->assertEquals(gettype($result[0]->r), 'object');
     $this->assertEquals(get_class($result[0]->r), 'Some\\Robots');
     $this->assertEquals(gettype($result[0]->p), 'object');
     $this->assertEquals(get_class($result[0]->p), 'Some\\RobotsParts');
     $this->assertEquals(count($result), 3);
     $this->assertEquals($result[0]->r->id, 1);
     $this->assertEquals($result[0]->p->id, 1);
     $this->assertEquals($result[1]->r->id, 1);
     $this->assertEquals($result[1]->p->id, 2);
     $result = $manager->executeQuery('SELECT id, name FROM Some\\Robots');
     $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Resultset\\Simple', $result);
     $this->assertEquals(count($result), 3);
     $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Row', $result[0]);
     $result = $manager->executeQuery('SELECT id, name FROM Some\\Robots ORDER BY name DESC LIMIT 2');
     $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Resultset\\Simple', $result);
     $this->assertEquals(count($result), 2);
     $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Row', $result[0]);
     // Issue 1011
     $result = $manager->executeQuery('SELECT r.name le_name FROM Robots r ORDER BY r.name ASC LIMIT ?1,?2', array(1 => 1, 2 => 2), array(1 => \Phalcon\Db\Column::BIND_PARAM_INT, 2 => \Phalcon\Db\Column::BIND_PARAM_INT));
     $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Resultset\\Simple', $result);
     $this->assertEquals(count($result), 2);
     $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Row', $result[0]);
     $this->assertTrue(isset($result[0]->le_name));
     $this->assertEquals($result[0]->le_name, 'Robotina');
 }
예제 #20
0
<?php

//Find a robot by its name
$robot = Robots::findFirst("theName = 'Voltron'");
echo $robot->theName, "\n";
//Get robots ordered by type
$robot = Robots::find(array('order' => 'theType DESC'));
foreach ($robots as $robot) {
    echo 'Code: ', $robot->code, "\n";
}
//Create a robot
$robot = new Robots();
$robot->code = '10101';
$robot->theName = 'Bender';
$robot->theType = 'Industrial';
$robot->theYear = 2999;
$robot->save();
 public function testMetadataLibmemcached()
 {
     require __DIR__ . '/config.db.php';
     if (empty($configMysql)) {
         $this->markTestSkipped('Test skipped');
         return;
     }
     if (!class_exists('Memcached')) {
         $this->markTestSkipped('Memcached class does not exist, test skipped');
         return;
     }
     $di = $this->_getDI();
     $di->set('modelsMetadata', function () {
         return new Phalcon\Mvc\Model\Metadata\Libmemcached(array("servers" => array(array("host" => "localhost", "port" => "11211", "weight" => "1"))));
     });
     $metaData = $di->getShared('modelsMetadata');
     $metaData->reset();
     $this->assertTrue($metaData->isEmpty());
     Robots::findFirst();
     $this->assertEquals($metaData->read("meta-robots-robots"), $this->_data['meta-robots-robots']);
     $this->assertEquals($metaData->read("map-robots"), $this->_data['map-robots']);
     $this->assertFalse($metaData->isEmpty());
     $metaData->reset();
     $this->assertTrue($metaData->isEmpty());
     Robots::findFirst();
 }
 public function testMetadataFiles()
 {
     $di = $this->_getDI();
     $di->set('modelsMetadata', function () {
         return new Phalcon\Mvc\Model\Metadata\Files(array('metaDataDir' => 'unit-tests/cache/'));
     });
     @unlink('unit-tests/cache/meta-robots-robots.php');
     @unlink('unit-tests/cache/map-robots.php');
     $metaData = $di->getShared('modelsMetadata');
     $metaData->reset();
     $this->assertTrue($metaData->isEmpty());
     Robots::findFirst();
     $this->assertEquals(require 'unit-tests/cache/meta-robots-robots.php', $this->_data['meta-robots-robots']);
     $this->assertEquals(require 'unit-tests/cache/map-robots.php', $this->_data['map-robots']);
     $this->assertFalse($metaData->isEmpty());
     Robots::findFirst();
 }
 public function testMetadataCache()
 {
     if (!extension_loaded('mongo')) {
         $this->markTestSkipped('Warning: mongo extension is not loaded');
         return;
     }
     require 'unit-tests/config.db.php';
     if (empty($configMysql)) {
         $this->markTestSkipped('Test skipped');
         return;
     }
     $di = $this->_getDI();
     $di->set('mycache', function () {
         $frontCache = new Phalcon\Cache\Frontend\Output(array('lifetime' => 60));
         $cache = new Phalcon\Cache\Backend\Mongo($frontCache, array('server' => 'mongodb://localhost', 'db' => 'phalcon_test', 'collection' => 'caches'));
         return $cache;
     });
     $di->set('modelsMetadata', function () {
         return new Phalcon\Mvc\Model\Metadata\Cache(array('service' => 'mycache'));
     });
     $metaData = $di->getShared('modelsMetadata');
     $metaData->reset();
     $this->assertTrue($metaData->isEmpty());
     Robots::findFirst();
     $this->assertEquals($metaData->read('meta-robots-robots'), $this->_data['meta-robots-robots']);
     $this->assertEquals($metaData->read('map-robots-robots'), $this->_data['map-robots-robots']);
     $this->assertFalse($metaData->isEmpty());
     $metaData->reset();
     $this->assertTrue($metaData->isEmpty());
     Robots::findFirst();
 }
예제 #24
0
<?php

$robot = Robots::findFirst(array("type = 'virtual'", "order" => "name DESC", "limit" => 30));
$robots = Robots::find(array("conditions" => "type = ?1", "bind" => array(1 => "virtual")));
예제 #25
0
 protected function _testCacheDefaultDI($di)
 {
     $di->set('modelsCache', function () {
         $frontCache = new Phalcon\Cache\Frontend\Data();
         return new Phalcon\Cache\Backend\File($frontCache, array('cacheDir' => 'unit-tests/cache/'));
     }, true);
     //Find
     $robots = Robots::find(array('cache' => array('key' => 'some'), 'order' => 'id'));
     $this->assertEquals(count($robots), 3);
     $this->assertTrue($robots->isFresh());
     $robots = Robots::find(array('cache' => array('key' => 'some'), 'order' => 'id'));
     $this->assertEquals(count($robots), 3);
     $this->assertFalse($robots->isFresh());
     //TODO: I really can't understand why postgresql fails on inserting a simple record
     //The error is "Object not in prerequisite state: 7 ERROR:
     //currval of sequence "robots_id_seq" is not yet defined in this session"
     //Is the ORM working with postgresql, is the database structure incorrect or
     //I'm using the wrong code?
     //Skip this test until someone can shed some light on this
     if (!$di->get("db") instanceof Phalcon\Db\Adapter\Pdo\Postgresql) {
         //Aggregate functions like sum, count, etc
         $robotscount = Robots::count(array('cache' => array('key' => 'some-count')));
         $this->assertEquals($robotscount, 3);
         //Create a temporary robot to test if the count is cached or fresh
         $newrobot = new Robots();
         $newrobot->name = "Not cached robot";
         $newrobot->type = "notcached";
         $newrobot->year = 2014;
         $newrobot->datetime = '2015-03-05 04:16:17';
         $newrobot->text = 'Not cached robot';
         $newrobot->create();
         $robotscount = Robots::count(array('cache' => array('key' => 'some-count')));
         $this->assertEquals($robotscount, 3);
         //Delete the temp robot
         Robots::findFirst("type = 'notcached'")->delete();
     }
 }
<?php

$robot = Robots::findFirst();
$robot->delete();
foreach (Robots::find() as $robot) {
    $robot->delete();
}
예제 #27
0
<?php

$robot = Robots::findFirst(array(array('name' => 'Astroy Boy')));
$robot->name = "Voltron";
$robot->save();