I use this base model, and have all of my objects inherit from it. The "cast" function makes the editor work correctly.
``` php
class My_RedBean_SimpleModel extends \RedBean_SimpleModel
{
public static function cast($instance): static
{
return $instance->box();
}
public function type()
{
return $this->unbox()->getMeta('type');
}
public function __construct()
{
#
https://stackoverflow.com/questions/41954415/cant-store-boxed-model-in-redbean foreach (array_keys(get_object_vars($this)) as $property) {
if ($property != 'bean')
unset($this->$property);
}
}
public static function _get_fake()
{
$table = model_table(get_called_class());
assert(!R::findOne($table));
$row = R::dispense($table);
foreach (get_class_vars(get_called_class()) as $name => $value) {
if ($value === null) {
# red($name . ', ' . $value);
continue;
} else if (in_array($name, ['id', 'bean'])) {
continue;
}
$row->$name = $value;
}
return $row;
}
}
const MEASUREMENTS = ['diameter' => '”', 'height' => '”'];
class Category extends My_RedBean_SimpleModel
{
public $name = "Example";
...
}
# example usage
$category = Category.cast($instance)
```
I think that Gabor may have included some part of this into RedBean proper.
The `_get_fake` methods isn't related, but, I use it to create single instances, to set up my tables during tests.