Giter Club home page Giter Club logo

Comments (6)

Pierozi avatar Pierozi commented on September 25, 2024

Actually I need this for continue my Project.
I'm able to purpose you a PR if you want ?

from jsonapi.

lode avatar lode commented on September 25, 2024

Actually, I haven't got to this because I fear it is going to be very slow.
As I also need it for my projects I working around it at the moment.

<?php

function get_to_many_relation($main_type, $relation_type, $data) {
    // collect ids
    $ids = array();
    foreach ($data as $object) {
        $ids[] = $object->id;
    }

    // build relation spec
    return array(
        'links' => array('related' => '/api/'.$main_type.'/'.$user->id.'/'.$relation_type),
        'data'  => array('type' => $relation_type, 'id' => $ids),
    );
}

$ships = models\ships::find_all();
$jsonapi->add_relation($main_type='user', $sub_type='ship', get_to_many_relation($ships));

As you can always add an array with the right set of data, you can just fill the relation yourself.
This method could become something I might include, but right now I feel it requires too much domain knowledge of the data you provide, so I haven't done it yet.

from jsonapi.

Pierozi avatar Pierozi commented on September 25, 2024

I got your trick by define yourself array will be apply to primary_relationships

But the fix are really quick, safe and fast.
I've just finished and push in my fork at branch feature/multirelation
Pierozi@b0ef713

from jsonapi.

lode avatar lode commented on September 25, 2024

That doesn't look bad for inside jsonapi indeed. However, from the callers perspective it feels awkward.

My original idea with this issue was something like this:

$user_resource = new jsonapi\resource('user', $user->id);

$ships = mysql::select("SELECT `id` FROM `ships`;");
$ship_collection = new jsonapi\collection('ship');
foreach ($ships as $ship) {
    $ship_resource = new jsonapi\resource('ship', $ship['id']);
    $ship_collection->add_resource($ship_resource);
}

$user_resource->add_relation('ship', $ship_collection);

That feels all ok from within jsonapi, but as you see it is a lot of code to get it arranged.

The fix you did now would change that into the following, right?

$user_resource = new jsonapi\resource('user', $user->id);

$ships = mysql::select("SELECT `id` FROM `ships`;");
foreach ($ships as $ship) {
    $ship_resource = new jsonapi\resource('ship', $ship['id']);
    $user_resource->add_relation('ship', $ship_resource);
}

Quite ok, but it could be simpler I would hope. I just want something like this:

$ships = mysql::select("SELECT `id` FROM `ships`;");

$user_resource = new jsonapi\resource('user', $user->id);
$user_resource->add_relation('ship', $ships); // some magic happens

It feels good adding your patch anyway, as it is almost a bugfix for when you would add the same kind of relation twice. But it doesn't feel like a nice interface yet.

Do you have ideas?
I'll also play with it a bit more.

from jsonapi.

Pierozi avatar Pierozi commented on September 25, 2024

The things you want is an automatic resource from a dataset right ?

I think is something can be done easily but for good practices and the future of PHP7, we need use Interface and dataset object based on this interface and not an Array result of mysql. Or in this case a magic function for convert an array into resource based on "id" mandatory and other free column.

I've build and advanced collection examples for this usecases and for relationship into included.
we can work around this example if you want.

from jsonapi.

lode avatar lode commented on September 25, 2024

@Pierozi, I've implemented this issue and used your commit. It was indeed simpler than I thought.

I did include an extra argument which is needed to add multiple resource objects, to prevent people from doing it by accident.

You can add relations like this:

$ship = new jsonapi\resource('ship', 42);
$ship->add_data('foo', 'bar');
$jsonapi->add_relation('ship', $ship, $skip_include=false, $type=jsonapi\resource::RELATION_TO_MANY);

$ship2 = new jsonapi\resource('ship', 24);
$ship2->add_data('bar', 'baz');
$jsonapi->add_relation('ship', $ship2, $skip_include=false, $type=jsonapi\resource::RELATION_TO_MANY);

$friend1 = new jsonapi\resource('user', 42);
$friend1->add_data('foo', 'bar');

$friend2 = new jsonapi\resource('user', 24);
$friend2->add_data('bar', 'baz');

$friends = new jsonapi\collection('friends');
$friends->add_resource($friend1);
$friends->add_resource($friend2);

$jsonapi->add_relation('friends', $friends);

Does this work well for your use case?

Update: the magic conversion from an array dataset from mysql is for me the base, not a trick. I want the method(s) to keep accepting raw array data. I'm not really fond of adding a lot of bureaucracy when we can already work with the format. If you know better and have everything in the right format, you should be able to add it.

from jsonapi.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.