-1
 $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
    maybe you want to show us, how a contact object looks like and what the desired result is ... – Jakumi Oct 30 '19 at 09:56
  • 1
    people are quick to downvote, sadly. However, I think you should take a look at the JsonSerializable interface (php standard interface), implement it on your Contact entity and then json_serialize the array of objects. – Jakumi Oct 30 '19 at 10:09
  • i tried serializer but its not compatible because serializer works on object to json but its entity is not object at first place – Munkhdelger Tumenbayar Oct 30 '19 at 10:12
  • sorry, I meant `json_encode`, when you implement the JsonSerializable interface (https://www.php.net/JsonSerializable) on your Contact entity and call `json_encode` on an array of Contact entities, it will call the jsonSerialize method on each entity, so you can decide what is in your result. also, your desired output is not valid json (just saying). (and, entities *are* objects) – Jakumi Oct 30 '19 at 10:15
  • hmm i wonder why its comes [{},{},{}]. I used json_encode – Munkhdelger Tumenbayar Oct 30 '19 at 10:28
  • have you implemented the JsonSerializable interface though? (also with `implements \JsonSerializable` in the class header). if so, how does your jsonSerialize method look? – Jakumi Oct 30 '19 at 10:35

1 Answers1

0

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

popota
  • 1
  • 2