public function testCacheResultsetBinding() { $cache = $this->_getCache(); $initialId = 0; $finalId = 4; $cache->save('test-resultset', Robots::find(array('conditions' => 'id > :id1: and id < :id2:', 'bind' => array('id1' => $initialId, 'id2' => $finalId), 'order' => 'id'))); $this->assertTrue(file_exists(__DIR__ . '/cache/test-resultset')); $robots = $cache->get('test-resultset'); $this->assertEquals(get_class($robots), 'Phalcon\\Mvc\\Model\\Resultset\\Simple'); $this->assertEquals(count($robots), 3); $this->assertEquals($robots->count(), 3); }
/** * Tests an image tag with a bare minimum of information passed * * @author Nikos Dimopoulos <*****@*****.**> * @since 2012-09-09 */ public function testSelectBasic() { // Populate the table // Empty the relationship table first $this->emptyTable('robots_parts'); $this->populateTable('robots'); $this->populateTable('robots_parts'); $robots = \Robots::find(); $params = array('some_name', $robots, 'using' => array('id', 'name')); $expected = '<select id="some_name" name="some_name">' . PHP_EOL . chr(9) . '<option value="1">Robotina</option>' . PHP_EOL . chr(9) . '<option value="2">Astro Boy</option>' . PHP_EOL . chr(9) . '<option value="3">Terminator</option>' . PHP_EOL . '</select>'; $actual = PhTag::select($params); $this->assertEquals($expected, $actual, sprintf($this->message, 'select basic')); }
public function testCacheResultsetNormal() { require 'unit-tests/config.db.php'; if (empty($configMysql)) { $this->markTestSkipped('Test skipped'); return; } $cache = $this->_getCache(); $cache->save('test-resultset', Robots::find(array('order' => 'id'))); $this->assertTrue(file_exists('unit-tests/cache/test-resultset')); $robots = $cache->get('test-resultset'); $this->assertEquals(get_class($robots), 'Phalcon\\Mvc\\Model\\Resultset\\Simple'); $this->assertEquals(count($robots), 3); $this->assertEquals($robots->count(), 3); }
public static function findByName($name) { $response = new Response(); if (!$name) { $response->status = $response::STATUS_BAD_REQUEST; $response->addError('параметр "name" не может быть пустым'); return $response->toArray(); } try { $response->data = Robots::find(['conditions' => 'name LIKE :name:', 'bind' => ['name' => '%' . $name . '%']])->toArray(); } catch (Exception $e) { $response->setException($e); } finally { return $response->toArray(); } }
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); } }
public function testResultsetAppendIterator() { if (!$this->_prepareTestMysql()) { $this->markTestSkipped("Skipped"); return; } // see http://php.net/manual/en/appenditerator.construct.php $iterator = new \AppendIterator(); $robots_first = Robots::find(array('limit' => 2)); $robots_second = Robots::find(array('limit' => 1, 'offset' => 2)); $robots_first_0 = $robots_first[0]; $robots_first_1 = $robots_first[1]; $robots_second_0 = $robots_second[0]; $iterator->append($robots_first); $iterator->append($robots_second); $iterator->rewind(); $this->assertTrue($iterator->valid()); $this->assertEquals($iterator->key(), 0); $this->assertEquals($iterator->getIteratorIndex(), 0); $this->assertEquals(get_class($iterator->current()), 'Robots'); $this->assertEquals($robots_first_0->name, $iterator->current()->name); $iterator->next(); $this->assertTrue($iterator->valid()); $this->assertEquals($iterator->key(), 1); $this->assertEquals($iterator->getIteratorIndex(), 0); $this->assertEquals(get_class($iterator->current()), 'Robots'); $this->assertEquals($robots_first_1->name, $iterator->current()->name); $iterator->next(); $this->assertTrue($iterator->valid()); $this->assertEquals($iterator->key(), 0); $this->assertEquals($iterator->getIteratorIndex(), 1); $this->assertEquals(get_class($iterator->current()), 'Robots'); $this->assertEquals($robots_second_0->name, $iterator->current()->name); $iterator->next(); $this->assertFalse($iterator->valid()); }
<?php $robot = Robots::findFirst(); $robot->delete(); foreach (Robots::find() as $robot) { $robot->delete(); }
protected function _executeTestsNormal($di) { $number = 0; $robots = Robots::find(); foreach ($robots as $robot) { $this->assertTrue(is_object($robot)); $this->assertEquals(get_class($robot), 'Robots'); $number++; } $robots->setHydrateMode(Phalcon\Mvc\Model\Resultset::HYDRATE_RECORDS); foreach ($robots as $robot) { $this->assertTrue(is_object($robot)); $this->assertEquals(get_class($robot), 'Robots'); $number++; } $robots->setHydrateMode(Phalcon\Mvc\Model\Resultset::HYDRATE_ARRAYS); foreach ($robots as $robot) { $this->assertTrue(is_array($robot)); $this->assertEquals(count($robot), 4); $number++; } $robots->setHydrateMode(Phalcon\Mvc\Model\Resultset::HYDRATE_OBJECTS); foreach ($robots as $robot) { $this->assertTrue(is_object($robot)); $this->assertEquals(get_class($robot), 'stdClass'); $number++; } $this->assertEquals($number, 12); $number = 0; $people = People::find(array('limit' => 33)); foreach ($people as $person) { $this->assertTrue(is_object($person)); $this->assertEquals(get_class($person), 'People'); $number++; } $people->setHydrateMode(Phalcon\Mvc\Model\Resultset::HYDRATE_RECORDS); foreach ($people as $person) { $this->assertTrue(is_object($person)); $this->assertEquals(get_class($person), 'People'); $number++; } $people->setHydrateMode(Phalcon\Mvc\Model\Resultset::HYDRATE_ARRAYS); foreach ($people as $person) { $this->assertTrue(is_array($person)); $number++; } $people->setHydrateMode(Phalcon\Mvc\Model\Resultset::HYDRATE_OBJECTS); foreach ($people as $person) { $this->assertTrue(is_object($person)); $this->assertEquals(get_class($person), 'stdClass'); $number++; } $this->assertEquals($number, 33 * 4); }
<?php $robots = Robots::find('id < 1000'); $robots = Robots::find('id > 100 AND type = "A"'); $robots = Robots::find('(id > 100 AND type = "A") AND id < 2000'); $robots = Robots::find(array('(id > ?0 AND type = "A") AND id < ?1', 'bind' => array(100, 2000), 'order' => 'type'));
<?php use Phalcon\Mvc\Model\Transaction\Manager as TxManager, Phalcon\Mvc\Model\Transaction\Failed as TxFailed; try { //Create a transaction manager $manager = new TxManager(); //Request a transaction $transaction = $manager->get(); //Get the robots will be deleted foreach (Robots::find("type = 'mechanical'") as $robot) { $robot->setTransaction($transaction); if ($robot->delete() == false) { //Something goes wrong, we should to rollback the transaction foreach ($robot->getMessages() as $message) { $transaction->rollback($message->getMessage()); } } } //Everything goes fine, let's commit the transaction $transaction->commit(); echo "Robots were deleted successfully!"; } catch (TxFailed $e) { echo "Failed, reason: ", $e->getMessage(); }
public function testCacheResultsetBinding() { require 'unit-tests/config.db.php'; if (empty($configMysql)) { $this->markTestSkipped('Test skipped'); return; } $cache = $this->_getCache(); $initialId = 0; $finalId = 4; $cache->save('test-resultset', Robots::find(array('conditions' => 'id > :id1: and id < :id2:', 'bind' => array('id1' => $initialId, 'id2' => $finalId), 'order' => 'id'))); $this->assertTrue(file_exists('unit-tests/cache/test-resultset')); $robots = $cache->get('test-resultset'); $this->assertEquals(get_class($robots), 'Phalcon\\Mvc\\Model\\Resultset\\Simple'); $this->assertEquals(count($robots), 3); $this->assertEquals($robots->count(), 3); }
<?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 mechanical robots ordered by name upward $robots = Robots::find(array(array("type" => "mechanical"), "sort" => array("name" => 1))); foreach ($robots as $robot) { echo $robot->name, "\n"; } // Get first 100 mechanical robots ordered by name $robots = Robots::find(array(array("type" => "mechanical"), "sort" => array("name" => 1), "limit" => 100)); foreach ($robots as $robot) { echo $robot->name, "\n"; }
<?php use Phalcon\Mvc\Model\Resultset; $robots = Robots::find(); //Return every robot as an array $robots->setHydrateMode(Resultset::HYDRATE_ARRAYS); foreach ($robots as $robot) { echo $robot['year'], PHP_EOL; } //Return every robot as an stdClass $robots->setHydrateMode(Resultset::HYDRATE_OBJECTS); foreach ($robots as $robot) { echo $robot->year, PHP_EOL; } //Return every robot as a Robots instance $robots->setHydrateMode(Resultset::HYDRATE_RECORDS); foreach ($robots as $robot) { echo $robot->year, PHP_EOL; }
<?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)));
<?php // Cache the files for 2 days using a Data frontend $frontCache = new Phalcon\Cache\Frontend\Data(array("lifetime" => 172800)); // Create the component that will cache "Data" to a "File" backend // Set the cache file directory - important to keep the "/" at the end of // of the value for the folder $cache = new Phalcon\Cache\Backend\File($frontCache, array("cacheDir" => "../app/cache/")); // Try to get cached records $cacheKey = 'robots_order_id.cache'; $robots = $cache->get($cacheKey); if ($robots === null) { // $robots is null due to cache expiration or data does not exist // Make the database call and populate the variable $robots = Robots::find(array("order" => "id")); // Store it in the cache $cache->save($cacheKey, $robots); } // Use $robots :) foreach ($robots as $robot) { echo $robot->name, "\n"; }
protected function _testCacheOtherService($di) { $di->set('otherCache', function () { $frontCache = new Phalcon\Cache\Frontend\Data(); return new Phalcon\Cache\Backend\File($frontCache, array('cacheDir' => 'unit-tests/cache/')); }, true); $robots = Robots::find(array('cache' => array('key' => 'other-some', 'lifetime' => 60, 'service' => 'otherCache'), 'order' => 'id')); $this->assertEquals(count($robots), 3); $this->assertTrue($robots->isFresh()); $robots = Robots::find(array('cache' => array('key' => 'other-some', 'lifetime' => 60, 'service' => 'otherCache'), 'order' => 'id')); $this->assertEquals(count($robots), 3); $this->assertFalse($robots->isFresh()); $this->assertEquals($robots->getCache()->getLastKey(), 'other-some'); $this->assertEquals($robots->getCache()->queryKeys(), array(0 => 'other-some')); }
<?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"; }
<?php // How many robots are there? $robots = Robots::find(); echo "There are ", count($robots), "\n"; // How many mechanical robots are there? $robots = Robots::find("type = 'mechanical'"); echo "There are ", count($robots), "\n"; // Get and print virtual robots ordered by name $robots = Robots::find(array("type = 'virtual'", "order" => "name")); foreach ($robots as $robot) { echo $robot->name, "\n"; } // Get first 100 virtual robots ordered by name $robots = Robots::find(array("type = 'virtual'", "order" => "name", "limit" => 100)); foreach ($robots as $robot) { echo $robot->name, "\n"; }
<?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();
<?php use Phalcon\Db\Column; //Bind parameters $parameters = array("name" => "Robotina", "year" => 2008); //Casting Types $types = array("name" => Column::BIND_PARAM_STR, "year" => Column::BIND_PARAM_INT); // Query robots binding parameters with string placeholders $conditions = "name = :name: AND year = :year:"; $robots = Robots::find(array($conditions, "bind" => $parameters, "bindTypes" => $types));
public function testResultsetNormalZero() { if (!$this->_prepareTestMssql()) { $this->markTestSkipped("Skipped"); return; } $robots = Robots::find('id > 1000'); $this->assertEquals(count($robots), 0); $this->assertEquals($robots->count(), 0); //Using a foreach $number = 0; foreach ($robots as $robot) { $number++; } $this->assertEquals($number, 0); //Using a while $number = 0; $robots->rewind(); while ($robots->valid()) { $robots->next(); $number++; } $this->assertEquals($number, 0); $robots->seek(1); $robots->valid(); $robot = $robots->current(); $this->assertFalse($robot); $robot = $robots->getFirst(); $this->assertFalse($robot); $robot = $robots->getLast(); $this->assertFalse($robot); try { $robot = $robots[0]; $this->assertFalse(true); } catch (Exception $e) { $this->assertEquals($e->getMessage(), 'The index does not exist in the cursor'); } try { $robot = $robots[2]; $this->assertFalse(true); } catch (Exception $e) { $this->assertEquals($e->getMessage(), 'The index does not exist in the cursor'); } $this->assertFalse(isset($robots[0])); }
<?php use Phalcon\Mvc\Model\Resultset; $robots = Robots::find(array('hydration' => Resultset::HYDRATE_ARRAYS)); foreach ($robots as $robot) { echo $robot['year'], PHP_EOL; }
<?php echo Phalcon\Tag::select(array("robotId", Robots::find("type = 'mechanical'"), "using" => array("id", "name")));
<?php $robot = Robots::findFirst(array("type = 'virtual'", "order" => "name DESC", "limit" => 30)); $robots = Robots::find(array("conditions" => "type = ?1", "bind" => array(1 => "virtual")));
<?php use Phalcon\Db\Column; //Bind parameters $parameters = array("name" => "Robotina", "year" => 2008); //Casting Types $types = array("name" => Column::BIND_PARAM_STR, "year" => Column::BIND_PARAM_INT); // Query robots binding parameters with string placeholders $robots = Robots::find(array("name = :name: AND year = :year:", "bind" => $parameters, "bindTypes" => $types));
<?php $robots = Robots::find(array(array("type" => "mechanical"))); foreach ($robots as $robot) { 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 //Using a standard foreach $robots = Robots::find(array("type='virtual'", "order" => "name")); foreach ($robots as $robot) { echo $robot->name, "\n"; } //Using a while $robots = Robots::find(array("type='virtual'", "order" => "name")); $robots->rewind(); while ($robots->valid()) { $robot = $robots->current(); echo $robot->name, "\n"; $robots->next(); }