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

Model::getAttribute() doesn't work when a method has the same name as attribute #1156

Open
ng1091 opened this issue Mar 18, 2017 · 1 comment

Comments

@ng1091
Copy link

ng1091 commented Mar 18, 2017

I created a Model like this:

class Article extends Moloquent
{
    // this class has an attribute like
    protected $fillable = ['like'];

    // and a static method like
    public static function like($article_id, $user_id) {
        // ...
        $article = static::find($article_id);
        $article->increment('like'); // here doesn't work
    }
}

Jenssegers\Mongodb\Eloquent\Model;
line 125:

    public function getAttribute($key)
    {
        if (! $key) {
            return;
        }

        // Dot notation support.
        if (str_contains($key, '.') and array_has($this->attributes, $key)) {
            return $this->getAttributeValue($key);
        }

        // This checks for embedded relation support.
        if (method_exists($this, $key) and ! method_exists(self::class, $key)) {
            return $this->getRelationValue($key);
        }

        return parent::getAttribute($key);
    }

The problem is method_exists($this, $key) returns true and try to getRelationValue(), but Article::like() is a static method, so it will crash.

@GromNaN
Copy link
Member

GromNaN commented Aug 28, 2023

Should be fixed by #2577

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

2 participants