src/Charcoal/Loader/CollectionLoader.php
public function loadFromQuery($query, callable $callback = null, callable $before = null)
    {
        $db = $this->source()->db();

        if (!$db) {
            throw new RuntimeException(
                'Could not instanciate a database connection.'
            );
        }

        /** @todo Filter binds */
        if (is_string($query)) {
            $this->logger->debug($query);
            $sth = $db->prepare($query);
            $sth->execute();
        } elseif (is_array($query)) {
            list($query, $binds, $types) = array_pad($query, 3, []);
            $sth = $this->source()->dbQuery($query, $binds, $types);
        } else {
            throw new InvalidArgumentException(sprintf(
                'The SQL query must be a string or an array: '.
                '[ string $query, array $binds, array $dataTypes ]; '.
                'received %s',
                is_object($query) ? get_class($query) : $query
            ));
        }

        $sth->setFetchMode(PDO::FETCH_ASSOC);

        if ($callback === null) {
            $callback = $this->callback();
        }

        return $this->processCollection($sth, $before, $callback);
    }
public function loadFromQuery($query, callable $callback = null, callable $before = null)
    {
        $db = $this->source()->db();

        if (!$db) {
            throw new RuntimeException(
                'Could not instanciate a database connection.'
            );
        }

        /** @todo Filter binds */
        if (is_string($query)) {
            $this->logger->debug($query);
            $sth = $db->prepare($query);
            $sth->execute();
        } elseif (is_array($query)) {
            list($query, $binds, $types) = array_pad($query, 3, []);
            $sth = $this->source()->dbQuery($query, $binds, $types);
        } else {
            throw new InvalidArgumentException(sprintf(
                'The SQL query must be a string or an array: '.
                '[ string $query, array $binds, array $dataTypes ]; '.
                'received %s',
                is_object($query) ? get_class($query) : $query
            ));
        }

        $sth->setFetchMode(PDO::FETCH_ASSOC);

        if ($callback === null) {
            $callback = $this->callback();
        }

        return $this->processCollection($sth, $before, $callback);
    }
public function loadFromQuery ( $ query , callable $ callback = null , callable $ before = null ) { $ db = $ this -> source ( ) -> db ( ) ; if ( ! $ db ) { throw new RuntimeException ( 'Could not instanciate a database connection.' ) ; } /** @todo Filter binds */ if ( is_string ( $ query ) ) { $ this -> logger -> debug ( $ query ) ; $ sth = $ db -> prepare ( $ query ) ; $ sth -> execute ( ) ; } elseif ( is_array ( $ query ) ) { list ( $ query , $ binds , $ types ) = array_pad ( $ query , 3 , [ ] ) ; $ sth = $ this -> source ( ) -> dbQuery ( $ query , $ binds , $ types ) ; } else { throw new InvalidArgumentException ( sprintf ( 'The SQL query must be a string or an array: ' . '[ string $query, array $binds, array $dataTypes ]; ' . 'received %s' , is_object ( $ query ) ? get_class ( $ query ) : $ query ) ) ; } $ sth -> setFetchMode ( PDO :: FETCH_ASSOC ) ; if ( $ callback === null ) { $ callback = $ this -> callback ( ) ; } return $ this -> processCollection ( $ sth , $ before , $ callback ) ; }
Load list from query.

**Example — Binding values to $query**

```php
$this->loadFromQuery([
'SELECT name, colour, calories FROM fruit WHERE calories < :calories AND colour = :colour',
[
'calories' => 150,
'colour'   => 'red'
],
[ 'calories' => PDO::PARAM_INT ]
]);
```

@param  string|array  $query    The SQL query as a string or an array composed of the query,
parameter binds, and types of parameter bindings.
@param  callable|null $callback Process each entity after applying raw data.
Leave blank to use {@see CollectionLoader::callback()}.
@param  callable|null $before   Process each entity before applying raw data.
@throws RuntimeException If the database connection fails.
@throws InvalidArgumentException If the SQL string/set is invalid.
@return ModelInterface[]|ArrayAccess
Load list from query .