1

I'm trying to query the database in my user repository like this (symfony2):

namespace Doobin\UserBundle\Entity;

use Doctrine\ORM\EntityRepository;

class UserRepository extends EntityRepository
{
public function pageAccess($User=1,$Page='_home')
{
    $query="SELECT `user` FROM `vu001_user_permission` WHERE `user` = '$User' AND `path`='$Page'";
    $conn = Doctrine_Manager::getInstance()->connection();
    $stmt = $conn->prepare($query);
    $stmt->execute();
    return $stmt;
}

But it won't work. It stop working when it arives at the live $conn = Doctrine_Manager::getInstance()->connection();

I copied that from the symfony website tuturial.

I Also tried this too:

$em = $this->getDoctrine()->getEntityManager();
    $connection = $em->getConnection();
    $statement = $connection->prepare("SELECT `user` FROM `vu001_user_permission` WHERE `user` = '$User' AND `path`='$Page'");
    $statement->execute();
    $results = $statement->fetchAll();

It stop working when it arive to line $em = $this->getDoctrine()->getEntityManager();

And tried this too:

namespace Doobin\UserBundle\Entity;

use Doctrine\ORM\EntityRepository;

class UserRepository extends EntityRepository
{
public function pageAccess($User=1,$Page='_home')
{
return $this->getEntityManager()
        ->createQuery(
            'SELECT p FROM UserBundle:User p ORDER BY p.name ASC'
        )
        ->getResult();
}
}

$this->getEntityManager() now working, please help I'm so confiused. I checked everything.

Am I missing anything?

2 Answers 2

7

Do the following to get connection:

$em = $this->getDoctrine()->getEntityManager();
$conn = $em->getConnection();

To Query,

$stmt = $conn->prepare($query);
$stmt->execute();
$results = $stmt->fetchAll();
Sign up to request clarification or add additional context in comments.

Comments

2

It kind of looks like you are mixing symfony 1 and 2.

In symfony2 you would most likely use the NativeQuery methods to achieve this. (see here http://docs.doctrine-project.org/en/latest/reference/native-sql.html)

Comments

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.