简体   繁体   中英

Where should my raw SQL go in Symfony 2?

I'm currently rebuilding my current website using Symfony 2 instead of the previous Codeigniter framework it was running on. I'm completely new to Doctrine too, and I'm having a little trouble working out the best place to put a complicated-ish MySQL query.

I have a service container currently that uses this query via prepare($sql)->execute() , and various functions that use the data it provides. As it stands now, I pass doctrine to the service container, and run the query within. I then use the same service container to do what I want with this data.

To me, it just doesn't feel right fetching and using the database data in the same class? Should I put the execute() query in the entities folder at the bottom of the Doctrine created file, in the entities folder in it's own file, or is it fine where it is?

I think, it will the be better way to create a custom repository class in Repository folder for some actions by preparing entity data. You can read more about custom repository here custom-repository-classes

Further repository can be used throw doctrine. For example:

$container->get('doctrine')->getRepository('YourBundleName:Entity')->someRepositoryMethod();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM