Skip to content
This repository has been archived by the owner on Feb 16, 2022. It is now read-only.

Class name must be a valid object or a string #26

Closed
psychonetic opened this issue Jan 4, 2018 · 12 comments
Closed

Class name must be a valid object or a string #26

psychonetic opened this issue Jan 4, 2018 · 12 comments

Comments

@psychonetic
Copy link
Contributor

psychonetic commented Jan 4, 2018

Hello,

if I try to create an entity, I get the following error:
Class name must be a valid object or a string

The error occurs inside Illuminate/Database/Eloquent/Concerns/HasRelationships at:

protected function newRelatedInstance($class)
   {
       //dd($class) gives null.
   return tap(new $class, function ($instance) {
           if (! $instance->getConnectionName()) {
               $instance->setConnection($this->connection);
           }
       });
   }

My code:

//attribute creation

app('rinvex.attributes.attribute')->create([
            'slug' => 'size',
            'name' => 'Product Size',
            'type' => 'Rinvex\Attributes\Models\Type\Varchar',
            'entities' => ['App\Models\Article'],
        ]);

//Article (yes, it uses the attributableTrait)

$article = Article::find(1);
        $article->size = "m";
        $article->save();

Any idea?
Laravel version is 5.5 and used attributes version is dev-develop 8bce023.

@Omranic
Copy link
Member

Omranic commented Jan 4, 2018

Can you confirm please what’s in your attribute_entity table? That would help troubleshoot this issue..

@psychonetic
Copy link
Contributor Author

psychonetic commented Jan 4, 2018

Here you go:

see
bildschirmfoto 2018-01-04 um 22 01 31

(saving in version 0.0.3 works as expected, but in the 0.0.3 branch I am unable to get the data as mentioned in #27 )

@tongkai-wzq
Copy link

the same problem to me !

@weusder
Copy link

weusder commented Jan 8, 2018

I'm also having the same problem

@tongkai-wzq
Copy link

tongkai-wzq commented Jan 9, 2018

please turn type to alias !

app('rinvex.attributes.attribute')->create([
'slug' => 'size',
'name' => 'Product Size',
'type' => 'varchar',
'entities' => ['App\Models\Article'],
]);

Array
(
[boolean] => Rinvex\Attributes\Models\Type\Boolean
[datetime] => Rinvex\Attributes\Models\Type\Datetime
[integer] => Rinvex\Attributes\Models\Type\Integer
[text] => Rinvex\Attributes\Models\Type\Text
[varchar] => Rinvex\Attributes\Models\Type\Varchar
)

@weusder
Copy link

weusder commented Jan 9, 2018

kfja85 thanks for the help, it worked!

@psychonetic
Copy link
Contributor Author

psychonetic commented Jan 9, 2018

@kfja85 Works perfectly!
Unfortunately I am still not able to get the data. Eg.
Article::with('size')->get()
Size is still null.

Did you manage this? @kfja85 @weusder

@Omranic Can you please update the documentation and include the changes?

@Omranic
Copy link
Member

Omranic commented Jan 9, 2018

@kfja85 @weusder Sorry for the delay, and yes that make sense now. Docs fixed to reflect the new type aliases at 102b930

@psychonetic I've done few tests again now & everything seems to be working fine. What you need to make sure of is the following:

  • attributes table has your new field
  • attribute_entity table has relation between your new field and your entity
  • attribute_varchar_values or any other values table (depending on your attribute type has your entity value

If all these three tables has the correct info, then you should retrieve the value. If not, then please screenshot or dump the three tables here and I'll try to go through it & see what's wrong..

@psychonetic
Copy link
Contributor Author

@Omranic That sounds good!

Yeah, I show you.

Attributes table:

bildschirmfoto 2018-01-09 um 16 53 24

Attributes entity table:

bildschirmfoto 2018-01-09 um 16 53 44

Attributes Varchar Table:

bildschirmfoto 2018-01-09 um 16 53 53

So everything should be fine in my opinion.

@Omranic
Copy link
Member

Omranic commented Jan 9, 2018

Seems to be fine, and I've reproduced the same data as yours & it worked like a charm. One last thing, if you've cache enabled please disable caching & check again and let me know if that makes any difference. It's good to flush app cache for the sake of troubleshooting..

@psychonetic
Copy link
Contributor Author

@Omranic I tried to clean the cache, but no change. Any further idea?
Maybe I will try to create a project later, only with your package and just laravel. Otherwise I don't know.

@kfja85 @weusder Do you the same problem or is it working as expected?

@Omranic
Copy link
Member

Omranic commented Jan 9, 2018

I assume that your App\Models\Article model already using Rinvex\Attributes\Traits\Attributable trait, and you're retrieving it normally like:

$article = \App\Models\Article::with(['size'])->find(1);
dd($article->size);

If that's what you're doing and you still get nothing, then I'm not sure it's something related to this package. It's working with or without any other packages without a problem, I replicated the same setup & even same table data and can't reproduce your issue. Make sure there's no other size attribute used by the same model or attached by any other package in someway, otherwise please try to reproduce in a new project and mention the steps on how can we reproduce the issue in order to troubleshoot it.

For now I'll close this issue as it's not confirmed as an issue, but feel free to open it again or open a new one for further assistance.

@Omranic Omranic closed this as completed Jan 9, 2018
Omranic added a commit that referenced this issue Feb 18, 2018
* release/v0.0.4: (56 commits)
  Update changelog
  Drop Laravel 5.5 support
  Update composer packages
  Update composer packages
  Apply fixes from StyleCI
  Check if attribute_entity database table exists before querying entity attributes
  Return eloquent collection always from getEntityAttributes method for compatibility
  Add force option to artisan commands
  Simplify IoC binding
  Update minimum required PHP version
  Add Laravel v5.6 support
  Sort attributes on retrieval
  Drop useless model contracts (models already swappable through IoC)
  Typehint method returns
  Typehint void method returns
  Fixed #30
  Fix entity custom primary id issue (fix #30, #26)
  Fix documentation code smaple
  Remove fillable relation rules
  Fix entities issue and tweak some features
  ...
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants