Skip to content

Schemas loading for relationships #8

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

Closed
phaberest opened this issue Apr 6, 2016 · 3 comments
Closed

Schemas loading for relationships #8

phaberest opened this issue Apr 6, 2016 · 3 comments

Comments

@phaberest
Copy link

Hi @lindyhopchris and thanks in advance for this awesome work!
I'm getting an error when I try to add a relationship in my schema, it seems it can't find the schema for the relationship, even if I've set it in the configuration file.

The error I get is

InvalidArgumentException in Parser.php line 241:
Schema is not registered for a resource at path 'addons'.

In json-api.php I set the schemas array as follows

/**
 * Schemas
 */
C::SCHEMAS => [
    Schemas::DEFAULTS => [
        'App\Models\Addon'    => 'App\Schemas\AddonSchema',        // The relationship
        'App\Models\Shipment' => 'App\Schemas\ShipmentSchema',
    ],
    // merged with defaults if JSON API middleware uses the 'extra-schemas' name.
    'extra-schemas' => [
        'User' => 'UserSchema',
    ],
],

And in the parent model schema I have

public function getRelationships(
    $shipment,
    array $includeList = []
) {
    /** @var Shipment $shipment */
    return [
        'addons'   => [
            self::DATA         => $shipment->addons,
            self::SHOW_SELF    => true,
            self::SHOW_RELATED => true
        ],
        'sender'   => [self::DATA => $shipment->sender],
        'receiver' => [self::DATA => $shipment->receiver],
    ];
}

I'm having troubles understanding the docs 100%, maybe I did something wrong. Maybe I'm not including enough from neomerx/json-api...I am jumping from an error to another 😔

Thanks for your help

@ghost
Copy link

ghost commented Apr 17, 2016

I had a problem like this, I can't remember if this was what fixed it but it might be not detecting the Addons model properly if empty.

I created a parent "Schema" class to add in a few extra tools like this:

<?php
namespace App\Schemas;

use Neomerx\JsonApi\Schema\SchemaProvider;

use Illuminate\Database\Eloquent\Collection;

abstract
class Schema extends SchemaProvider
{
    public
    function getId($resource)
    {
        return $resource->id;
    }

    protected
    function extractRelation($resource, $relationName, $force = false)
    {

        if (empty($resource->{$relationName}) && $force)
        {
            $resource->load($relationName);
        }

        if (empty($resource->{$relationName}))
        {
            return null;
        }

        if ($resource->{$relationName} instanceof Collection)
        {
            return $resource->{$relationName}->all();
        }

        return $resource->{$relationName};
    }

}

And then when I extract the relationship I do it like:

<?php
namespace App\Schemas;

use App\Schemas\Schema;

class `ShipmentSchema` extends Schema
{
...

    public
    function getRelationships($shipment, array $includeRelationships = [])
    {
        return [
            'addons' => [
                self::DATA         => $this->extractRelation($shipment, 'addons')
            ],
        ];
    }

}

@lindyhopchris
Copy link
Member

@phaberest Sorry for slow reply - I've been on holiday the last two weeks.

The error you're getting is a neomerx/json-api error - the most likely cause is that if $shipment->addons returns an Eloquent Collection, then the encoder will be trying to lookup a schema for that class, rather than the class of model that is contained within the collection. You need to cast the addons relationship to an array i.e. $shipment->addons->all()

Does that fix it?

@phaberest
Copy link
Author

Thanks to both of you, I have temporarily put it in the parent element getAttributes() ad I've added an accessor in the model to return the array as I wanted.

Tried on fly @lindyhopchris solution and it seems to look for the schema 👍 I'll look into it, thanks a lot!

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

2 participants