Skip to content

Instantly share code, notes, and snippets.

@viccherubini
Created July 19, 2010 11:04
Show Gist options
  • Select an option

  • Save viccherubini/481278 to your computer and use it in GitHub Desktop.

Select an option

Save viccherubini/481278 to your computer and use it in GitHub Desktop.
<?php
/**
* Return a single entity object.
*/
$statement = $pdo->prepare('SELECT * FROM product WHERE product_id = ?');
$objectifier->setEntity(new Product);
$product1 = $objectifier->fetch($statement, array(10));
$product2 = $objectifier->fetch($statement, array(11));
/**
* Return an Iterator of objects.
*/
$statement = $pdo->prepare('SELECT * FROM category ORDER BY sortorder DESC');
$objectifier->setEntity(new Category);
$categoryIterator = $objectifier->fetchIterator($statement);
/**
* Complex query that returns a single object, will only return the rows defined in each Entity.
*/
$statement = $pdo->prepare('SELECT * FROM product p INNER JOIN product_image pi ON p.product_id = pi.product_id WHERE p.product_id = ?');
$objectifier->setEntity(new Product);
$product1 = $objectifier->fetch($statement, array(10));
$product2 = $objectifier->fetch($statement, array(11));
// Immediately switch to fetching a Product\Image object
$objectifier->setEntity(new Product\Image);
$productImage1 = $objectifier->fetch($statement, array(10));
// When no entity is set, returns an Entity class with no defaults set, assume the calls to setEntity() above don't exist
$entity = $objectifier->fetch($statement, array(10));
/**
* Insert a new Entity
*/
$product = new Product;
$product->setName('product name')
->setPrice(10.94)
->setQuantity(100)
->setSku('PROD-123');
$product = $objectifier->save($product); // The value returned now has the id() of the newly inserted product.
/**
* Update the $product from the previous insert
*/
$product->setName('updated product name');
$objectifier->save($product);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment