0

If I use this method to retrieve data I cannot parse it in my PHP like this $data[0]['string']. PHP just says this is an Object and it cannot parse it as Array

error message

Cannot use object of type App\Entity\MyEntity as array

Controller code:

$data = $this->em
     ->getRepository(Myclass::class)
     ->findBy(['foo' => $var]);

How can I return a parsable array from a simple findBy() directly from the controller without having to go through the Repository and QueryBuilder with ->getArrayResult() ?

6
  • 3
    What does "it does not work" mean? Do you get any error message? What's the behaviour you observe? What did you expect instead? Commented Aug 14, 2018 at 14:47
  • I updated the question hopefully this is clearer. I just want to be able to parse my array in PHP with the first method Commented Aug 14, 2018 at 14:49
  • What's wrong with ->findBy(['foo' => $var])->toArray( ); ? Commented Aug 14, 2018 at 17:07
  • @Paulpro would be awesome but does not work for me: undefined function Commented Aug 14, 2018 at 18:49
  • Are you sure you're not using findOneBy? findBy should return a doctrine collection, not an individual entity as the error message you posted shows. Commented Aug 14, 2018 at 18:51

1 Answer 1

2

All you need to do is to create your custom function in your repository:

public function findItByFoo($var){
        $qb = $this->createQueryBuilder('myClass');
        $qb->select("myClass")
            ->where('foo = :var')
            ->setParameter('var', $var);
        $result = $qb->getQuery()->getArrayResult();
        return $result;
}

You cannot achieve this with built-in findBy method. More specifically, the findBy definition is:

$repository->findBy(array $criteria, array $orderBy = null, $limit  = null, $offset = null);

As you can see for yourself there is not any argument for the hydration mode.

Finally, since you want to hydrate your entities as arrays instead of objects, and you want to do that in your controller, the only solution is something like this:

$data = $this->em->getRepository(Myclass::class)->createQueryBuilder('m')
            ->select("m")->where('foo = :var')->setParameter('var', $var)
            ->getQuery()->getArrayResult();

As you can see, it almost identical with the code in the findItByFoo() function I wrote above, I just wanted to mark that you can do that also in your controller (even not suggested, programmatically speaking).

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

3 Comments

Hello, I understand what you mean and sorry if my question was not clear enough but I am asking if there is a solution without having to go through the repository and querybuilder. Just output an array from a simple findBy() in my controller
Do you need to have an array of objects or an array of arrays?
I need an array of array

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.