SolrBundle icon indicating copy to clipboard operation
SolrBundle copied to clipboard

Repository service

Open gadelkareem opened this issue 8 years ago • 14 comments

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

gadelkareem avatar Dec 20 '16 21:12 gadelkareem

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.

Koalabaerchen avatar Dec 20 '16 21:12 Koalabaerchen

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

gadelkareem avatar Dec 20 '16 21:12 gadelkareem

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

Koalabaerchen avatar Dec 20 '16 21:12 Koalabaerchen

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

gadelkareem avatar Dec 20 '16 22:12 gadelkareem

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

Can you post your constructor of your service?

Koalabaerchen avatar Dec 21 '16 09:12 Koalabaerchen

You mean the Entity?

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

gadelkareem avatar Dec 21 '16 09:12 gadelkareem

@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.

floriansemm avatar Dec 21 '16 10:12 floriansemm

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

gadelkareem avatar Dec 21 '16 10:12 gadelkareem

I am working on it

floriansemm avatar Dec 21 '16 10:12 floriansemm

the lastest release includes some refactorings which solves your issue

floriansemm avatar Dec 21 '16 15:12 floriansemm

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

gadelkareem avatar Dec 21 '16 15:12 gadelkareem

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>

floriansemm avatar Dec 21 '16 15:12 floriansemm

Worked Thank you!

gadelkareem avatar Dec 21 '16 20:12 gadelkareem

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 avatar Dec 21 '16 20:12 gadelkareem