Open gadelkareem opened 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.
But is there a way to use it as a service?
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
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
Oh, I didn't see the edit in your first posting.
Can you post your constructor of your service?
You mean the Entity?
class MyEntity
{
public function __construct($title, $url)
{
$this->title = trim($title);
$this->url = trim($url);
$this->dateCreated = new \DateTime();
}
}
@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.
Thank you! is it possible to introduce this improvement any soon?
I am working on it
the lastest release includes some refactorings which solves your issue
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
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>
Worked Thank you!
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..
Can you explain how do you create a repository service? I could not find it in the docs..
I tried:
I get an error