Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

error when using composite key package #838

Closed
MartinP7r opened this issue Oct 23, 2019 · 5 comments
Closed

error when using composite key package #838

MartinP7r opened this issue Oct 23, 2019 · 5 comments
Labels

Comments

@MartinP7r
Copy link

#706 seems related

I'm using awobaz/compoships (https://github.com/topclaudy/compoships) for a relationship that uses composite keys in one of my models, which seems to break the generation script for models as it throws the following error:

Exception: Illegal offset type in isset or empty
Could not analyze class App\ClassName.

the breaking composite key relationship looks like this:

public function department()
{
    return $this->hasOne(
        Department::class,
        ['company_id', 'department_id'],
        ['company_id', 'department_id']
    );
}

I realize that you can't account for 3rd party packages, but would there be a workaround way to at least exclude the relationship from scanning?

@mpyw
Copy link

mpyw commented Apr 24, 2020

@MartinP7r (CC @barryvdh)

Quick fix for this issue (requires barryvdh/laravel-ide-helper^2.7 php>=7.4):

<?php

namespace App\Console\Commands\IdeHelper;

use Barryvdh\LaravelIdeHelper\Console\ModelsCommand as BaseModelsCommand;
use Illuminate\Support\Arr;
use Illuminate\Database\Eloquent\Relations\Relation;

class ModelsCommand extends BaseModelsCommand
{
    /**
     * @param  string   $relation
     * @param  Relation $relationObj
     * @return bool
     */
    protected function isRelationNullable(string $relation, Relation $relationObj): bool
    {
        $reflectionObj = new \ReflectionObject($relationObj);

        if (in_array($relation, ['hasOne', 'hasOneThrough', 'morphOne'], true) || !$reflectionObj->hasProperty('foreignKey')) {
            return parent::isRelationNullable($relation, $relationObj);
        }

        $fkProp = $reflectionObj->getProperty('foreignKey');
        $fkProp->setAccessible(true);

        return (bool)Arr::first(
            (array)$fkProp->getValue($relationObj),
            fn (string $value) => isset($this->nullableColumns[$value])
        );
    }
}

@stale
Copy link

stale bot commented Jul 29, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
If this issue is still present on the latest version of this library on supported Laravel versions, please let us know by replying to this issue so we can investigate further.
Thank you for your contribution! Apologies for any delayed response on our side.

@stale stale bot added the stale label Jul 29, 2020
Repository owner deleted a comment from burut13 Jul 30, 2020
@stale stale bot removed the stale label Jul 30, 2020
@stale
Copy link

stale bot commented Oct 28, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
If this issue is still present on the latest version of this library on supported Laravel versions, please let us know by replying to this issue so we can investigate further.
Thank you for your contribution! Apologies for any delayed response on our side.

@stale stale bot added the stale label Oct 28, 2020
@stale stale bot closed this as completed Dec 28, 2020
@musznik
Copy link

musznik commented Jan 16, 2022

error stil exist in "barryvdh/laravel-ide-helper": "^2.10" and "awobaz/compoships": "^2.1",

Trace:

Exception: Illegal offset type in isset or empty
Could not analyze class App\Models\MyModel.

#0 \barryvdh\laravel-ide-helper\src\Console\ModelsCommand.php(772): Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(2, 'Illegal offset ...', '--', 772, Array)
#1 \barryvdh\laravel-ide-helper\src\Console\ModelsCommand.php(734): Barryvdh\LaravelIdeHelper\Console\ModelsCommand->isRelationNullable('belongsTo', Object(Awobaz\Compoships\Database\Eloquent\Relations\BelongsTo))

Model:

class FirstModel extends Model
{
    use \Awobaz\Compoships\Compoships;

    function commune(){
        return  $this->belongsTo(SecondayModel::class,['WOJ','POW','GMI'],['WOJ','POW','GMI']);
    }
}

@maned3v
Copy link

maned3v commented Jan 20, 2022

I'm facing the same problem in version 2.10 and solved by checking each compoships array key element:

...\web\vendor\barryvdh\laravel-ide-helper\src\Console\ModelsCommand.php

protected function isRelationNullable(string $relation, Relation $relationObj): bool
    {
        $reflectionObj = new ReflectionObject($relationObj);

        if (in_array($relation, ['hasOne', 'hasOneThrough', 'morphOne'], true)) {
            $defaultProp = $reflectionObj->getProperty('withDefault');
            $defaultProp->setAccessible(true);

            return !$defaultProp->getValue($relationObj);
        }

        if (!$reflectionObj->hasProperty('foreignKey')) {
            return false;
        }

        $fkProp = $reflectionObj->getProperty('foreignKey');
        $fkProp->setAccessible(true);

        //Compoships
        if (is_array($fkProp->getValue($relationObj))) {
            foreach ($fkProp->getValue($relationObj) as $key) if (!isset($this->nullableColumns[$key])) return false;
            return true;
        }

        return isset($this->nullableColumns[$fkProp->getValue($relationObj)]);
    }

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants