Reputation: 1890
I'm working on my first project on Symfony2. I have some problems with basic actions. I've generated entity of my table:
CREATE TABLE IF NOT EXISTS `product_category` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
Now I want to select all rows from this table. How should I do that? I tried to add new method in my entity.
public function getAll()
{
$em = $this->getDoctrine()->getManager();
$query = $em->createQuery(
'SELECT *
FROM product_category'
);
return $query->getResult();
}
... but I'm pretty sure that this is bad way cause I would have to use this method in context of concrete entity. Any tips?
Upvotes: 0
Views: 47
Reputation: 3668
Doctrine uses DQL not SQL and you don't need a custom query get all the records, you can use the default repository:
// e.g. in a controller
$repository = $this->getDoctrine()->getRepository('AcmeStoreBundle:Product');
$products = $repository->findAll();
This is an example right from the symfony docs. You should really read the docs first, these projects have greate docs.
Upvotes: 1