public function testNumberOfQueriesForLoadingUserArticles() { $sqlLogger = new SqlLogger(); //$sqlLogger->setEchoOutput(true); self::$rm->getConnection()->setSqlLogger($sqlLogger); $userTable = self::$rm->getTable('user'); /** @var \Dive\Collection\RecordCollection|User[] $users */ $users = $userTable->createQuery('u')->leftJoin('u.Author au')->where('au.id IS NOT NULL')->execute(); $this->assertEquals(count(self::$tableRows['author']), $users->count()); foreach ($users as $user) { if ($author = $user->Author) { $author->Article; } } foreach ($users as $user) { if ($author = $user->Author) { $author->Article; } } $sqlLogger->setEchoOutput(false); $this->assertEquals(3, $sqlLogger->getCount()); }
public function commitChanges() { $this->checkRestrictOnCommit(); $this->validateScheduledSaveRecords(); $conn = $this->recordManager->getConnection(); $conn->beginTransaction(); try { $this->processChanges(); $conn->commit(); } catch (\PDOException $e) { $conn->rollBack(); throw $e; } $this->resetScheduled(); }
/** * @param array $definition * @return string */ protected function getLeftJoinByDefinition(array $definition) { $conn = $this->rm->getConnection(); $quote = $conn->getIdentifierQuote(); if (!isset($definition['onClause'])) { $parentAlias = $definition['parent']; $alias = $definition['alias']; $relationName = $definition['relation']; $parentTableName = $this->queryComponents[$parentAlias]['table']; $parentTable = $this->rm->getTable($parentTableName); $relation = $parentTable->getRelation($relationName); $onClause = $relation->getJoinOnCondition($relationName, $parentAlias, $alias, $quote); if (isset($definition['withClause'])) { $onClause .= ' AND ' . $definition['withClause']; } } else { $onClause = $definition['onClause']; } return $conn->quoteIdentifier($definition['table']) . ' ' . $conn->quoteIdentifier($definition['alias']) . ' ON ' . $onClause; }
public function testGetConnection() { $this->assertInstanceOf('Dive\\Connection\\Connection', $this->rm->getConnection()); }
/** * @param RecordManager $rm * @param Relation $relation * @return string */ protected function insertRequiredLocalRelationGraph(RecordManager $rm, Relation $relation) { $randomGenerator = new FieldValuesGenerator(); $conn = $rm->getConnection(); $refTableName = $relation->getReferencedTable(); $refTable = $rm->getTable($refTableName); $refFields = $refTable->getFields(); $data = array(); // recursion: walk through all local relations that are required and handle this by calling this method with // next relation $owningRelations = $refTable->getReferencedRelationsIndexedByOwningField(); foreach ($owningRelations as $owningField => $owningRelation) { // check if field is required (default of matchType) and insert required related data if ($randomGenerator->matchType($refFields[$owningField])) { $data[$owningField] = $this->insertRequiredLocalRelationGraph($rm, $owningRelation); } } // insert a record and return its id $data = $randomGenerator->getRandomRecordData($refFields, $data); $conn->insert($refTable, $data); return $conn->getLastInsertId($refTableName); }
/** * @param RecordManager $rm */ private function addPreFieldValueChangeEventListener(RecordManager $rm) { $fieldValueChangeListener = function (FieldValueChangeEvent $event) { /** @var Record $record */ $record = $event->getRecord(); $record->markFieldAsModified($event->getProperty()); }; // clean event dispatcher with no listeners $rm->getConnection()->setEventDispatcher(new EventDispatcher()); $eventDispatcher = $rm->getEventDispatcher(); $eventDispatcher->addListener(Record::EVENT_PRE_FIELD_VALUE_CHANGE, $fieldValueChangeListener); }
/** * Gets connection belonging to the table * * @return Connection */ public function getConnection() { return $this->rm->getConnection(); }