public function get($object, $key, $cache = null) { if ($cache) { $schema = \Pheasant::instance()->schema($this->class); if ($cached = $cache->get($schema->hash($object, array(array($this->local, $this->foreign))))) { return $cached; } } if (($localValue = $object->{$this->local}) === null) { if ($this->_allowEmpty) { return null; } else { throw new \Pheasant\Exception("Local value is null while not allowed"); } } $result = $this->query("{$this->foreign}=?", $localValue)->execute(); if (!count($result)) { if ($this->_allowEmpty) { return null; } else { throw new \Pheasant\Exception("Failed to find a {$key} (via {$this->foreign}={$localValue})"); } } return $this->hydrate($result->row()); }
public function setUp() { $dsn = getenv('PHEASANT_TEST_DSN') ?: 'mysql://root@localhost/pheasanttest?charset=utf8'; // initialize a new pheasant $this->pheasant = \Pheasant::setup($dsn); // wipe sequence pool $this->pheasant->connection()->sequencePool()->initialize()->clear(); }
/** * Acquire the lock on the object * @return object the reloaded object */ public function acquire() { if (!$this->_object->isSaved()) { throw new LockingException("Can't lock unsaved objects"); } $finder = \Pheasant::instance()->finderFor($this->_object); return $freshObject = $finder->find($this->_object->className(), $this->_object->identity()->toCriteria())->lock($this->_clause)->one(); }
public function loadCache() { $this->_cache = new ArrayCache(); $ids = iterator_to_array($this->_query->select('DISTINCT ' . $this->_rel->local)->execute()->column()); $relatedObjects = \Pheasant::instance()->finderFor($this->_rel->class)->find($this->_rel->class, new Criteria($this->_rel->foreign . '=?', array($ids)))->includes($this->_nested); foreach ($relatedObjects as $obj) { $this->_cache->add($obj); } }
public function testObjectTransactionNotExecuting() { $this->assertCount(0, Animal::findByType('llama')); $t = \Pheasant::transaction(function () { $animal = new Animal(array('type' => 'llama')); $animal->save(); }, false); $this->assertCount(0, Animal::findByType('llama')); $t->execute(); $this->assertCount(1, Animal::findByType('llama')); }
/** * Adds a join clause to the given query for the given schema and relationship. Optionally * takes a nested list of relationships that will be recursively joined as needed. * @return void */ public static function addJoin($query, $parentAlias, $schema, $relName, $nested = array(), $joinType = 'inner') { if (!in_array($joinType, array('inner', 'left', 'right'))) { throw new \InvalidArgumentException("Unsupported join type: {$joinType}"); } list($relName, $alias) = self::parseRelName($relName); $rel = $schema->relationship($relName); // look up schema and table for both sides of join $instance = \Pheasant::instance(); $localTable = $instance->mapperFor($schema->className())->table(); $remoteSchema = $instance->schema($rel->class); $remoteTable = $instance->mapperFor($rel->class)->table(); $joinMethod = $joinType . 'Join'; $query->{$joinMethod}($remoteTable->name()->table, sprintf('ON `%s`.`%s`=`%s`.`%s`', $parentAlias, $rel->local, $alias, $rel->foreign), $alias); foreach (self::normalizeMap($nested) as $relName => $nested) { self::addJoin($query, $alias, $remoteSchema, $relName, $nested, $joinType); } }
<?php require_once __DIR__ . '/common.php'; // set up the database $migrator = new \Pheasant\Migrate\Migrator(); $migrator->create('testobject', TestObject::schema()); $migrator->create('testrelationship', TestRelationship::schema()); $connection = \Pheasant::instance()->connection(); $connection->sequencePool()->initialize()->clear(); $connection->table('testobject')->truncate(); $connection->table('testrelationship')->truncate();
/** * Reloads the contents of the object */ public function reload() { $fresh = \Pheasant::instance()->finderFor($this)->find($this->className(), $this->identity()->toCriteria())->one(); $this->_data = $fresh->_data; return $this; }
/** * Helper to build Wizard * @return Wizard */ public static function fromClass($className) { return new self(\Pheasant::instance()->schema($className), \Pheasant::instance()->finderFor($className)); }
/** * Constructor */ public function __construct($connection = null) { $this->_connection = $connection ?: \Pheasant::instance()->connection(); $this->_events = new \Pheasant\Events(); }
<?php require_once __DIR__ . '/../vendor/autoload.php'; use Pheasant\Types; $memory_peak = 0; declare (ticks=35); register_tick_function('log_peak_memory'); Pheasant::setup('mysql://root@localhost:/pheasanttest'); /** * Log peak memory usage */ function log_peak_memory() { global $memory_peak; if (($usage = memory_get_usage()) > $memory_peak) { $memory_peak = $usage; } } /** * Generic benchmarking function */ function benchmark($times, $callback) { global $memory_peak; $counts = array(); $memory = array(); $params = array_slice(func_get_args(), 2); // warm-up the benchmark for ($i = 0; $i < 5; $i++) { call_user_func_array($callback, $params); }
/** * Resets the default static Pheasant instance */ public static function reset($instance) { return self::$_instance = $instance; }
public function __construct($pheasant = null) { $this->_pheasant = $pheasant ?: \Pheasant::instance(); }