responsiv / uploader-plugin

Collection of File Upload components for October
MIT License
62 stars 39 forks source link

Call to a member function avatar() on null #12

Open bkrajendra opened 8 years ago

bkrajendra commented 8 years ago

I'm getting error:

Call to a member function avatar() on null
H:\xampp\htdocs\sacs.org\cms_sacs\oas\plugins\Responsiv\Uploader\traits\ComponentUtils.php line 81

I'm using following code in my component :

    public function init()
    {
        $component = $this->addComponent(
            'Responsiv\Uploader\Components\ImageUploader',
            'imageUploader',
            ['deferredBinding' => false]
        );

        $component->bindModel('avatar', $this->user);
    }

Ive also tried in Page Init method but its showing same error.

alxy commented 8 years ago

This is when $this->user is not defined, e.g. if no user is logged in. You need to make sure you always pass a User object there, otherwise you will get an error. You can use this workaround, if you cant ensure you always have a logged in user:

$user = !is_null($this->user) ? $this->user : new User;
bkrajendra commented 8 years ago

Got it working as follows: Make sure this component is placed by code initiation not by dragging it on page.. A better way is to add it dynamically by using following code on onInit of code section of a page. Assuming we want to add avatar/profile pic for account page, add :

{% component 'imageUploader' %}

anywhere you want to put upload box. For example in my case I've added above line in update.htm partial (overridden) of user component. This will display upload box in account page above update account form.

After this add following code to account page code section:

function onInit ()
{
    $user = Auth::getUser();
    if($user){
        $component = $this->addComponent(
            'NetSTI\Uploader\Components\ImageUploader',
            'imageUploader',
            ['modelClass'=>'RainLab\User\Models\User','modelKeyColumn'=>'avatar', 'deferredBinding' => false]
        );

        $component->bindModel('avatar', $user);
    }
}

Here modelClass is the class in which to relate picture (avatar) and modelKeyColumn is the column name to which this pic to relate.

gw2princeps commented 3 years ago

@bkrajendra Your code snippet did not fix the issue for me. Anyone got something else that works?