getPerson()返回NULL,为什么?

时间:2014-09-06 16:34:32

标签: php symfony doctrine-orm doctrine symfony-2.5

我有两个与OrdersPerson相关的实体,其中一个Person可以有多个Orders。这些是实体的映射:

class Orders {

    /**
     * @ORM\ManyToOne(targetEntity="Person", inversedBy="orders")
     * @ORM\JoinColumn(name="person_id", referencedColumnName="id")
     * */
    protected $person;

    public function setPerson(Person $person)
    {
        $this->person = $person;
        return $this;
    }

    public function getPerson()
    {
        $this->person;
    }

}

class Person {

    /**
     * @ORM\Column(name="person_type", type="boolean", nullable=false)
     */
    protected $person_type = 1;

    /**
     * @ORM\OneToMany(targetEntity="NaturalPerson", mappedBy="person")
     * */
    private $naturals;

    /**
     * @ORM\OneToMany(targetEntity="LegalPerson", mappedBy="person")
     * */
    private $legals;

    /**
     * @ORM\OneToMany(targetEntity="Orders", mappedBy="person")
     * */
    private $orders;

    public function __construct()
    {
        $this->naturals = new ArrayCollection();
        $this->legals = new ArrayCollection();
        $this->orders = new ArrayCollection();
    }


    public function setPersonType($person_type)
    {
        $this->person_type = $person_type;
        return $this;
    }

    public function getPersonType()
    {
        return $this->person_type;
    }

    public function getNaturals()
    {
        return $this->naturals;
    }

    public function getLegals()
    {
        return $this->legals;
    }

    public function getOrders()
    {
        return $this->orders;
    }

}

在我的控制器中,我试图从Orders获取Person的相关记录,但我收到NULL,因为JSON显示:

{
   "data":[
      [
         "sdasdasd",
         null
      ],
      [
         "werwerwer",
         null
      ],
      [
         "sdfsdfsf435435",
         null
      ]
   ]
}

这就是我在控制器中获取数据的方式:

public function getOrdersAction()
{
    $response = array();
    $em = $this->getDoctrine()->getManager();

    $entities = $em->getRepository("FrontendBundle:Orders")->findAll();

    $orders = array();
    foreach ($entities as $entity)
    {
        $order = array();
        $order[] = $entity->getNickname();
        $order[] = $entity->getPerson();
        $orders[] = $order;
    }

    $response['data'] = $orders;
    return new JsonResponse($response);
}

我通过运行此查询来测试数据库表的值:

SELECT ord.nickname, ord.person_id, pn.id, pn.description FROM orders ord left join person pn on pn.id = ord.person_id

这就是结果:

enter image description here

所以记录是相关的,那么我做错了什么?

1 个答案:

答案 0 :(得分:4)

嗯...你只是想念"返回"。

public function getPerson()
{
   return $this->person;
}