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

getAliasProperties are not working for included relationships #127

Open
rduque1 opened this issue Sep 13, 2017 · 0 comments
Open

getAliasProperties are not working for included relationships #127

rduque1 opened this issue Sep 13, 2017 · 0 comments

Comments

@rduque1
Copy link

rduque1 commented Sep 13, 2017

When I get a resource, the aliases for the properties are well done, but the aliases for the included relationships are not used.

{
    "data": [
        {
            "type": "coupon-referrals",
            "id": "11",
            "attributes": {
                "coupon-id": 34,
                "created-at": "2017-09-12 12:44:17",
                "id": 11,
                "order-id": 5830,
                "updated-at": "2017-09-12 12:44:17",
                "user-id": 2000000010
            },
            "relationships": {
                "order": {
                    "data": {
                        "type": "order",
                        "id": "5830"
                    }
                },
                "coupon": {
                    "data": {
                        "type": "coupon",
                        "id": "34"
                    }
                }
            }
        }
    ],
    "included": [
        {
            "type": "order",
            "id": "5830",
            "attributes": {
                "user_id": 2000000010,
                "payment_id": 5675,
                "apps": "[]",
                "products": "[{\"id\":2,\"quantity\":1,\"price\":1500}]",
                "shipped": 0,
                "shipping_costs": 0,
                "created_at": "2017-09-12 12:44:15",
                "updated_at": "2017-09-12 12:44:17"
            }
        },
        {
            "type": "coupon",
            "id": "34",
            "attributes": {
                "name": "REF2642b",
                "amount": 1000,
                "coupon_type": "referral-amount",
                "counter": 2,
                "max_counter": 2,
                "valid_from": "2017-09-12",
                "valid_to": "2018-09-12",
                "is_products_only": 1,
                "created_at": "2017-09-12 12:44:17",
                "updated_at": "2017-09-12 12:47:25"
            }
        }
    ],
    "links": {
        "self": {
            "href": "http://192.168.99.100:4200/api/coupon-referrals/11?page[number]=1&page[size]=10"
        },
        "first": {
            "href": "http://192.168.99.100:4200/api/coupon-referrals/11?page[number]=1&page[size]=10"
        },
        "last": {
            "href": "http://192.168.99.100:4200/api/coupon-referrals/11?page[number]=1&page[size]=10"
        }
    },
    "meta": {
        "page": {
            "total": 1,
            "last": 1,
            "number": 1,
            "size": 10
        }
    },
    "jsonapi": {
        "version": "1.0"
    }
}

As you can see the order has in the properties _ as in the mapping is defined in the aliases to use -.

<?php

namespace App\Api\V1\Mappings;

use App\Coupon;
use NilPortugues\Api\Mappings\JsonApiMapping;

class CouponMapping implements JsonApiMapping
{
    public function getClass()
    {
        return Coupon::class;
    }

    /**
     * Returns an array of properties that will be renamed.
     * Key is current property from the class. Value is the property's alias name.
     *
     * @return array
     */
    public function getAliasedProperties()
    {
        return [
            'coupon_type'       => 'coupon-type',
            'max_counter'       => 'max-counter',
            'valid_from'        => 'valid-from',
            'valid_to'          => 'valid-to',
            'is_products_only'  => 'is-products-only',
            'created_at'        => 'created-at',
            'updated_at'        => 'updated-at'
        ];
    }

    public function getIdProperties()
    {
        return ['id'];
    }
    /**
     * Returns an array containing the relationship mappings as an array.
     * Key for each relationship defined must match a property of the mapped class.
     *
     * @return array
     */
    public function getRelationships()
    {
        return [];
    }

    /**
     * Implement the remaining methods from NilPortugues\Api\Mappings\ApiMapping::getRequiredProperti
     */
     public function getRequiredProperties()
    {
        return [];
    }
}

It the same for the order relationship. What did I do wrong?

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

1 participant