safrazik / breeze.server.php

Breeze JS support for PHP applications
MIT License
29 stars 11 forks source link

[feature] Custom Services And non-mapped properties #12

Open davidsielert opened 9 years ago

davidsielert commented 9 years ago

This library has been working out pretty great sitting in a laravel 5 project. Just a couple improvements could make it exceed endlessly... It'd be cool if you could create service endpoints that return arrays of objects (think Lookups). The example in the demo is OK but highly ineffceient for a large lookup set .. (talking 10+ queries) which could be exacted in one lookup service..

Also I notice if you put a property on the doctrine class but don't specify a doctrine @column tag it still sends the data down in the metadata.. I personally think this or perhaps something more clever could be useful for sending unmaped properties . Since per the breeze docs you can't get tracking on unmaped properties unless they come down from the server side...

safrazik commented 9 years ago

For the first question, you can try like the following for the time being


Route::get('/api/Lookups', function(){
    $app = require_once __DIR__.'/models/bootstrap.php';
    $manager = $app->getObjectManager();
   // get the lookup entities array
    $results = array();
    $lookupEntities = array('Some\Entity', 'Another\Entity');
    foreach($lookupEntities as $entityClass){
        $results = array_merge($results, $manager->getRepository($entityClass)->findAll());
    }
    $request = Request::instance();
    $response = $app->getSerializedResponse($results, $request);
    $response->send();
});

and in the client side


var query = new breeze.EntityQuery('Lookups');
em.executeQuery(query).then(function(data){
    // all lookups were fetched and now are managed by breeze
}, function(error){
    console.log(error);
});