B
B
BonBon Slick2018-05-22 02:14:36
symfony
BonBon Slick, 2018-05-22 02:14:36

Difference of native, without DQL queries?

Which of the 3 native queries to use, why?

$conn = $this->entityManager->getConnection();
            $sql = '
              SELECT * FROM users  
              ORDER BY created_at ASC
            ';
            $stmt = $conn->prepare($sql);
            $stmt->execute();
             return $stmt->fetchAll();

or
$conn = $this->entityManager->getConnection();
            $sql = '
              SELECT * FROM users  
              ORDER BY created_at ASC
            ';
            return $conn->query($sql)->fetchAll();

this is already with hydration in objects, which can be manually registered above
$query = $this->entityManager->createQuery(
                'SELECT p
                    FROM App\Domain\User\Model\Entity\User p
                    ORDER BY p.createdAt ASC'
            );
            return $query->execute();

here with an alias and an array in the execut
$sql = '
            SELECT * FROM users p
            ORDER BY p.created_at ASC
            ';
            $stmt = $conn->prepare($sql);
            $stmt->execute([]);
            return $stmt->fetchAll();

and further
$query = $this->entityManager->createNativeQuery(
                'SELECT alias
                    FROM users alias'
            , new ResultSetMapping());
            return $query->getResult();

1, 2 and 4 return the same results, arrays that can be hydrated.
The 3rd option will return the objects as if using DQL.
4 - option from off-docs https://www.doctrine-project.org/projects/doctrine... will return an empty array, all previous requests from symfony's docks https://symfony.com/doc/current/doctrine.html

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question