Ejemplo n.º 1
0
 public function setUp()
 {
     $this->connection = DB::getConnection('testdb');
     $this->driver = $this->connection->getDriver();
     // Clean up events before every test
     $this->triggeredEvents = array();
     $this->triggeredArguments = array();
     $that = $this;
     foreach ($this->queryEvents as $event) {
         Event::removeListeners($event);
         Event::on($event, function () use($event, $that) {
             $that->triggeredEvents[] = $event;
             $that->triggeredArguments[] = func_get_args();
         });
     }
 }
Ejemplo n.º 2
0
<?php

/**
 * Demonstrates using QuerySets to read data.
 */
// Include Phormium and models
require __DIR__ . "/../vendor/autoload.php";
require __DIR__ . "/models/Person.php";
// Configure Phormium
\Phormium\DB::configure('config.json');
// A separator for cosmetic outuput
define('SEPARATOR', str_repeat('-', 50) . "\n");
/**
 * QuerySet is an object returned by Model::objects() which allows querying of
 * data in various ways.
 *
 * The simplest operation is to fetch all records from a table.
 */
/**
 * The simplest operation is to fetch all records from a table.
 */
$persons = Person::objects()->fetch();
echo SEPARATOR . "The person table has " . count($persons) . " records.\n";
/**
 * To limit the output, the results can be filtered.
 */
$persons = Person::objects()->filter('salary', '>', 5000)->fetch();
echo SEPARATOR . "The person table has " . count($persons) . " records with salary over 5000.\n";
/**
 * Note that filter() will return a new instance of QuerySet with the given
 * filter added to it, this allows chaining.
Ejemplo n.º 3
0
 public static function setUpBeforeClass()
 {
     DB::configure(PHORMIUM_CONFIG_FILE);
 }
Ejemplo n.º 4
0
 /**
  * Prepares, then executes a statement and returns the number of affected
  * rows.
  *
  * The method is useful for updates or deletes, which do
  * not return anything.
  *
  * @param string $query The SQL query to execute.
  * @param array $arguments The arguments used to substitute params.
  * @return integer Number of rows affected by the query.
  */
 public function preparedExecute($query, $arguments = array())
 {
     DB::getConnection($this->name);
     // Handles transactions
     Event::emit(Event::QUERY_STARTED, array($query, $arguments, $this));
     $stmt = $this->pdoPrepare($query, $arguments);
     $this->pdoExecute($query, $arguments, $stmt);
     Event::emit(Event::QUERY_COMPLETED, array($query, $arguments, $this, null));
     return $stmt->rowCount();
 }
Ejemplo n.º 5
0
 /** Performs a prepared query and returns only a single column. */
 private function singleColumnQuery($query, $args, $column)
 {
     $conn = DB::getConnection($this->meta->database);
     $pdo = $conn->getPDO();
     $stmt = $pdo->prepare($query);
     $stmt->execute($args);
     $data = array();
     while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
         $data[] = $row[$column];
     }
     return $data;
 }
Ejemplo n.º 6
0
 /**
  * @expectedException PHPUnit_Framework_Error_Warning
  * @expectedExceptionMessage Attribute PDO::ATTR_ERRMODE is set to something other than PDO::ERRMODE_EXCEPTION for database "db1". This is not allowed because Phormium depends on this setting. Skipping attribute definition.
  */
 public function testAttributesCannotChangeError()
 {
     DB::configure(["databases" => ["db1" => ["dsn" => "sqlite:tmp/test.db", "username" => "myuser", "password" => "mypass", "attributes" => [PDO::ATTR_ERRMODE => PDO::ERRMODE_SILENT]]]]);
     // Test the warning is emitted
     DB::getConnection("db1");
 }
Ejemplo n.º 7
0
 public function testDoubleBegin()
 {
     DB::begin();
     try {
         DB::begin();
         $this->fail('Expected an exception here.');
     } catch (\Exception $e) {
         $this->assertContains("Already in transaction.", $e->getMessage());
     }
     DB::rollback();
 }
Ejemplo n.º 8
0
 /**
  * @expectedException \Exception
  */
 public function testFileExistsButIsADirectory()
 {
     $config = __DIR__;
     @DB::configure($config);
 }
Ejemplo n.º 9
0
 /**
  * Configures database definitions.
  *
  * @param string|array $config Either a path to the JSON encoded
  *      configuration file, or the configuration as an array.
  */
 public static function configure($config)
 {
     DB::disconnectAll();
     Config::load($config);
 }
Ejemplo n.º 10
0
 public static function setUpBeforeClass()
 {
     DB::configure(PHORMIUM_CONFIG_FILE);
     self::$person = Person::fromArray(['name' => 'Udo Dirkschneider']);
     self::$person->save();
 }