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()); }
/** * 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); } }
/** * 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(); }
public function __construct($pheasant = null) { $this->_pheasant = $pheasant ?: \Pheasant::instance(); }