-1
 $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;
    }
}

我有测试联系人数据

在此处输入图像描述

我想我更喜欢这种物体

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

或任何东西

4

1 回答 1

0

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

于 2019-10-30T13:04:31.773 回答