This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.
    matto
    
            
            
                    
                                            
        
    
        Inside my plugin I am trying to add a custom attribute but I got stucked with a basic example I copied from the documentation (https://octobercms.com/docs/plugin/extending#extending-user-model):
PostModel::extend(function($model) {
    $model->bindEvent('model.getAttribute', function($attribute, $value) {
        if ($attribute === 'foo') {
            return 'bar';
        }
    });
});
However caling a $post->foo (or rather post.foo) inside my twig template returns NULL.
What am I missing?
    mjauvin
    
            
            
                    
                                            
        
    
        You need to properly reference the model you want to extend...
use Rainlab\Blog\Models\Post;
class Plugin extends PluginBase
{
    public function boot()
    {
        Post::extend(function($model) {
            $model->bindEvent('model.getAttribute', function($attribute, $value) {
                if ($attribute === 'foo') {
                    return 'bar';
                }
            });
        });
    }
}
                    
    matto
    
            
            
                    
                                            
        
    
        Thank you for your reply.
I do have it correctly referenced, like that:
use RainLab\Blog\Models\Post as PostModel;
    mjauvin
    
            
            
                    
                                            
        
    
        I just realized, you also need to add that field to your model:
Post::extend(function($model) {
    $model->addDynamicProperty('foo', 'initial value');
}
                    1-4 of 4