Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
bkrajendra opened this issue Feb 19, 2016 · 3 comments
Open

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

bkrajendra opened this issue Feb 19, 2016 · 3 comments

Comments

@bkrajendra
Copy link

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
Copy link
Contributor

alxy commented Feb 25, 2016

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
Copy link
Author

bkrajendra commented Jun 6, 2016

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants