Answer the question
In order to leave comments, you need to log in
What is the best way to use Doctrine Repository and Entity in Symfony?
I don't really need to use Doctrine ORM. You just need some class in which there will be methods for obtaining data with native SQL, or DQL, or dynamic methods, too, for simple selections. The best way to do everything is to simply create an Entitiy and describe the necessary fields there, and then use dynamic methods like findOneBy*, etc. Or just create a Repository without Entity and describe your methods there? Or is it better not to be tied to this at all, but to create a separate class, get $this->getDoctrine()->getManager(); and already make queries using the createQuery methods? Why so, because you need to make very complex queries, which you are tormented to do with ORM.
Answer the question
In order to leave comments, you need to log in
In the end, I decided it all like this, I threw this filthy Doctrine\ORM into the trash and just started using Doctrine\DBAL. I created the Models folder, declared the Model class there, I get Doctrine\DBAL\Connection in it. Then from Model I inherit my other models. In order for all this to work properly, we need a more advanced DI implementation, so that we do not constantly declare each model as a service, for this there is a gorgeous bundle https://github.com/dunglas/DunglasActionBundle.
As a result, I do not have unnecessary Entity and Repository, I reduced the code not by 2 or 3 times, but probably by 5 times! Now I have a simple and concise model without the most disgusting and unnecessary ORM links. In this case, I can use all the charms of Doctrine \ DBAL!
Model example:
namespace AppBundle\Models;
use Doctrine\DBAL\Connection;
class Model
{
private $connection;
public function __construct(Connection $connection)
{
$this->connection = $connection;
}
public function getConnection()
{
return $this->connection;
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question