itsgoingd / clockwork

Clockwork - php dev tools in your browser - server-side component
https://underground.works/clockwork
MIT License
5.66k stars 323 forks source link

Laravel hasMany relation and relation has 2 primary keys #598

Open Patus opened 2 years ago

Patus commented 2 years ago

Hello,

When making query with entries.entry where model has hasMany relationship to other model and Entries model has two primary keys. I get error: array_key_exists(): The first argument should be either a string or an integer at ...vendor\\laravel\\framework\\src\\Illuminate\\Database\\Eloquent\\Concerns\\HasAttributes.php:321)

example: Model::with( 'entries.entry' )->get();

Model:

public function entries()
    {
        return $this->hasMany( 'App\entries', 'id' );
    }

entries model: protected $primaryKey = ['id', 'entry_id'];

Laravel 6 clockwork 5.1

Error:

#0 [internal function]: Illuminate\\Foundation\\Bootstrap\\HandleExceptions->handleError(2, 'array_key_exist...', '...', 321, Array)
#1 ...\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Eloquent\\Concerns\\HasAttributes.php(321): array_key_exists(Array, Array)
#2...\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Eloquent\\Model.php(1396): Illuminate\\Database\\Eloquent\\Model->getAttribute(Array)
#3 ...\\vendor\\itsgoingd\\clockwork\\Clockwork\\DataSource\\EloquentDataSource.php(201): Illuminate\\Database\\Eloquent\\Model->getKey()
#4 ...\\vendor\\itsgoingd\\clockwork\\Clockwork\\DataSource\\EloquentDataSource.php(151): Clockwork\\DataSource\\EloquentDataSource->collectModelEvent('retrieved', Object(App\\entries))
#5 ...\\vendor\\laravel\\framework\\src\\Illuminate\\Events\\Dispatcher.php(344): Clockwork\\DataSource\\EloquentDataSource->Clockwork\\DataSource\\{closure}(Object(App\\entries), Array)
#6 ...\\vendor\\laravel\\framework\\src\\Illuminate\\Events\\Dispatcher.php(196): Illuminate\\Events\\Dispatcher->Illuminate\\Events\\{closure}('eloquent.retrie...', Array)
#7 ...\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Eloquent\\Concerns\\HasEvents.php(188): Illuminate\\Events\\Dispatcher->dispatch('eloquent.retrie...', Array)
#8 ...\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Eloquent\\Model.php(420): Illuminate\\Database\\Eloquent\\Model->fireModelEvent('retrieved', false)
#9 ...\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Eloquent\\Builder.php(298): Illuminate\\Database\\Eloquent\\Model->newFromBuilder(Object(stdClass))
#10 [internal function]: Illuminate\\Database\\Eloquent\\Builder->Illuminate\\Database\\Eloquent\\{closure}(Object(stdClass))
itsgoingd commented 2 years ago

Hey, I don't think composite keys are supported in Laravel.

Remcojansen88 commented 1 year ago

Got the same kind of issue. You can define a array as key type in laravel.

public function getKeyType() { return 'array'; }

When we disable clockwork this works fine, but if we enable it this error is thrown. So clockwork changes in some kind of way the $key values of setAttribute from string to the primary key. Illegal offset type in isset or empty

itsgoingd commented 1 year ago

Hey, the Laravel documentation only mentions int and string as possible values for the key type.

Remcojansen88 commented 1 year ago

This issues is more that clockwork changes the type of the value. I liked clockwork, so i maybe create a fork thats supports the the array as primary key type

itsgoingd commented 1 year ago

If you look at the stack trace, the crash happens somewhere in the Eloquent code, when Clockwork calls $model->getKey(), which should return the primary key value.

amashkarin commented 6 months ago

In my case using Illuminate\Database\Eloquent\Relations\Pivot instead Illuminate\Database\Eloquent\Model as parent class for model has resolve problem