Skip to content

[5.5] Model->getAttributes() override may cause inconsistency #20902

Closed

Description

@austenc
  • Laravel Version: 5.5.0
  • PHP Version: 7.1
  • Database Driver & Version: N/A

Description:

This change (specifically changing $this->attributes to $this->getAttributes()) is not backwards compatible with some packages / apps that may override the getAttributes() model method. If the attribute values aren't strings, the query builder can't bind them properly right here and you get an "Object could not be converted to string" message.

Such an example is the EloquentTrait class of the popular Stapler package.

I'm assuming the attributes are intended to always be strings? Regardless, I wanted to at least bring up the discussion that $this->attributes and $this->getAttributes() could yield different results if the method is overridden. I'm assuming this isn't too common, but it could be considered a breaking change for some.

Steps to Reproduce:

1. Override the getAttributes() method on a model to something like:

public function getAttributes()
{
    return array_merge(['something' => new StdClass], $this->attributes);
}

2. Call $model->save()

Activity

  1. changed the title [-]Model->getAttributes() override may cause inconsistency[/-] [+][5.5] Model->getAttributes() override may cause inconsistency[/+] on Aug 31, 2017
  2. added a commit that references this issue on Aug 31, 2017
    3904e1d
  3. themsaid commented on Sep 1, 2017

    @themsaid
    Member

    Closing as per Taylor's response, seems like the package itself needs to adapt to the change.

  4. austenc commented on Sep 1, 2017

    @austenc
    ContributorAuthor

    Yep, I figured this would probably be the case, but wanted to confirm with you guys. Thanks @themsaid!

  5. lintaba commented on Apr 9, 2018

    @lintaba

    have the same issue. this helped me:
    czim/laravel-paperclip@515a66b

  6. mostafaznv commented on Jun 10, 2018

    @mostafaznv

    refer to this answer, you can fix it with getDirty method

    CodeSleeve/laravel-stapler#64 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions