Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID

Extended fields default values

So, in keeping with my running theme of core lib improvements, here's something that I think would be a useful addition to the CMS. This is an update to the ExtendedModel::__construct( ) method.

public function __construct ($vals = false, $is_new = true) {
    $defaults = [];
    foreach ($this->verify as $k => $v) {
        if (isset ($v['extended'])) {
            if (isset ($v['default'])) {
                $defaults[$k] = $v['default']
            unset ($v['extended']);
            $this->_extended_verify[$k] = $v;
            unset ($this->verify[$k]);

    // `_extended` is a special field to auto-populate extended attributes
    if (is_array ($vals) && isset ($vals['_extended'])) {
        $extended = $vals['_extended'];
        unset ($vals['_extended']);

    parent::__construct ($vals, $is_new);

    // Pre-populate extended attributes with given default values
    if (!empty($defaults)) {
        foreach ($defaults as $k => $v) {
            if (!isset($this->{$k})) $this->ext ($k, $v);
    // Populate extended attributes after `parent::__construct()`
    if (isset ($extended)) {
        foreach ($extended as $k => $v) {
            $this->ext ($k, $v);

It's a small update, but it allows for default values to be defined for extended values since the DB itself can't (for obvious reasons). Thoughts?


  • Just getting through all the updates and things one at a time. This is a nice and simple change. For reference, here's an example usage:

    namespace myapp;
    class Person extends \ExtendedModel {
        public $table = '#prefix#myapp_people';
        public $_extended_field = 'extra';
        public $verify = array (
            'favorite_food' => array (
                'extended' => true,
                'default' => 'pizza'

    Now when a new Person is instantiated, $p->ext ('favorite_food') will return pizza if nothing else is set.

    I made one small change just to support the old PHP array style (still keeping backward compatibility) and added a test as well:


  • Trouble with ExtendedModel defaults

    The defaults are having issue when using the Model::get($id); method and I can't figure out why.

    Model::query()->where(Model::$key,$id)->fetch(); and new Model($id); both work as expected. The get method doesn't overwrite the default values for some reason and I'm having a hard time figuring out why.

    It's supposed to instantiate the class and set any extended defaults, then overwrite them in the get method, but it just ends up displaying the defaults on the output whereas new Model($id); displays the right values in the same output.

  • Can you provide a bit of sample code that shows the issue?

  • namespace myapp;
    class MyModel extends \ExtendedModel { // id(int:sequence), user(int), ts(timestamp), content(text:extended(one:false, two:'Text', three:0, four))
        public $_extended_field = 'content';
        public $verify = array(
            'one' => array(
                'extended' => 1,
                'default' => false
            'two' => array(
                'extended' => 1,
                'defualt' => 'Text' 
            'three' => array(
                'extended' => 1,
                'default' => 0
            'four' => array(
                'extended' => 1
    $model = new MyModel(array(
        'user' => \User::val('id'),
        'ts' => gmdate ('Y-m-d H:i:s'),
        '_extended' => array(
            'one' => true,
            'two' => 'Words',
            'four' => 'something random, idk'
    echo 'MyModel::query()<br>'; // works, output database values
    echo var_export(MyModel::query()->where('id',$model->id)->single(), true);
    echo '<br><br>new MyModel()<br>'; // works, outputs database values
    echo var_export(new MyModel($model->id), true);
    echo '<br><br>MyModel::get()<br>'; // doesn't work, only outputs the default values for extended fields
    echo var_export(MyModel::get($model->id), true);

    the comment at the top is just a shorthand I use to remind me what the model schema is.

Sign In or Register to comment.