Giter Club home page Giter Club logo

Comments (8)

werepa avatar werepa commented on August 19, 2024

Hi guys, I am suffering in trying to get a custom function fetchAll for my collections using zf-apigility-doctrine. Can someone give me an example for that? I need a mapper when using Doctrine? Thanks.

from zf-apigility-doctrine.

intellix avatar intellix commented on August 19, 2024

Thanks for hijacking my issue by the way :P I think you're looking for stackoverflow or your own issue.

To shed a little more light onto my problem, I've half managed to get around it, but I still question if there should be a different set of strategies depending on the call.

My cheap solution for now:

/**
 * For being able to hydrate a relationship that uses a _link, 
 * like: account: {id: 1}
 */
class HydratableCollectionLink extends CollectionLink
{
    public function hydrate($value)
    {
        return $value;
    }
}

I don't want a nested exception, caused by Doctrine providing parental references so I apply a CollectionLink strategy to stop it from being included, but I want different results like so:

GET /accounts/me

{
    "id": 1,
    "name": "Joe Bloggs",
    "things": [
        {
            "id": 1,
            "name": "Book",
            "account": null
        },
        {
            "id": 2,
            "name": "Pencil",
            "account": null
        }
    ]
}

GET /things/1

{
    "id": 1,
    "name": "Book",
    "account": {
        "id": 1,
        "name": "Joe Bloggs",
        "things": []
    }
}

Right now I can only define 1 strategy for everything and I'm struggling to handle the different situations I'd expect to use an API.

from zf-apigility-doctrine.

veewee avatar veewee commented on August 19, 2024

@intellix
There are some options:

  1. You can create a custom hydrator and register it to the hydrator_manager config.
  2. You can create custom hydration strategies and add it to the service_manager config.
  3. When you want it to be request specific, you could create an AggregateHydrator and inject the request object in this hydrator.

At the moment there aren't any custom ORM strategies in the hydration module. These collection strategies are available for ODM. So maybe you can try to reuse one of these?

@werepa
This is not possible yet. You can wait untill #76 is merged.

from zf-apigility-doctrine.

intellix avatar intellix commented on August 19, 2024

I've created a custom hydrator to half get around the issue for now, it doesn't really do much but allows relations using CollectionLinks to be hydrated.
I'll have a look into creating an AggregateHydrator but I think this is a direction the project needs to head towards anyway. I thought I was doing something wrong or missing an important feature but it seems the functionality doesn't yet exist.

How do you guys envision hydration to be used? Zoom is a good start, it allows you to do what you want per request but I think we need to be able to configure different strategies depending on usage/request.

Maybe I could create Code Connected Resources per scenario so I have something like:

/account-things/me which would pull an Account AND Things in the same call. I suppose Doctrine connected resources aren't supposed to jack of all trades but more convenience, just like DB Connected resources

from zf-apigility-doctrine.

veewee avatar veewee commented on August 19, 2024

I don't like to use the zoom feature. At the end, this is just a custom strategy which you can configure in the doctrine hydrator configuration. So it seems useless to me.

It is hard to provide a general configuration-set for the problem you describe.
Most of the time this will be custom to the specific problem you have.
The things you describe can be done with custom hydration strategies.
You can also create some hydrator filters to show/hide specific fields based on request type.
Maybe we should provide some extra hydration strategies where you can choose from?
Strategies for ORM collections are definitely on my personal wish-list :)

When PR #79 is getting merges, it is also possible to hook in on specific doctrine events. This means it is possible to fine-tune the results before sending it back to the user. You can apply some additional hydration or filtering there.

from zf-apigility-doctrine.

intellix avatar intellix commented on August 19, 2024

I really like the way that https://github.com/zfcampus/zf-content-validation handles the Request specific Validators:

'zf-content-validation' => array(
    'Application\Controller\HelloWorld' => array(
        'input_filter' => 'Application\Controller\HelloWorld\Validator',
        'POST' => 'Application\Controller\HelloWorld\CreationValidator',
    ),
),

You specify a catch-all and override depending on the different request types. That way you can do it all via config. How about something similar for specifying hydrators?

'zf-apigility' => array(
    'doctrine-connected' => array(
        'MyApp\\V1\\Rest\\Account\\AccountResource' => array(
            'object_manager' => 'doctrine.entitymanager.orm_default',
            'hydrators' => array(
                'default' => 'MyApp\\V1\\Entity\\AccountHydrator',
                // Override GET resource hydrator to also hydrate things
                'fetch' => 'MyApp\\V1\\Entity\\AccountFetchHydrator',
            ),
        ),
    ),
),
'doctrine-hydrator' => array(
    'MyApp\\V1\\Entity\\AccountHydrator' => array(
        'entity_class' => 'MyApp\\V1\\Entity\\Account',
        'object_manager' => 'doctrine.entitymanager.orm_default',
        'by_value' => true,
        'use_generated_hydrator' => true,
        'strategies' => array(),
    ),
    'MyApp\\V1\\Entity\\AccountFetchHydrator' => array(
        'entity_class' => 'MyApp\\V1\\Entity\\Account',
        'object_manager' => 'doctrine.entitymanager.orm_default',
        'by_value' => true,
        'use_generated_hydrator' => true,
        'strategies' => array(
            'things' => 'CollectionExtract'
        ),
    ),
),

from zf-apigility-doctrine.

TomHAnderson avatar TomHAnderson commented on August 19, 2024

@intellix: anything to add or contribute on this topic? I'll close the issue soon.

from zf-apigility-doctrine.

intellix avatar intellix commented on August 19, 2024

I suppose there isn't anything for me to add or contribute. Will close!

from zf-apigility-doctrine.

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.