/** * read $columns from $table_name * @param array $columns * @param array $filters * @param array $limit * @param array $order_by * @param string $return_type * @return array: */ public function read(array $columns = [], array $filters = [], array $limit = [], array $order_by = [], $return_type = self::READ_RETURN_COMPLEX) { $return = []; $alias_mapping = []; $qb = $this->conn->createQueryBuilder(); if (empty($columns)) { $columns = $this->getColumnNames(); } foreach ($columns as $column) { $column_name = null; $alias = null; $expr = []; if (is_string($column)) { $column_name = $alias = $column; if (strpos($alias, ':') !== false) { $alias = explode(':', $alias); $alias = array_pop($alias); } } elseif (is_array($column)) { list($column_name, $alias) = $column; } if (strpos($column_name, ':') !== false) { $expr = explode(':', $column_name); $column_name = array_pop($expr); } $alias_mapping[$alias] = $column_name; if ($this->getColumn($column_name) === null) { throw Schema\SchemaException::columnDoesNotExist($column_name, $this->table_name); } $column_expr = $this->conn->quoteIdentifier($column_name); $expr = array_reverse($expr); foreach ($expr as $ex) { if (in_array($ex, ['max', 'min', 'avg', 'count', 'sum', 'lower', 'upper'])) { $column_expr = call_user_func([$this->conn->getDatabasePlatform(), 'get' . ucfirst($ex) . 'Expression'], $column_expr); } else { throw QueryBuilderException::expressionTypeDoesNotExist($ex); } } if ($column_name !== $alias || !empty($expr)) { $column_expr .= ' AS ' . $this->conn->quoteIdentifier($alias); } $qb->addSelect($column_expr); } $qb->from($this->quoted_table_name); $this->buildWhere($qb, $filters)->buildOrderBy($qb, $order_by)->buildLimit($qb, $limit); $res = $qb->execute(); $return = $res->fetchAll(\PDO::FETCH_ASSOC); if (in_array($return_type, [self::READ_RETURN_SIMPLE, self::READ_RETURN_COMPLEX])) { foreach ($return as $index => $row) { foreach ($row as $column => $value) { if ($return_type === self::READ_RETURN_SIMPLE && !$this->isSimpleType($alias_mapping[$column])) { continue; } $return[$index][$column] = $this->conn->convertToPHPValue($value, $this->column_types[$alias_mapping[$column]]); } } } return $return; }
/** * {@inheritdoc} */ public function execute(LoggerInterface $logger) { // update field itself $sql = "UPDATE oro_entity_config_index_value\n SET value = ?\n WHERE\n entity_id = (SELECT id FROM oro_entity_config WHERE class_name = ? LIMIT 1) AND\n field_id IS NULL AND\n scope = ? AND\n code = ?\n "; $parameters = [$this->value, $this->entityName, $this->scope, $this->code]; $statement = $this->connection->prepare($sql); $statement->execute($parameters); $this->logQuery($logger, $sql, $parameters); $logger->debug($sql); // update entity config cached data $sql = 'SELECT data FROM oro_entity_config WHERE class_name = ? LIMIT 1'; $parameters = [$this->entityName]; $data = $this->connection->fetchColumn($sql, $parameters); $this->logQuery($logger, $sql, $parameters); $data = $data ? $this->connection->convertToPHPValue($data, Type::TARRAY) : []; $data[$this->scope][$this->code] = $this->value; $data = $this->connection->convertToDatabaseValue($data, Type::TARRAY); $sql = 'UPDATE oro_entity_config SET data = ? WHERE class_name = ?'; $parameters = [$data, $this->entityName]; $statement = $this->connection->prepare($sql); $statement->execute($parameters); $this->logQuery($logger, $sql, $parameters); }
/** * Select a single entity matching an SQL query. If more than one * row is matched by the query, only the first entity will be * returned. * * @param Connection $connection A connection instance * @param string $sql The SQL query * @param array $parameters Any bound parameters required for the query * @param array $field_mapping An array of result columns => entity fields * * @return Entity|null The selected Entity, or null if no entity was found */ public static function selectOneSQL(Connection $connection, $sql, array $parameters = [], array $field_mapping = []) { $stmt = $connection->prepare($sql); $stmt->execute($parameters); $result = $stmt->fetch(); if ($result) { foreach ($field_mapping as $result_column => $entity_field) { if (!isset($result[$result_column])) { continue; } $result[$entity_field] = $result[$result_column]; unset($result[$result_column]); } foreach (static::$types as $column => $type) { if (isset($result[$column])) { $result[$column] = $connection->convertToPHPValue($result[$column], $type); } } $entity = new static($connection, $result); return $entity->setStored(); } return null; }