Created
June 29, 2011 13:20
-
-
Save michaelwills/1053807 to your computer and use it in GitHub Desktop.
Extending a redbeanphp model to handle instantiation with IDE code hints
This file contains 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 | |
// in BaseSimpleModel.php | |
class BaseSimpleModel extends RedBean_SimpleModel { | |
/** | |
* Get or create a model with the bean set | |
* @static | |
* @param string $table | |
* @param int $id | |
* @return \BaseSimpleModel | |
*/ | |
public static function get($table = '', $id = 0) { | |
$item = new self(); | |
$item->bean = R::loadOrDispense($table, $id); | |
return $item; | |
} | |
public function save() { | |
R::store($this->bean); | |
} | |
public function export() { | |
return $this->bean->export(); | |
} | |
public function trash() { | |
R::trash($this->bean); | |
unset($this->bean); | |
$this->log("trashed"); | |
} | |
public function update() { | |
$this->log("update"); | |
} | |
public function log($msg) { | |
echo "$msg\n"; | |
} | |
} | |
// in Model_Item.php | |
/** | |
* @property int id | |
* @property string name | |
* @property string description | |
*/ | |
class Model_Item extends BaseSimpleModel { | |
public static $table_name = 'item'; | |
/** | |
* @static | |
* @param int $id | |
* @return Model_Item | |
*/ | |
public static function get($id = 0) { | |
return parent::get(self::$table_name, $id); | |
} | |
} | |
// usage | |
$item = Model_Item::get(); | |
$item->name = "Foo"; | |
$item->description = "Bar"; | |
$item->save(); | |
var_dump($item->export()); | |
$id = $item->id; | |
$new_item = Model_Item::get($id); | |
var_dump($new_item->export()); | |
$new_item->trash(); | |
var_dump($new_item); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment