Skip to content

Instantly share code, notes, and snippets.

@phalcon
Created November 17, 2012 17:04
Show Gist options
  • Save phalcon/4097695 to your computer and use it in GitHub Desktop.
Save phalcon/4097695 to your computer and use it in GitHub Desktop.
<?php
namespace Knode\Dao\Sql;
$di = new \Phalcon\Di\FactoryDefault();
$di->set('db', function(){
$config = array(
"host" => "localhost",
"username" => "phalcon",
"password" => "secret",
"dbname" => "invo"
);
// Create a connection
$connection = new \Phalcon\Db\Adapter\Pdo\Mysql($config);
return $connection;
});
abstract class Orm extends \Phalcon\Mvc\Model
{
/**
* @static
* @param string $alias
* @return \Phalcon\Db\Adapter\Pdo\Mysql
*/
public static function conn($alias = 'db')
{
try {
if ($conn = \Phalcon\DI::getDefault()->getShared($alias, $parameters = array())) {
return $conn;
}
throw new Exception("Error connecting to DataStore ({$alias})");
} catch (\Exception $e) {
self::logger()->error($e);
throw new Exception($e->getMessage(), Exception::DAO_ERROR_CODE, $e);
}
}
/**
* @static
* @param array $options
* @return \ArrayObject|\Knode\Model[]
*/
public static function findAll($options = array())
{
/*
// this works which is weird same query as static::find()
$sql = "SELECT id,username,password,email,first_name,last_name,is_active,last_seen_ip,last_seen_at,created_a t,updated_at FROM users";
$result = self::conn()->query($sql);
$result->setFetchMode(\Phalcon\Db::FETCH_ASSOC);
echo '<pre>';
while ($row = $result->fetchArray()) {
print_r($row);
}
print_r(self::conn());
die;
*/
$result = static::find($options);
$out = new \ArrayObject();
foreach ($result as $daoModel) {
$out[] = static::loadModel($daoModel);
}
return $out;
}
/**
* ¿?
*/
public static function loadModel($x){
return $x;
}
}
class User extends Orm
{
/**
* Set table name.
*
* @return string
*/
public function getSource()
{
return 'users';
}
}
foreach(User::findAll() as $user){
echo $user->name, PHP_EOL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment