Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Repository service #150

Open
gadelkareem opened this issue Dec 20, 2016 · 14 comments
Open

Repository service #150

gadelkareem opened this issue Dec 20, 2016 · 14 comments

Comments

@gadelkareem
Copy link

gadelkareem commented Dec 20, 2016

Can you explain how do you create a repository service? I could not find it in the docs..

I tried:

    app.my_entity_solr_repository:
        class: FS\SolrBundle\Repository\Repository
        factory: ['@solr.client', getRepository]
        arguments:
          - AppBundle\Entity\MyEntity

I get an error

Warning: Missing argument 1 for AppBundle\Entity\MyEntity::__construct(), called in application/vendor/floriansemm/solr-bundle/FS/SolrBundle/Solr.php on line 149
@Koalabaerchen
Copy link

You need to create the repository class (see https://github.com/floriansemm/SolrBundle#repositories) and then include it via the annotation inside your entity (see https://github.com/floriansemm/SolrBundle#setting-custom-repository-class-with-repository-option)

It's basically like the normal doctrine repositories except that you write solr queries instead of using the querybuilder or writing dql.

@gadelkareem
Copy link
Author

But is there a way to use it as a service?

@Koalabaerchen
Copy link

Koalabaerchen commented Dec 20, 2016

The repository afaik not. Because a repository is different from a service in Symfony. A repository is a extension of an entity, which you can access directly from the entity repository. A service is (in basic sense) more like a helper that you can include in your controllers or services that does stuff you always have to do anyway (like initialize the client, create the query, set the hydration mode, set standard filters/sorting etc.)

You can create a service and inject solr.client (it's FS\SolrBundle\Solr) in the arguments into it. Then continue from there.

More infos on how to create a service you can find in the Symfony documentation

@gadelkareem
Copy link
Author

gadelkareem commented Dec 20, 2016

I digged a bit into the code and I see the instantiation of the entity is important for the Repository and other services but it might be avoided as far it is only used to read the annotations in \FS\SolrBundle\Doctrine\Mapper\MetaInformationFactory::loadInformation. What do you think?

Update removing the params from MyEntity::__construct() initiated the service with that config I posted

@Koalabaerchen
Copy link

Oh, I didn't see the edit in your first posting.

Can you post your constructor of your service?

@gadelkareem
Copy link
Author

You mean the Entity?

class MyEntity
{
   public function __construct($title, $url)
   {
        $this->title = trim($title);
        $this->url = trim($url);
        $this->dateCreated = new \DateTime();
    }
}

@floriansemm
Copy link
Owner

@gadelkareem to fix your issue you can set default-constructor parameters:

public function __construct($title = '', $url = '')

But your are right. It is a good idea to remove the entity dependency from the repository-class.

@gadelkareem
Copy link
Author

Thank you! is it possible to introduce this improvement any soon?

@floriansemm
Copy link
Owner

I am working on it

@floriansemm
Copy link
Owner

the lastest release includes some refactorings which solves your issue

@gadelkareem
Copy link
Author

Thank you! would the repository service config I posted earlier still work?

    app.my_entity_solr_repository:
        class: FS\SolrBundle\Repository\Repository
        factory: ['@solr.client', getRepository]
        arguments:
          - AppBundle\Entity\MyEntity

@floriansemm
Copy link
Owner

floriansemm commented Dec 21, 2016

Yes. I have tested locally with a similar configuration:

<service class="FS\SolrBundle\Repository\Repository" id="test_repo">
    <factory service="solr.client" method="getRepository" />
    <argument>Acme\DemoBundle\Entity\Post</argument>
</service>

@gadelkareem
Copy link
Author

Worked Thank you!

@gadelkareem
Copy link
Author

I just got the same error while using findBy(). The error is in FS/SolrBundle/Solr.php on line 109 where $entity = new $class; is still used..

@gadelkareem gadelkareem reopened this Dec 21, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants