Last active
September 28, 2015 03:48
-
-
Save chanmix51/1380301 to your computer and use it in GitHub Desktop.
Overloading the selectFields method in your model
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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()); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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