Git Product home page Git Product logo

Comments (4)

jdeniau avatar jdeniau commented on July 26, 2024

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.

from rest-client-sdk.

leroy0211 avatar leroy0211 commented on July 26, 2024

Let me see. Something like this (comments are in the code)

use Mapado\RestClientSdk\Mapping\Annotations as Rest;

/**
 * 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 repository
class CartRepository extends Repository
{
    public function findByUser(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);

from rest-client-sdk.

jdeniau avatar jdeniau commented on July 26, 2024

In fact, you can manage multiple endpoints too when using the bundle:

You can configure something like this:

# app/config/config.yml
mapado_rest_client_sdk:
    entity_managers:
        foo:
            server_url: 'http://foo.com:8080'
            mappings:
                prefix: /v1
                dir: '%kernel.root_dir%/../src/Foo/Entity/'
        bar:
            server_url: 'http://bar.com'
            mappings:
                dir: '%kernel.root_dir%/../src/Bar/Entity/'

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.

from rest-client-sdk.

jdeniau avatar jdeniau commented on July 26, 2024

Fixed in #21

from rest-client-sdk.

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.