0

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?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I just read that. But after doing that in $products I have a large set of variety of data.
Looks like I don't have to convert result into array or something just pass it into the template as written here: stackoverflow.com/questions/19138915/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.