esensi / model

The base model traits of Esensi
MIT License
202 stars 25 forks source link

RelatingModelTrait throws exception accessing BelongsTo relationship #26

Closed einkoro closed 8 years ago

einkoro commented 8 years ago
Exception: array_key_exists() expects parameter 2 to be array, null given in /home/vagrant/Projects/Varar/vendor/esensi/model/src/Traits/RelatingModelTrait.php:155

full strack trace and relevant models

laravel/framework 5.1.28 esensi/model 0.5.8

Steps to reproduce from php artisan tinker:

>>> \Varar\Models\Product::create([
            'name'                => 'Test Product 1YR',
            'default_activations' => 3,
            'default_duration'    => 365,
            'trial_activations'   => 1,
            'trial_duration'      => 14
        ]);
=> Varar\Models\Product {#815
     type: "Internal",
     messaging: false,
     subscription: false,
     status: true,
     name: "Test Product 1YR",
     default_activations: 3,
     default_duration: 365,
     trial_activations: 1,
     trial_duration: 14,
     updated_at: "2016-01-07 23:45:06",
     created_at: "2016-01-07 23:45:06",
     id: 1,
   }
>>> \Varar\Models\WhiteLabel::create([
            'name'       => 'Test Whitelabel Inherited',
            'product_id' => 1
        ]);
PHP warning:  array_key_exists() expects parameter 2 to be array, null given in /home/vagrant/Projects/Varar/vendor/esensi/model/src/Traits/RelatingModelTrait.php on line 155
>>> 

Interestingly enough if I extend WhiteLabel from Esensi\Model\Model rather than Illuminate\Database\Eloquent\Model and using the traits this error does not occur.

einkoro commented 8 years ago

Looks like its because of $relationshipPivots which defaults to [] in Model but wasn't around in 4.2 supporting verisons before I upgraded to 5.1 recently. Would it be reasonable to add an is_array() or is_null() check on hasPivotAttributes()?

dalabarge commented 8 years ago

Fixing with return array_key_exists( $name, $this->relationshipPivots ?: [] ); instead.