floriansemm / SolrBundle

Solr-Integration into Symfony and Doctrine2
http://floriansemm.github.io/SolrBundle
MIT License
123 stars 73 forks source link

Repository service #150

Open gadelkareem opened 7 years ago

gadelkareem commented 7 years ago

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 commented 7 years ago

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 commented 7 years ago

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

Koalabaerchen commented 7 years ago

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 commented 7 years ago

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 commented 7 years ago

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

Can you post your constructor of your service?

gadelkareem commented 7 years ago

You mean the Entity?

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

@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 commented 7 years ago

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

floriansemm commented 7 years ago

I am working on it

floriansemm commented 7 years ago

the lastest release includes some refactorings which solves your issue

gadelkareem commented 7 years ago

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 commented 7 years ago

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 commented 7 years ago

Worked Thank you!

gadelkareem commented 7 years ago

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