You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Split the Client in a Client and an Entity Repository #15
Wouldn't it be nicer to have a default Client which handles the Conversion and persisting, and a separate class like the Doctrine EntityRepository to handle lookups?
If we want to load a specific filter, we can create an EntityRepository and add the specific methods to load something specific. Next add the EntityRepository to the Entity.
If we would want to persist or convert in a different way, we extend the Client and add it to the Entity tell the sdkClient to use a different client
Let me see. Something like this (comments are in the code)
useMapado\RestClientSdk\Mapping\AnnotationsasRest;
/** * This way no client (or repository) means the default Client/Repository will be used for lookups * The repository will return an object (or array of objects) of Cart when looking up information * @Rest\Entity(key="carts") */class Cart
{
/** * @Rest\Id() */protected$id;
}
$cartClient = $sdkClient->getClient(); // this contains the default client (a parameter can give back another client)// Multiple clients could be configured in 1 single $sdkClient like https://example.com/api and https://anotherpage.com/api/v2$repository = $cartClient->getRepository('carts'); // the client can give back the default Client/Repository$cart = $repository->find(1); // with a repository you can lookup information// A custom repositoryclass CartRepository extends Repository
{
publicfunctionfindByUser(User$user)
{
return$this->find($user->getId()); // find is defined in the repository (currently in your AbstractClient)
}
}
// the Entity would look like/** * @Rest\Entity(repository="namespace\to\CartRepository") * or with a new annotation like * @Rest\EntityRepository("namespace\to\CartRepository") */class Cart
{
/** * @Rest\Id() */protected$id;
}
// now the code would look exactly the same$cartClient = $sdkClient->getClient(); // this contains the default client (a parameter can give back another client)$repository = $cartClient->getRepository('carts'); // the client will now give the CartRepository back instead of the default Repository// but now you can use the findByUser on the CartRepository$cart = $repository->findByUser($app->getUser()); // with a repository you can lookup information
Basically the same approach Doctrine uses with EntityRepositories and EntityManager(s)
With Doctrine another EntityManager can be loaded (for example when you use a different database)
$em = $doctrine->getEntityManager('products'); // this will lookup in database X$repo = $em->getRepository("namespace\to\Product\Entity");
$product = $repo->findBy(array("id" => 1));
$em1 = $doctrine->getEntityManager('category'); // this will lookup in database Y$repo1 = $em1->getRepository("namespace\to\Category\Entity");
$categories = $repo1->findByProduct($product);
$defaultrepo = $doctrine->getEntityManager()->getRepository("namespace\to\User\Entity"); // this will lookup in your default database$user = $defaultrepo->find(1);
You will have too clients: mapado.rest_client_sdk.foo and mapado.rest_client_sdk.bar, which is pretty similar to the doctrine differents entity managers. (in fact the doctrine bundle allows you to call doctrine.orm.foo_entity_manager and doctrine.orm.bar_entity_manager).
You have a point though about the component, which does not have a main container.
Activity
jdeniau commentedon Feb 17, 2016
Can you write some pseudo-code to see exactly how you see it ?
I think this is a really good idea because the default client is kind of heavy.
leroy0211 commentedon Feb 17, 2016
Let me see. Something like this (comments are in the code)
Basically the same approach Doctrine uses with
EntityRepositoriesandEntityManager(s)With Doctrine another EntityManager can be loaded (for example when you use a different database)
jdeniau commentedon Feb 18, 2016
In fact, you can manage multiple endpoints too when using the bundle:
You can configure something like this:
You will have too clients:
mapado.rest_client_sdk.fooandmapado.rest_client_sdk.bar, which is pretty similar to the doctrine differents entity managers. (in fact the doctrine bundle allows you to calldoctrine.orm.foo_entity_manageranddoctrine.orm.bar_entity_manager).You have a point though about the component, which does not have a main container.
jdeniau commentedon Mar 11, 2016
Fixed in #21