Skip to content

Commit

Permalink
Bug fixes to ORM and database.
Browse files Browse the repository at this point in the history
  • Loading branch information
xeoncross committed Apr 26, 2011
1 parent d6987e2 commit ed709cf
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 40 deletions.
25 changes: 22 additions & 3 deletions example/controller/school.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,31 @@ class Example_Controller_School extends Controller
public function action()
{
// Load database
$db = new DB(config('database'));
//$db = new DB(config('database'));

// You can also save a copy of the object in the
// registry in case something needs it later...
registry('db', $db);
//registry('db', $db);

// Set default database object for all models
ORM::$db = $db;
//ORM::$db = $db;

$this->load_database();

//$student = new Example_Model_Student();

//$pagination = new Pagination(NULL, NULL, NULL);
//$file = new Upload();
//$dir = new Dir();
//$form = new Form();
//$gd = new GD();
//$html = new HTML();
//registry();
//$time = new Time();
//$validation = new Validation();

//$this->content = 'loaded_database and created student';
//return;

// You can over-ride this in certain models if needed,
// allowing you to use multiple databases.
Expand Down Expand Up @@ -69,6 +86,8 @@ public function action()
$club->load();
foreach($club->students() as $student)
{
//$student->load();
//$this->content .= print dump($student);
$this->content .= dump('Removing '. $student->name. ' and her records');
$student->delete(); // Remove their car, club membership, and them
}
Expand Down
3 changes: 2 additions & 1 deletion example/model/student.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class Example_Model_Student extends ORM
{
public static $table = 'student';
public static $foreign_key = 'student_id';
public static $cascade_delete = TRUE;

public static $has = array(
'car' => 'Example_Model_Car',
Expand All @@ -15,6 +16,6 @@ class Example_Model_Student extends ORM
);

public static $has_many_through = array(
'clubs' => array('Model_Membership' => 'Example_Model_Club'),
'clubs' => array('Example_Model_Membership' => 'Example_Model_Club'),
);
}
6 changes: 3 additions & 3 deletions system/db.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ public function update($table, $data, array $where = NULL)
* @param array $order array of order by conditions
* @return array
*/
public function select($column, $table, $where = array(), $limit = NULL, $offset = 0, $order = array())
public function select($column, $table, $where = NULL, $limit = NULL, $offset = 0, $order = NULL)
{
$i = $this->i;

Expand Down Expand Up @@ -300,7 +300,7 @@ public function select($column, $table, $where = array(), $limit = NULL, $offset
* @param array $where array of column => $value indexes
* @return array
*/
public function where(array $where = NULL)
public function where($where = NULL)
{
$a = $s = array();

Expand Down Expand Up @@ -334,7 +334,7 @@ public function where(array $where = NULL)
*
* @param array $fields to order by
*/
public function order_by(array $fields = NULL)
public function order_by($fields = NULL)
{
if($fields)
{
Expand Down
6 changes: 5 additions & 1 deletion system/form.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Form extends View
* @param string $view
* @return string
*/
public function __construct($validation = NULL, array $attributes = array(), $view = 'form')
public function __construct($validation = NULL, array $attributes = NULL, $view = 'form')
{
parent::__construct($view);
$this->set(array('attributes' => $attributes, 'validation' => $validation));
Expand Down Expand Up @@ -77,6 +77,10 @@ public function fields(array $fields)
*/
public function __toString()
{
if( ! $this->attributes)
{
$this->attributes = array();
}
return html::tag('form', parent::__toString(), $this->attributes + array('method' => 'post'));
}

Expand Down
34 changes: 27 additions & 7 deletions system/html.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,13 @@ public static function email($email)
* @param array $attributes the array of HTML attributes
* @return string
*/
static function ul_from_array(array $ul, array $attributes = array())
static function ul_from_array(array $ul, array $attributes = NULL)
{
if( ! $attributes)
{
$attributes = array();
}

$h = '';
foreach($ul as $k => $v)
{
Expand All @@ -85,9 +90,9 @@ static function ul_from_array(array $ul, array $attributes = array())
* @param array $attributes the tag's attribute list
* @return string
*/
public static function attributes(array $attributes = array())
public static function attributes(array $attributes = NULL)
{
if(! $attributes)return;
if( ! $attributes) return;

asort($attributes);
$h = '';
Expand All @@ -107,8 +112,13 @@ public static function attributes(array $attributes = array())
* @param array $attributes of additional tag settings
* @return string
*/
public static function tag($tag, $text = '', array $attributes = array())
public static function tag($tag, $text = '', array $attributes = NULL)
{
if( ! $attributes)
{
$attributes = array();
}

return"\n<$tag" . self::attributes($attributes) . ($text === 0 ? ' />' : ">$text</$tag>");
}

Expand All @@ -121,10 +131,14 @@ public static function tag($tag, $text = '', array $attributes = array())
* @param array $attributes of additional tag settings
* @return string
*/
public static function link($url, $text = '', array $attributes = array())
public static function link($url, $text = '', array $attributes = NULL)
{
if( ! $attributes)
{
$attributes = array();
}

return self::tag('a', $text, ($attributes+array('href' => site_url($url))));

}


Expand All @@ -137,7 +151,7 @@ public static function link($url, $text = '', array $attributes = array())
* @param array $attributes of additional tag settings
* @return string
*/
public static function select($name, array $options = array(), $selected = NULL, array $attributes = array())
public static function select($name, array $options = array(), $selected = NULL, array $attributes = NULL)
{
$h = '';
foreach($options as $k => $v)
Expand All @@ -152,6 +166,12 @@ public static function select($name, array $options = array(), $selected = NULL,

$h .= self::tag('option', $v, $a);
}

if( ! $attributes)
{
$attributes = array();
}

return self::tag('select', $h, $attributes+array('name' => $name));
}

Expand Down
56 changes: 31 additions & 25 deletions system/orm.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ class ORM
{

// object data, related, changed, loaded, saved
public $data = array(), $related = array(), $changed = array(), $loaded, $saved;
public $data, $related, $changed, $loaded, $saved;

public static $db;
public static $table;
public static $key = 'id';
public static $foreign_key;
public static $belongs_to = array();
public static $has = array(); // Has one/many
public static $has_many_through = array(); // Has many through
public static $order_by = array();
public static $belongs_to;
public static $has; // Has one/many
public static $has_many_through; // Has many through
public static $order_by;
public static $cache = 0;
public static $cascade_delete = FALSE;

Expand All @@ -41,6 +41,8 @@ class ORM
*/
public function __construct($id = 0)
{
$this->data = array();

if(! $id) return;

if(is_numeric($id))
Expand Down Expand Up @@ -82,28 +84,31 @@ public function to_array()
/**
* Set an array of values on this object
*
* @param string $a
* @return self
* @param array $values to set
* @return object
*/
public function set($values)
{
foreach($values as $k => $v) $this->__set($k, $v);
foreach($values as $key => $value)
{
$this->__set($key, $value);
}
return $this;
}


/**
* Set a propery of this object
*
* @param string $k name
* @param string $key name
* @param mixed $v value
*/
public function __set($k, $v)
public function __set($key, $value)
{
if(!array_key_exists($k, $this->data) OR $this->data[$k] !== $v)
if(!array_key_exists($key, $this->data) OR $this->data[$key] !== $value)
{
$this->data[$k] = $v;
$this->changed[$k] = $k;
$this->data[$key] = $value;
$this->changed[$key] = $key;
$this->saved = 0;
}
}
Expand All @@ -112,32 +117,33 @@ public function __set($k, $v)
/**
* Retive a property or 1-to-1 object relation
*
* @param string $k the column or relation name
* @param string $key the column or relation name
* @return mixed
*/
public function __get($k)
public function __get($key)
{
$this->load();
return array_key_exists($k, $this->data) ? $this->data[$k] : $this->related($k);
return array_key_exists($key, $this->data) ? $this->data[$key] : $this->related($key);
}


/**
* @see isset()
*/
public function __isset($k)
public function __isset($key)
{
if($this->load()) return (array_key_exists($k, $this->data) OR isset($this->related[$k]));
$this->load();
return array_key_exists($key, $this->data) OR isset($this->related[$key]);
}


/**
* @see unset()
*/
public function __unset($k)
public function __unset($key)
{
$this->load();
unset($this->data[$k], $this->changed[$k], $this->related[$k]);
unset($this->data[$key], $this->changed[$key], $this->related[$key]);
}


Expand All @@ -163,7 +169,7 @@ public function reload()
public function clear()
{
$this->data = $this->changed = $this->related = array();
$this->loaded = $this->saved = 0;
$this->loaded = $this->saved = FALSE;
}


Expand Down Expand Up @@ -319,7 +325,7 @@ public function __call($alias, $args)
* @param array $order by conditions
* @return array
*/
public static function objects($column = 0, $class = 0, $model = 0, $where = 0, $limit = 0, $offset = 0, $order = array())
public static function objects($column = NULL, $class = NULL, $model = NULL, $where = NULL, $limit = 0, $offset = 0, $order = NULL)
{
if($rows = self::select('fetch', $column, $model, $where, $limit, $offset, $order))
{
Expand All @@ -345,10 +351,10 @@ public static function objects($column = 0, $class = 0, $model = 0, $where = 0,
* @param array $order by conditions
* @return mixed
*/
public static function select($func, $column, $model = NULL, $where = array(), $limit = 0, $offset = 0, $order = array())
public static function select($func, $column, $model = NULL, $where = NULL, $limit = 0, $offset = 0, $order = NULL)
{
$model = $model ?: get_called_class();
$order = $order + static::$order_by;
$order = ($order ?: array()) + (static::$order_by ?: array());

// Count queries don't have offsets, limits, or order conditions
if($func != 'fetch')
Expand All @@ -372,7 +378,7 @@ public static function select($func, $column, $model = NULL, $where = array(), $
* @param int $offset filter
* @param array $order_by conditions
*/
public static function fetch(array $where = NULL, $limit = 0, $offset = 0, array $order_by = array())
public static function fetch(array $where = NULL, $limit = 0, $offset = 0, array $order_by = NULL)
{
return self::objects(static::$key, 0, 0, $where, $limit, $offset, $order_by);

Expand Down

0 comments on commit ed709cf

Please sign in to comment.