如何获取实体查询结果作为对象

时间:2019-10-30 09:52:23

标签: php symfony doctrine-orm symfony-4.3

 $contacts = $this->getDoctrine()->getManager()
                                ->getRepository(Contact::class)
                                ->createQueryBuilder('c')
                                ->select('c')
                                ->where('c.author = :id')
                                ->setParameter('id',$this->getUser()->getId())
                                ->getQuery()->getResult(); 

我对symfony和学说超级陌生,但是我试图得到它。

所以我从查询构建中得到了一些尽可能多的结论

我如何以Json的身份获得这个结果

只要我知道如何遍历它

foreach($contact as $contact){
  $contact->getId() // or whatever
}

除了我我想要将所有结果作为1个数组对象

我认为问题标签是错误的,希望有人会为我解决,因为我没有解释它的对不起

这是联系人实体

class Contact
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;
    /**
     * @ORM\Column(type="string", length=25)
     */
    private $Name;


    /**
     * @ORM\Column(type="string", length=10)
     * @Assert\NotBlank(message="Утасны дугаар оруулна уу")
     */
    private $mobile;

    /**
     * @ORM\Column(type="string", length=25, nullable=true)
     */
    private $email;


    /**
     * @ORM\Column(type="string", length=10, nullable=true)
     * @Assert\NotBlank(message="Утасны дугаар оруулна уу")
     */
    private $mobilesecond;

    /**
     * @ORM\Column(type="string", length=25, nullable=true)
     */
    private $work;

    /**
     * @ORM\Column(type="string", length=25, nullable=true)
     */
    private $position;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\user", inversedBy="contacts")
     */
    private $author;



    public function getId(): ?int
    {
        return $this->id;
    }

    public function getName(): ?string
    {
        return $this->Name;
    }

    public function setName(string $Name): self
    {
        $this->Name = $Name;

        return $this;
    }

    public function getEmail(): ?string
    {
        return $this->email;
    }

    public function setEmail(string $email): self
    {
        $this->email = $email;

        return $this;
    }

    public function getMobile(): ?string
    {
        return $this->mobile;
    }

    public function setMobile(string $mobile): self
    {
        $this->mobile = $mobile;

        return $this;
    }

    public function getMobilesecond(): ?string
    {
        return $this->mobilesecond;
    }

    public function setMobilesecond(string $mobilesecond): self
    {
        $this->mobilesecond = $mobilesecond;

        return $this;
    }

    public function getWork(): ?string
    {
        return $this->work;
    }

    public function setWork(string $work): self
    {
        $this->work = $work;

        return $this;
    }

    public function getPosition(): ?string
    {
        return $this->position;
    }

    public function setPosition(string $position): self
    {
        $this->position = $position;

        return $this;
    }

    public function getAuthor(): ?user
    {
        return $this->author;
    }

    public function setAuthor(?user $author): self
    {
        $this->author = $author;

        return $this;
    }
}

我的测试联系方式为

enter image description here

我想我更喜欢这种物体

{
 ['51','dfgds','sfg','gfsgfs','s','fg','fgs','1'],
 ['54','dfg','gdf','gd','fgdf','dfgdg','fgdg','1'],
 ['55','wer','','ewe','','','erwrewwer','1'],
}

或其他任何东西

1 个答案:

答案 0 :(得分:0)

如果您在控制器中,则可以通过=>返回对象,返回新的JsonResponse($ objectList)