summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--application/classes/controller/home.php4
-rw-r--r--application/classes/model/fairy.php4
-rw-r--r--application/classes/model/tree.php4
-rw-r--r--application/config/database.php2
-rw-r--r--modules/database/classes/db.php2
-rw-r--r--modules/database/classes/driver/pdo/db.php21
-rw-r--r--modules/orm/classes/orm.php65
-rw-r--r--modules/orm/classes/ormresult.php3
8 files changed, 89 insertions, 16 deletions
diff --git a/application/classes/controller/home.php b/application/classes/controller/home.php
index 2e353a0..3b9dbf3 100644
--- a/application/classes/controller/home.php
+++ b/application/classes/controller/home.php
@@ -2,9 +2,7 @@
class Home_Controller extends Controller {
public function action_index(){
- $view = View::get('home');
- $view->message = 'Have fun coding!';
- $this->response->body=$view->render();
+ print_r(ORM::factory('fairy')->with('tree')->find_all()->as_array(true));
}
}
diff --git a/application/classes/model/fairy.php b/application/classes/model/fairy.php
new file mode 100644
index 0000000..31a942a
--- /dev/null
+++ b/application/classes/model/fairy.php
@@ -0,0 +1,4 @@
+<?php
+class Fairy_Model extends ORM{
+ public $belongs_to=array('tree');
+} \ No newline at end of file
diff --git a/application/classes/model/tree.php b/application/classes/model/tree.php
new file mode 100644
index 0000000..0c4973d
--- /dev/null
+++ b/application/classes/model/tree.php
@@ -0,0 +1,4 @@
+<?php
+class Tree_Model extends ORM{
+ public $has_many=array('fairies');
+} \ No newline at end of file
diff --git a/application/config/database.php b/application/config/database.php
index 3a04f14..0295f13 100644
--- a/application/config/database.php
+++ b/application/config/database.php
@@ -4,7 +4,7 @@ return array(
'default' => array(
'user'=>'root',
'password' => '',
- 'driver' => 'mysql',
+ 'driver' => 'pdo',
//'Connection' is required if you use the PDO driver
'connection'=>'mysql:host=localhost;dbname=phpixie',
diff --git a/modules/database/classes/db.php b/modules/database/classes/db.php
index 854b06f..dc6934f 100644
--- a/modules/database/classes/db.php
+++ b/modules/database/classes/db.php
@@ -44,6 +44,8 @@ abstract class DB {
* @access public
*/
public abstract function get_insert_id();
+
+ public abstract function list_columns($table);
/**
* Executes a named query where parameters are passed as an associative array
diff --git a/modules/database/classes/driver/pdo/db.php b/modules/database/classes/driver/pdo/db.php
index fa02e63..c73995d 100644
--- a/modules/database/classes/driver/pdo/db.php
+++ b/modules/database/classes/driver/pdo/db.php
@@ -60,7 +60,26 @@ class DB_PDO_Driver extends DB{
return $this->execute('SELECT lastval() as id')->current()->id;
return $this->conn->lastInsertId();
}
-
+
+ public function list_columns($table) {
+ $columns=array();
+ if ($this->db_type == 'mysql') {
+ $table_desc = $this->execute("DESCRIBE `$table`");
+ foreach($table_desc as $column)
+ $columns[]=$column->Field;
+ }
+ if ($this->db_type == 'pgsql') {
+ $table_desc = $this->execute("select column_name from information_schema.columns where table_name = '{$table}' and table_catalog=current_database();");
+ foreach($table_desc as $column)
+ $columns[]=$column->column_name;
+ }
+ if ($this->db_type == 'sqlite') {
+ $table_desc = $this->execute("PRAGMA table_info('$table')");
+ foreach($table_desc as $column)
+ $columns[]=$column->name;
+ }
+ return $columns;
+ }
/**
* Executes a prepared statement query
*
diff --git a/modules/orm/classes/orm.php b/modules/orm/classes/orm.php
index fa69870..8089ea5 100644
--- a/modules/orm/classes/orm.php
+++ b/modules/orm/classes/orm.php
@@ -67,9 +67,9 @@ class ORM {
/**
* An instance of the database connection
* @var DB
- * @access private
+ * @access protected
*/
- private $db;
+ protected $db;
/**
* Current row returned by the database
@@ -88,9 +88,9 @@ class ORM {
/**
* A flag whether the row was loaded from the database
* @var boolean
- * @access private
+ * @access protected
*/
- private $_loaded = false;
+ protected $_loaded = false;
/**
* The name of the model
@@ -102,9 +102,11 @@ class ORM {
/**
* Cached properties
* @var array
- * @access private
+ * @access protected
*/
- private $_cached = array();
+ protected $_cached = array();
+
+ protected static $_column_cache = array();
/**
* Constructs the model. To use ORM it is enough to
@@ -150,7 +152,9 @@ class ORM {
if ($rels == 'has_many' && isset($rel['through']))
if (!isset($rel['foreign_key']))
- $normalized[$key]['foreign_key']=$rel['model'].'_id';
+ $normalized[$key]['foreign_key'] = $rel['model'].'_id';
+
+ $normalized[$key]['name']=$key;
}
$this->$rels=$normalized;
@@ -184,7 +188,34 @@ class ORM {
* @access public
*/
public function find_all() {
- return new ORMResult(get_class($this), $res=$this->query->execute());
+
+ $relations = array();
+ $model_alias = $this->query->last_alias();
+ $fields=array("{$model_alias}.*");
+ foreach($this->_with as $rel) {
+
+ $model = ORM::factory($rel['model']);
+ $alias = $model->query->add_alias();
+ $relations[$rel['name']] = $alias;
+
+ if ($rel['type'] == 'belongs_to') {
+ $this->query->join(array($model->table, $alias), array(
+ $model_alias.'.'.$rel['key'],
+ $alias.'.'.$model->id_field,
+ ),'left');
+ }else {
+ $this->query->join(array($model->table, $alias), array(
+ $model_alias.'.'.$this->id_field,
+ $alias.'.'.$rel['key'],
+ ), 'left');
+ }
+ $fields[]="{$alias}.*";
+ }
+
+ $this->query->fields($fields);
+ print_r($this->query->query());
+ die;
+ return new ORMResult(get_class($this), $res=$this->query->execute(),$model_alias,$relations);
}
/**
@@ -431,7 +462,7 @@ class ORM {
array($rel['key'],$this->_row[$this->id_field]),
array($rel['foreign_key'],$model->_row[$model->id_field])
))
- ->execute();
+ ->execute();
}else {
$key=$rel['key'];
$model->$key = null;
@@ -439,7 +470,21 @@ class ORM {
}
$this->_cached=array();
}
-
+
+ protected $_with = array();
+ public function columns() {
+ if (!isset(ORM::$_column_cache[$this->table]))
+ ORM::$_column_cache[$this->table] = array_keys(DB::instance($this->connection)->list_columns($this->table));
+ return ORM::$_column_cache[$this->table];
+ }
+ public function with($relation) {
+ $rels = array_merge($this->has_one, $this->has_many,$this->belongs_to);
+ $rel = Misc::arr($rels, $relation, false);
+ if (!$rel)
+ throw new Exception("Model doesn't have a '{$relation}' relation defined");
+ $this->_with[] = $rel;
+ return $this;
+ }
/**
* Deletes current item from the database
*
diff --git a/modules/orm/classes/ormresult.php b/modules/orm/classes/ormresult.php
index 39553ef..381f37c 100644
--- a/modules/orm/classes/ormresult.php
+++ b/modules/orm/classes/ormresult.php
@@ -34,9 +34,10 @@ class ORMResult implements Iterator {
* @return void
* @access public
*/
- public function __construct($model,$dbresult){
+ public function __construct($model,$dbresult,$relations=array()){
$this->_model=$model;
$this->_dbresult = $dbresult;
+ print_r($this->_dbresult->current());die;
}
/**