Skip to content

Instantly share code, notes, and snippets.

@chanmix51
Last active September 28, 2015 03:48
Show Gist options
  • Save chanmix51/1380301 to your computer and use it in GitHub Desktop.
Save chanmix51/1380301 to your computer and use it in GitHub Desktop.
Overloading the selectFields method in your model
<?php
$loader = require __DIR__."/../vendor/.composer/autoload.php";
$loader->add('YourDb', "/tmp"); // Set yours
use YourDb\Blog\Post;
use YourDb\Blog\PostMap;
$database = new Pomm\Connection\Database(array('dsn' => 'pgsql://user:password@host:port/db_name'));
$posts = $database
->getConnection()
->getMapFor('YourDb\Blog\Post')
->findAll('post')
;
foreach($posts as $post)
{
printf("Title: %s, posted %s days ago.\n", $post->getTitle(), $post->getCreatedSince());
}
<?php
namespace YourDb\Blog;
use YourDb\Blog\Base\PostMap as BasePostMap;
use Pomm\Exception\Exception;
use Pomm\Query\Where;
class PostMap extends BasePostMap
{
public function getSelectFields($alias = null)
{
$fields = parent::getSelectFields($alias);
$db_alias = is_null($alias) ? '' : sprintf('%s.', $alias);
$fields['created_since'] = sprintf("date_part('d', now() - %screated_at)", $db_alias)
return $fields;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment