从我的数据库中获取数据 table

get data from my database table

我想从我的数据库中获取数据 table "Client",但我得到了这个结果:

[{},{},{}]

这是我的代码:

 <?php
    namespace OP\OPBundle\Controller;
    use Symfony\Bundle\FrameworkBundle\Controller\Controller;
    use OP\OPBundle\Entity\Client;
    class ClientAPIController extends Controller
    {
        public function indexAction()
        {

            $em = $this->getDoctrine()->getManager();
           $personne = $em->getRepository('OPOPBundle:Client')->findAll();
    //    $personne_tab=array();
    //    $personne_tab['nom']=$personne->getNom();
    //    $personne_tab['prenom']=$personne->getPrenom();
    //   $personne_tab['id']=$personne->getId();
        $personntojson=  json_encode($personne);
            return $this->render('OPOPBundle:ClientAPI:index.html.twig', array(
                    'reponse'=> $personntojson
                ));    }

这是index.html.twig:

{{reponse}}

很简单。 json_encode 将仅 present/convert public 可用数据。对象中的所有成员可能都是私有的。

您可以:

  • 加入成员public
  • 使用 JMSSerializerBundle
  • 用序列化信息注释您的模型
  • 或者如果你是 运行 php > 5.4 实现 JsonSerializable 接口,然后添加一个 jsonSerialize 方法,你可以自己进行转换。