1

I use Symfony doctrine to set and get data from my MySQL database. I can push new data without any problem but when I try to get them with a findAll for exemple, I get an array with the good length but nothing in.

Here's my controller:

namespace KGN\CoreBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\JsonResponse; 
use Symfony\Component\HttpFoundation\Response;
use Doctrine\ORM\EntityManagerInterface;
use KGN\CoreBundle\Entity\Appointment;
use KGN\CoreBundle\Entity\Testy;

class AdminController extends Controller
{
    public function indexAction()
    {
        return $this->render('KGNCoreBundle:Admin:index.html.twig');
    }


    public function aptAction()
    {

        $rep = $this->getDoctrine()
                                ->getRepository('KGNCoreBundle:Testy');

        $testy = $rep->findAll();


        // return new Response('This is for show : '. count($testy) );
        return new JsonResponse($testy);

    }

    public function createAction()
    {

        $em = $this->getDoctrine()->getManager();

        $testy = new Testy();

        $testy->setTitre('Magnifique');
        $testy->setName('Helicoptere');

        $em->persist($testy);

        $em->flush();

        return new Response('This is for create');

    }

}

and what I get on my view page

[{},{}]

And it's true that there is 2 elements in my SQL table.

( I have create my entity with php bin/console doctrine:generate:entity without edition stuff in the "Testy" class or rep )

Entity/Testy

namespace KGN\CoreBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Testy
 *
 * @ORM\Table(name="testy")
 * @ORM\Entity(repositoryClass="KGN\CoreBundle\Repository\TestyRepository")
 */
class Testy
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="titre", type="string", length=255)
     */
    private $titre;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255)
     */
    private $name;


    /**
     * Get id
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set titre
     *
     * @param string $titre
     *
     * @return Testy
     */
    public function setTitre($titre)
    {
        $this->titre = $titre;

        return $this;
    }

    /**
     * Get titre
     *
     * @return string
     */
    public function getTitre()
    {
        return $this->titre;
    }

    /**
     * Set name
     *
     * @param string $name
     *
     * @return Testy
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }
}

associed rep

namespace KGN\CoreBundle\Repository;

/**
 * TestyRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class TestyRepository extends \Doctrine\ORM\EntityRepository
{
}
4
  • Could we see the mapping? Commented Aug 29, 2017 at 17:05
  • sure, i've edited the post Commented Aug 29, 2017 at 17:08
  • 1
    I thing you have to iterate the array. Try and tell..Regards Commented Aug 29, 2017 at 17:20
  • Already tryed, but that gave me an error when I try to get test.title for exemple Commented Aug 29, 2017 at 18:00

2 Answers 2

2

Hi The Function findAll Return the correct answer but its return as array of Objects And JsonResponse can't desplay Object.
to fixe that you have to create a custom function in your repository that return An array exemple

public function getAll() {
    $qb = $this->createQueryBuilder('u');     


    return $qb->getQuery()->getArrayResult();
}
Sign up to request clarification or add additional context in comments.

1 Comment

Will try this in the worst case. But I don't realy need the JsonResponse, it's just to try. But still have error when I try to passe this object into a twig for loop
1
$em = $this->getDoctrine()->getManager(); 
$records = $em->getRepository("KGNCoreBundle:Testy")->findAll();

Hope its help you

1 Comment

explain, it may be helpful to me if you explain

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.