Comments (8)
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.
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.
@intellix
There are some options:
- You can create a custom hydrator and register it to the hydrator_manager config.
- You can create custom hydration strategies and add it to the service_manager config.
- 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.
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.
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.
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.
@intellix: anything to add or contribute on this topic? I'll close the issue soon.
from zf-apigility-doctrine.
I suppose there isn't anything for me to add or contribute. Will close!
from zf-apigility-doctrine.
Related Issues (20)
- Question/Problem :: Add data with POST on a @ORM\OneToMany relation HOT 3
- naming strategy ? HOT 2
- Cannot attach listeners via config HOT 1
- Unable to fetch hydrators after creating a new service HOT 1
- Change Entitymanager for DoctrineResource HOT 4
- Query Create Filter on patch/update too? HOT 14
- listeners, query_create_filter and query_providers keys in 'doctrine-connected' not saved HOT 8
- Patch's data can't be changed by listener HOT 1
- odm_default adapter not recognized by ZF\Apigility\Doctrine\Admin HOT 7
- ZF\Apigility\Admin dependency missing HOT 4
- Inserting a new Resource in a Many2Many Relationship HOT 1
- How to add dependency inside resource? HOT 4
- Validator factory not work HOT 14
- Injecting dependencies into DoctrineResource HOT 2
- Unable to create entities with dependencies HOT 4
- Uni-directional ManyToOne/OneToOne repeated in Entity to same Target throws fatal DBAL exception HOT 5
- EVENT_FETCH_PRE Not Fired as Doctrine Resource Event HOT 6
- Include link to book in README HOT 1
- Link to zf-component-installer 404 HOT 1
- Invalid condition HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from zf-apigility-doctrine.