Skip to content

PreparedTypedQuery

sweiguny edited this page Sep 23, 2014 · 3 revisions

A PreparedTypedQuery gives you the opportunity to retrieve data. The PreparedTypedQuery provides result sets containing your entity-objects. Similar to other queries, it uses SQL.

$query = new PreparedTypedQuery("SELECT * FROM `role` WHERE id = ?", "\\PPA\\core\\entity\\Role");

The code above just instantiates a PreparedTypedQuery object and passes the query-string as well as the fully qalified classname to the constructor. On calling the constructor the query is actually prepared! Also the kind of query is analyzed (select, update, delete or insert).

Note: A PreparedTypedQuery only provides select statements. On others it will throw a DomainException.

A PreparedTypedQuery provides two public methods:

  • getSingleResult(array $values)
  • getResultList(array $values)

The behavior of both methods depends on the kind of query. On calling one of these methods the query is actually executed on the database with the values passed to the method. The sequence of the values inside the array has to correspond to the sequence of ? within the query string.


Examples:

$query = new PreparedTypedQuery("SELECT * FROM `role` WHERE id = ?", "\\PPA\\examples\\entity\\Role");
$role = $query->getSingleResult(array(1));

Returns:
PPA\examples\entity\Role Object
(
    [id:PPA\examples\entity\Role:private] => 1
    [name:PPA\examples\entity\Role:private] => admin
    [rights:PPA\examples\entity\Role:private] => Array
        (
            ...
        )

)

getResultList()

This method returns an array with objects, that inherit from \PPA\core\Entity - in contrast to getSingleResult(). The objects are ones of the class, that was passed to the constructor. The indexes of the array correspond to the primary values of the entities.


See also:

Clone this wiki locally