-
Notifications
You must be signed in to change notification settings - Fork 0
TypedQuery
A TypedQuery
gives you the opportunity to retrieve data from the database in a more advanced way than the normal Query
. As the normal query mostly provides POPOs
, the TypedQuery
provides result sets containing your entity-objects. Similar to the normal Query
, it uses SQL
.
$query = new TypedQuery("SELECT * FROM `role` WHERE id = 1", "\\PPA\\core\\entity\\Role");
The code above just instantiates a TypedQuery
object and passes the query-string as well as the fully qalified classname to the constructor. It has yet not been executed, but the TypedQuery
analyzes what kind of query had been passed (select, update, delete or insert).
Note: A TypedQuery
only provides select statements. On others it will throw a DomainException
.
A TypedQuery
provides two public methods:
- getSingleResult()
- getResultList()
On calling one of these methods the query is actually executed on the database.
Basically it will return objects, that inherit from \PPA\core\Entity
. This inheritance is necessary, because otherwise PPA
cannot work as designated.
If the query execution resulted in more than one row, this method will return the first row as an object of the class, that was passed to the constructor. If the execution resulted in just one row, then this one row is returned as object of this certain class as well.
Example:
$query = new TypedQuery("SELECT * FROM `role` WHERE id = 2", "\\PPA\\examples\\entity\\Role");
$query->getSingleResult();
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
(
[3] => PPA\examples\entity\Right Object
(
[id:PPA\examples\entity\Right:private] => 3
[desc:PPA\examples\entity\Right:private] => ch-pw
)
[1] => PPA\examples\entity\Right Object
(
[id:PPA\examples\entity\Right:private] => 1
[desc:PPA\examples\entity\Right:private] => login
)
[2] => PPA\examples\entity\Right Object
(
[id:PPA\examples\entity\Right:private] => 2
[desc:PPA\examples\entity\Right:private] => logout
)
)
)
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.
Example:
$query = new TypedQuery("SELECT * FROM `role`", "\\PPA\\examples\\entity\\Role");
$query->getResultList();
Returns:
Array
(
[1] => 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
(
...
)
)
[2] => PPA\examples\entity\Role Object
(
[id:PPA\examples\entity\Role:private] => 2
[name:PPA\examples\entity\Role:private] => user
[rights:PPA\examples\entity\Role:private] => Array
(
...
)
)
[4] => PPA\examples\entity\Role Object
(
[id:PPA\examples\entity\Role:private] => 4
[name:PPA\examples\entity\Role:private] => sales
[rights:PPA\examples\entity\Role:private] => Array
(
...
)
)
)
See also: