Skip to content

Instantly share code, notes, and snippets.

@viccherubini
Created June 12, 2010 12:48
Show Gist options
  • Save viccherubini/435702 to your computer and use it in GitHub Desktop.
Save viccherubini/435702 to your computer and use it in GitHub Desktop.
<?php
/**
* #########################
* DataModeler Public SQL Interface
* #########################
*/
/**
* SEARCHING
*/
// Find a single person regardless of how many rows are returned.
// Because where() wasn't called, assumes to use the pkey from the
// $object and the key in the $input_parameters that matches that key.
$person = $sql->find($object, $input_paramters);
// Find a single person regardless of how many rows are returned
// based on the where clause.
$person = $sql->where('age > :age AND height = :height')
->find($object, $input_parameters);
// Find a list of people in an iterator.
$people_list = $sql->where('id <> :id AND age > :age')
->findAll($object, $input_parameters);
/**
* INSERTING
*/
// Insert this object into the database. Will check that you can't
// insert fields that don't exist. Will check to see if $object already
// has field values.
$sql->insert($object);
// Insert this object into the database. Will check that you can't
// insert fields that don't exist. Will pull values from
// $input_parameters and override any values in $object.
$sql->insert($object, $input_parameters);
/**
* UPDATING
*/
// Update a Model object into the databse. Look to get the pkey and pkey
// value from the object itself to construct the WHERE clause.
$sql->update($object);
// Update a Model object into the databse. Ignore the pkey and pkey
// value from the object itself and use the second and third parameters
// to build the WHERE clause.
$sql->update($object, 'id = :id', $input_parameters);
/**
* QUERYING
*/
// Execute a raw, but prepared, query. Always returns an Iterator object
// of anonymous Model objects regardless of the number of rows matched.
$iterator = $sql->query("SELECT u.*, ua.first_name, ua.last_name
FROM user u
INNER JOIN user_address ua
ON u.user_id = ua.user_id
WHERE u.age = :age
AND ua.first_name <> :first_name
AND ua.last_name = :last_name
GROUP BY u.user_id
ORDER BY u.date_created", $input_parameters);
// Execute a raw, but prepared, query. Coerce the returned fields into a
// User object (which is a Model object) so that the fields returned
// match the possible fields of User. Returns an Iterator of User
// objects.
$user_iterator = $sql->query("SELECT u.*, ua.first_name, ua.last_name
FROM user u
INNER JOIN user_address ua
ON u.user_id = ua.user_id
WHERE u.age = :age
AND ua.first_name <> :first_name
AND ua.last_name = :last_name
GROUP BY u.user_id
ORDER BY u.date_created", $input_parameters, $user);
// Execute a raw, but prepared query. Coerce the returned fields into a
// User object (which is a Model object) so that the fields returned
// match the possible fields of User. Return the first matched row
// as a User object. queryFirst() will not support returning an
// anonymous object. Returns an empty User object if no rows are
// matched.
$user_found = $sql->queryFirst("SELECT u.*, ua.first_name, ua.last_name
FROM user u
INNER JOIN user_address ua
ON u.user_id = ua.user_id
WHERE u.age = :age
AND ua.first_name <> :first_name
AND ua.last_name = :last_name
GROUP BY u.user_id
ORDER BY u.date_created", $input_parameters, $user);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment