/** * Constructor Method * * @param Spot_Mapper * @param string $entityName Name of the entity to query on/for */ public function __construct(\Spot\Mapper $mapper) { $this->_mapper = $mapper; $this->_entityName = $mapper->entity(); $this->_tableName = $mapper->table(); // Create Doctrine DBAL query builder from Doctrine\DBAL\Connection $this->_queryBuilder = $mapper->connection()->createQueryBuilder(); }
/** * {@inheritdoc} */ public function findById($speakerId) { $speaker = $this->mapper->get($speakerId); if (false === $speaker) { throw new EntityNotFoundException(); } return $speaker; }
/** * POST /oauth/clients */ public function registerClient(Request $request) { $clientIdentifier = $this->generator->generate(40); $clientSecret = $this->generator->generate(40); try { $client = $this->clients->create(['id' => $clientIdentifier, 'secret' => $clientSecret, 'name' => $request->get('name')]); foreach ($request->get('redirect_uris') as $uri) { $this->endpoints->create(['client_id' => $clientIdentifier, 'redirect_uri' => $uri]); } return $this->respond($client->toArrayForApi()); } catch (\Exception $e) { return $this->respondBadRequest(); } }
/** * SPL Countable function * Called automatically when attribute is used in a 'count()' function call * Caches results when there are no query changes * * @return int * @todo - this is broken */ public function count() { return count($this->execute()); $that = $this; // New scope with closure to get only PUBLIC properties of object instance (can't include cache property) $cacheParams = function () use($that) { // This trick doesn't seem to work by itself in PHP 5.4... $props = get_object_vars($that); // Depends on protected/private properties starting with underscore ('_') $publics = array_filter(array_keys($props), function ($key) { return strpos($key, '_') !== 0; }); return array_intersect_key($props, array_flip($publics)); }; $cacheKey = sha1(var_export($cacheParams(), true)) . "_count"; $cacheResult = isset($this->cache[$cacheKey]) ? $this->cache[$cacheKey] : false; // Check cache if ($cacheResult) { $result = $cacheResult; } else { // Execute query $result = $this->mapper->getDi()->get($this->mapper->getAdapterName())->count($this); // Set cache $this->cache[$cacheKey] = $result; } return is_numeric($result) ? $result : 0; }
/** * Escape/quote identifier * * @param string $identifier * @return string */ public function escapeIdentifier($identifier) { if ($this->_noQuote) { return $identifier; } return $this->mapper->connection()->quoteIdentifier(trim($identifier)); }
/** * Insert one new record using the Entity class. * * @param $insertedEntities * @return string */ public function execute($insertedEntities) { $obj = $this->mapper->build([]); $this->fillColumns($obj, $insertedEntities); $this->callMethods($obj, $insertedEntities); $this->mapper->insert($obj); return $obj; }
/** * Get field name with table alias appended * @param string $field * @param bool $escaped * @return string */ public function fieldWithAlias($field, $escaped = true) { $fieldInfo = $this->_mapper->entityManager()->fields(); // Determine real field name (column alias support) if (isset($fieldInfo[$field])) { $field = $fieldInfo[$field]['column']; } $field = $this->_tableName . '.' . $field; return $escaped ? $this->escapeIdentifier($field) : $field; }
/** * Drop Table * * @param string $table Table name * @return bool */ public function dropTable($table) { $result = false; $connection = $this->mapper->connection(); try { $result = $connection->getSchemaManager()->dropTable($table); } catch (\Exception $e) { $result = false; } return $result; }
/** * Add foreign keys from BelongsTo relations to the table schema * @param Table $table * @return Table */ protected function addForeignKeys(Table $table) { $entityName = $this->mapper->entity(); $entity = new $entityName(); $relations = $entityName::relations($this->mapper, $entity); $fields = $this->mapper->entityManager()->fields(); foreach ($relations as $relationName => $relation) { if ($relation instanceof BelongsTo) { $fieldInfo = $fields[$relation->localKey()]; if ($fieldInfo['foreignkey'] === false) { continue; } $foreignTableMapper = $relation->mapper()->getMapper($relation->entityName()); $foreignTable = $foreignTableMapper->table(); $foreignSchemaManager = $foreignTableMapper->connection()->getSchemaManager(); $foreignTableObject = $foreignSchemaManager->listTableDetails($foreignTable); $foreignTableColumns = $foreignTableObject->getColumns(); $foreignTableNotExists = empty($foreignTableColumns); $foreignKeyNotExists = !array_key_exists($relation->foreignKey(), $foreignTableColumns); // We need to use the is_a() function because the there is some inconsistency in entity names (leading slash) $notRecursiveForeignKey = !is_a($entity, $relation->entityName()); /* Migrate foreign table if: * - the foreign table not exists * - the foreign key not exists * - the foreign table is not the same as the current table (recursion check) * This migration eliminates the 'Integrity constraint violation' error */ if (($foreignTableNotExists || $foreignKeyNotExists) && $notRecursiveForeignKey) { $foreignTableMapper->migrate(); } $onUpdate = !is_null($fieldInfo['onUpdate']) ? $fieldInfo['onUpdate'] : "CASCADE"; if (!is_null($fieldInfo['onDelete'])) { $onDelete = $fieldInfo['onDelete']; } else { if ($fieldInfo['notnull']) { $onDelete = "CASCADE"; } else { $onDelete = "SET NULL"; } } // Field alias support $fieldAliasMappings = $this->mapper->entityManager()->fieldAliasMappings(); if (isset($fieldAliasMappings[$relation->localKey()])) { $localKey = $fieldAliasMappings[$relation->localKey()]; } else { $localKey = $relation->localKey(); } $fkName = $this->mapper->table() . '_fk_' . $relationName; $table->addForeignKeyConstraint($foreignTable, [$localKey], [$relation->foreignKey()], ["onDelete" => $onDelete, "onUpdate" => $onUpdate], $fkName); } } return $table; }
/** * @param Talk $talk * * @return mixed */ public function persist(Talk $talk) { $this->mapper->save($talk); }
public static function relations(Mapper $mapper, Entity $entity) { return ['event' => $mapper->belongsTo($entity, 'SpotTest\\Entity\\Event', 'event_id')]; }
public static function relations(Mapper $mapper, Entity $entity) { return ['tags' => $mapper->hasManyThrough($entity, 'SpotTest\\Entity\\Tag', 'SpotTest\\Entity\\PostTag', 'tag_id', 'post_id'), 'comments' => $mapper->hasMany($entity, 'SpotTest\\Entity\\Post\\Comment', 'post_id')->order(['date_created' => 'ASC']), 'author' => $mapper->belongsTo($entity, 'SpotTest\\Entity\\Author', 'author_id')]; }
public function hookUpdateSearchIndex(\Spot\Mapper $mapper) { $result = $mapper->upsert('Entity_Event_Search', array('event_id' => $this->id, 'body' => $this->title . ' ' . $this->description), array('event_id' => $this->id)); }
public static function relations(Mapper $mapper, Entity $entity) { return ['search' => $mapper->hasOne($entity, 'SpotTest\\Entity\\Event\\Search', 'event_id')]; }
public static function relations(Mapper $mapper, Entity $entity) { return ['post' => $mapper->belongsTo($entity, 'SpotTest\\Entity\\Post', 'post_id'), 'tag' => $mapper->belongsTo($entity, 'SpotTest\\Entity\\Tag', 'tag_id')]; }
public static function relations(Mapper $mapper, Entity $entity) { return ['posts' => $mapper->hasManyThrough($entity, 'SpotTest\\Entity\\Post', 'SpotTest\\Entity\\PostTag', 'tag_id', 'post_id')]; }