How to get entity query result as Object

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

I am super new to symfony and doctrine but i trying to get it.

So i have some result from query build as doctrine as you can you

how i get this result as Json

as long i know how to to loop throughs it

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

beside that i i want get all result as 1 array Objects

I think question label is wrong hope someone will fix it for me because i dont explain it how sorry

This is contact Entity

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

i have test contact data as

enter image description here

and i think i prefer this kind of object

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

or anything

1

There are 1 best solutions below

0
On

If you are in a controller you can return to the object via => return new JsonResponse ($ objectList)