/** * @param \App\Model\Entities\Listing $listing * @param array $workedHours * @param array $localities * @return void * @throws Exception */ protected function generateItems(\App\Model\Entities\Listing $listing, array $workedHours, array $localities) { dump($workedHours); $c = count($workedHours); if ($c != count($localities)) { throw new Exception('Wrong amount of array items'); } $itemsData = []; for ($i = 0; $i < $c; $i++) { $itemsData['listingID'][] = $listing->listingID; $wh = $this->setupWorkedHours(new InvoiceTime($workedHours[$i][0]), new InvoiceTime($workedHours[$i][1]), new InvoiceTime($workedHours[$i][2]), new InvoiceTime($workedHours[$i][3])); $itemsData['workedHoursID'][] = $wh->workedHoursID; $loc = $this->setupLocality($localities[$i]); $itemsData['localityID'][] = $loc->localityID; $itemsData['day'][] = $i + 1; $itemsData['description'][] = null; $itemsData['descOtherHours'][] = isset($workedHours[$i]['descOtherHours']) ? $workedHours[$i]['descOtherHours'] : null; /*$item = new \App\Model\Entities\ListingItem(); $item->assign($arr); $items[] = $item;*/ } $this->connection->query('INSERT INTO [listing_item] %m', $itemsData); /*$id = $this->connection->getInsertId(); // first inserted ID foreach ($items as $item) { $this->makeEntityAlive($item, $id); $id++; } return $items;*/ }
/** * Persists changes in M:N relationships * * @param Entity $entity */ protected function persistHasManyChanges(Entity $entity) { $primaryKey = $this->mapper->getPrimaryKey($this->getTable()); $idField = $this->mapper->getEntityField($this->getTable(), $primaryKey); foreach ($entity->getHasManyRowDifferences() as $key => $difference) { list($columnReferencingSourceTable, $relationshipTable, $columnReferencingTargetTable) = explode(':', $key); $multiInsert = []; foreach ($difference as $value => $count) { if ($count > 0) { for ($i = 0; $i < $count; $i++) { $multiInsert[] = [$columnReferencingSourceTable => $entity->{$idField}, $columnReferencingTargetTable => $value]; } } else { $this->connection->query('DELETE FROM %n WHERE %n = ? AND %n = ? %lmt', $relationshipTable, $columnReferencingSourceTable, $entity->{$idField}, $columnReferencingTargetTable, $value, -$count); } } if (!empty($multiInsert)) { $this->connection->query('INSERT INTO %n %ex', $relationshipTable, $multiInsert); } } }
/** * @param string $table * @param string $viaColumn * @param Filtering|null $filtering * @param string $strategy * @throws InvalidArgumentException * @throws InvalidStateException * @return self */ private function getReferencingResult($table, $viaColumn = null, Filtering $filtering = null, $strategy = self::STRATEGY_IN) { $strategy = $this->translateStrategy($strategy); if ($this->isDetached) { throw new InvalidStateException('Cannot get referencing Result for detached Result.'); } if ($viaColumn === null) { $viaColumn = $this->mapper->getRelationshipColumn($table, $this->table); } $key = "{$table}({$viaColumn}){$strategy}"; if (isset($this->referencing[$preloadedKey = $key . '#' . self::PRELOADED_KEY])) { return $this->referencing[$preloadedKey]; } $primaryKey = $this->mapper->getPrimaryKey($this->table); if ($strategy === self::STRATEGY_IN) { if ($filtering === null) { if (!isset($this->referencing[$key])) { $ids = $this->extractIds($primaryKey); $statement = $this->createTableSelection($table, $ids); if ($this->isAlias($viaColumn)) { $statement->where('%n IN %in', $this->trimAlias($viaColumn), $ids); } else { $statement->where('%n.%n IN %in', $table, $viaColumn, $ids); } $this->referencing[$key] = self::createInstance($statement->fetchAll(), $table, $this->connection, $this->mapper, $key); } } else { $ids = $this->extractIds($primaryKey); $statement = $this->createTableSelection($table, $ids); if ($this->isAlias($viaColumn)) { $statement->where('%n IN %in', $this->trimAlias($viaColumn), $ids); } else { $statement->where('%n.%n IN %in', $table, $viaColumn, $ids); } $this->applyFiltering($statement, $filtering); $args = $statement->_export(); $key .= '#' . $this->calculateArgumentsHash($args); if (!isset($this->referencing[$key])) { $this->referencing[$key] = self::createInstance($this->connection->query($args)->fetchAll(), $table, $this->connection, $this->mapper, $key); } } return $this->referencing[$key]; } // $strategy === self::STRATEGY_UNION if ($filtering === null) { if (!isset($this->referencing[$key])) { $ids = $this->extractIds($primaryKey); if (count($ids) === 0) { $data = array(); } else { $data = $this->connection->query($this->buildUnionStrategySql($ids, $table, $viaColumn))->fetchAll(); } $this->referencing[$key] = self::createInstance($data, $table, $this->connection, $this->mapper, $key); } } else { $ids = $this->extractIds($primaryKey); if (count($ids) === 0) { $this->referencing[$key] = self::createInstance(array(), $table, $this->connection, $this->mapper, $key); } else { $firstStatement = $this->createTableSelection($table, array(reset($ids))); if ($this->isAlias($viaColumn)) { $firstStatement->where('%n = ?', $this->trimAlias($viaColumn), reset($ids)); } else { $firstStatement->where('%n.%n = ?', $table, $viaColumn, reset($ids)); } $this->applyFiltering($firstStatement, $filtering); $args = $firstStatement->_export(); $key .= '#' . $this->calculateArgumentsHash($args); if (!isset($this->referencing[$key])) { $sql = $this->buildUnionStrategySql($ids, $table, $viaColumn, $filtering); $this->referencing[$key] = self::createInstance($this->connection->query($sql)->fetchAll(), $table, $this->connection, $this->mapper, $key); } } } return $this->referencing[$key]; }
/** * @param string $table * @param string $viaColumn * @param Filtering|null $filtering * @param string $strategy * @throws InvalidArgumentException * @throws InvalidStateException * @return self */ private function getReferencingResult($table, $viaColumn = null, Filtering $filtering = null, $strategy = self::STRATEGY_IN) { $strategy = $this->translateStrategy($strategy); if ($this->isDetached) { throw new InvalidStateException('Cannot get referencing Result for detached Result.'); } if ($viaColumn === null) { $viaColumn = $this->mapper->getRelationshipColumn($table, $this->table); } $key = "{$table}({$viaColumn}){$strategy}"; if (isset($this->referencing[$forcedKey = $key . '#' . self::KEY_FORCED])) { $ids = $this->extractIds($this->mapper->getPrimaryKey($this->table)); foreach ($this->referencing[$forcedKey] as $filteringResult) { if ($filteringResult->isValidFor($ids, $filtering->getArgs())) { return $filteringResult->getResult(); } } } if (isset($this->referencing[$preloadedKey = $key . '#' . self::KEY_PRELOADED])) { return $this->referencing[$preloadedKey]; } if ($strategy === self::STRATEGY_IN) { if ($filtering === null) { if (!isset($this->referencing[$key])) { isset($ids) or $ids = $this->extractIds($this->mapper->getPrimaryKey($this->table)); $statement = $this->createTableSelection($table, $ids); if ($this->isAlias($viaColumn)) { $statement->where('%n IN %in', $this->trimAlias($viaColumn), $ids); } else { $statement->where('%n.%n IN %in', $table, $viaColumn, $ids); } $data = $statement->execute()->setRowClass(null)->fetchAll(); $this->referencing[$key] = self::createInstance($data, $table, $this->connection, $this->mapper); } } else { isset($ids) or $ids = $this->extractIds($this->mapper->getPrimaryKey($this->table)); $statement = $this->createTableSelection($table, $ids); if ($this->isAlias($viaColumn)) { $statement->where('%n IN %in', $this->trimAlias($viaColumn), $ids); } else { $statement->where('%n.%n IN %in', $table, $viaColumn, $ids); } $filteringResult = $this->applyFiltering($statement, $filtering); if ($filteringResult instanceof FilteringResultDecorator) { if (!isset($this->referencing[$forcedKey])) { $this->referencing[$forcedKey] = []; } $this->referencing[$forcedKey][] = $filteringResult; return $filteringResult->getResult(); } $args = $statement->_export(); $key .= '#' . $this->calculateArgumentsHash($args); if (!isset($this->referencing[$key])) { $data = $this->connection->query($args)->setRowClass(null)->fetchAll(); $this->referencing[$key] = self::createInstance($data, $table, $this->connection, $this->mapper); } } return $this->referencing[$key]; } // $strategy === self::STRATEGY_UNION if ($filtering === null) { if (!isset($this->referencing[$key])) { isset($ids) or $ids = $this->extractIds($this->mapper->getPrimaryKey($this->table)); if (count($ids) === 0) { $data = []; } else { $data = $this->connection->query($this->buildUnionStrategySql($ids, $table, $viaColumn))->setRowClass(null)->fetchAll(); } $this->referencing[$key] = self::createInstance($data, $table, $this->connection, $this->mapper); } } else { isset($ids) or $ids = $this->extractIds($this->mapper->getPrimaryKey($this->table)); if (count($ids) === 0) { $this->referencing[$key] = self::createInstance([], $table, $this->connection, $this->mapper); } else { $firstStatement = $this->createTableSelection($table, [reset($ids)]); if ($this->isAlias($viaColumn)) { $firstStatement->where('%n = ?', $this->trimAlias($viaColumn), reset($ids)); } else { $firstStatement->where('%n.%n = ?', $table, $viaColumn, reset($ids)); } $filteringResult = $this->applyFiltering($firstStatement, $filtering); if ($filteringResult instanceof FilteringResultDecorator) { if (!isset($this->referencing[$forcedKey])) { $this->referencing[$forcedKey] = []; } $this->referencing[$forcedKey][] = $filteringResult; return $filteringResult->getResult(); } $args = $firstStatement->_export(); $key .= '#' . $this->calculateArgumentsHash($args); if (!isset($this->referencing[$key])) { $sql = $this->buildUnionStrategySql($ids, $table, $viaColumn, $filtering); $data = $this->connection->query($sql)->setRowClass(null)->fetchAll(); $result = self::createInstance($data, $table, $this->connection, $this->mapper); $this->referencing[$key] = $result; } } } return $this->referencing[$key]; }