Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Nested belongsTo relationship #8

Open
johnwilhite opened this issue May 11, 2018 · 2 comments
Open

Nested belongsTo relationship #8

johnwilhite opened this issue May 11, 2018 · 2 comments

Comments

@johnwilhite
Copy link

This is a similar issue to #5.

The trait is failing on the following example:

$contract = new Contract([
    'price' => '12',
    'customer' => [ // belongsTo relation
            'name' => 'First Last',
            'reference' => [ // hasMany relation
                [
                    'name' => 'First Last'
                ]
            ]
        ]
    ]
]);

The fillBelongsToRelation save for customer fails with the following, because it's trying to select reference, which is the relation and not a column, from the customers table.

Column not found: 1054 Unknown column 'reference' in 'where clause' 

The following seems to fix the issue for belongsTo relations, it's extracting the relations out and only using the attributes in that lookup.

    /**
     * @param BelongsTo $relation
     * @param array|Model $attributes
     */
    public function fillBelongsToRelation(BelongsTo $relation, $attributes, $relationName)
    {
        $entity = $attributes;
        if (!$attributes instanceof Model) {
            if (method_exists($relation->getRelated(), 'extractFillableRelations')) {
                list($relations, $attributes) = $relation->getRelated()->extractFillableRelations($attributes);
            }

            $entity = $relation->getRelated()
                ->where($attributes)->firstOrFail();
        }
        $relation->associate($entity);
    }

Would this be an appropriate fix? Also I haven't tested the belongs to many, but I'd imagine it would need a similar fix.

@johnwilhite johnwilhite changed the title Nested belongs to relationship Nested belongsTo relationship May 11, 2018
@troelskn
Copy link
Owner

Hi John. Sorry for the late response.

Could you supply the db schema and the full model code that you are using?

@FredrikBergelin
Copy link

This is how I just managed to solve it, I think. Haven't tested it thoroughly. Makes sense?

public function fillBelongsToRelation(BelongsTo $relation, $attributes, $relationName)
{
    $entity = $attributes;
    $created = self::getModelName($relationName)::create($entity);
    $relation->associate($created);

    // if (!$attributes instanceof Model) {
    //     $entity = $relation->getRelated()
    //         ->where($attributes)->firstOrFail();
    // }
    // $relation->associate($entity);
}
private static function getModelName($name)
{
    return 'App\Models\' . rtrim(ucfirst($name), 's');
}

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

No branches or pull requests

3 participants