Skip to content

Commit

Permalink
support load from args
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Feb 6, 2012
1 parent f845a71 commit 9473627
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
23 changes: 19 additions & 4 deletions src/Lazy/BaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ class BaseModel

protected $_data;



public function createQuery()
{
$q = new QueryBuilder();
Expand All @@ -36,7 +34,7 @@ public function createExecutiveQuery()



public function load($kVal)
public function _load($kVal)
{
$key = $this->_schema->primaryKey;
$column = $this->_schema->getColumn( $key );
Expand Down Expand Up @@ -385,6 +383,7 @@ public function __call($m,$a)
case 'create':
case 'delete':
case 'update':
case 'load':
return call_user_func_array(array($this,'_' . $m),$a);
break;
}
Expand Down Expand Up @@ -435,7 +434,23 @@ public static function __static_delete()

public static function __static_load($args)
{

$model = new static;
if( is_array($args) ) {
$q = $model->createExecutiveQuery();
$q->callback = function($b,$sql) use ($model) {
$stm = $model->dbQuery($sql);
$record = $stm->fetchObject( get_class($model) );
$record->deflate();
return $record;
};
$q->limit(1);
$q->whereFromArgs($args);
return $q->execute();
}
else {
$model->load($args);
return $model;
}
}

public function deflateHash( & $args)
Expand Down
10 changes: 9 additions & 1 deletion tests/Lazy/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,15 @@ function testSqlite()
));
ok( $record->_result->success );


$record = \tests\Author::load( (int) $record->_result->id );
ok( $record );
ok( $id = $record->id );

$record = \tests\Author::load( array(
'id' => $id
));
ok( $record );
ok( $record->id );


/**
Expand Down

0 comments on commit 9473627

Please sign in to comment.