How to access saved entity via metadata using parameters in the route

  entity, symfony4, twig

I have an entity contact

<?php

namespace AppEntity;

use AppRepositoryContactRepository;
use DoctrineORMMapping as ORM;

/**
 * @ORMEntity(repositoryClass=ContactRepository::class)
 */
class Contact
{
    /**
     * @ORMId
     * @ORMGeneratedValue
     * @ORMColumn(type="integer")
     */
    private $id;

    /**
     * @ORMColumn(type="string", length=50)
     */
    private $firstName;

    /**
     * @ORMColumn(type="string", length=50)
     */
    private $lastName;

    /**
     * @ORMColumn(type="date", nullable=true)
     */
    private $dob;

    /**
     * @ORMColumn(type="string", length=50)
     */
    private $email;

    
    /**
     * @ORMColumn(type="string", length=15)
     */
    private $phone;

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

    public function getFirstName(): ?string
    {
        return $this->firstName;
    }

    public function setFirstName(string $firstName): self
    {
        $this->firstName = $firstName;

        return $this;
    }

    public function getLastName(): ?string
    {
        return $this->lastName;
    }

    public function setLastName(string $lastName): self
    {
        $this->lastName = $lastName;

        return $this;
    }

    public function getDob(): ?DateTimeInterface
    {
        return $this->dob;
    }

    public function setDob(?DateTimeInterface $dob): self
    {
        $this->dob = $dob;

        return $this;
    }

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

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

        return $this;
    }

    public function getPhone(): ?string
    {
        return $this->phone;
    }

    public function setPhone(string $phone): self
    {
        $this->phone = $phone;

        return $this;
    }
}

I want to select one (or more) contact with this URL: /c00210/readallbyparam/{routeProperty}/{routeValue}

For example: /c00210/readallbyparam/lastname/Mendelsohn
This should return 2 records:

  • Felix Mendelsohn
  • Fanny Mendelsohn

How do I write the render() function?
How do I write the twig instructions?

I suppose I should use the metadata, but I don’t know

Source: Symfony4 Questions

LEAVE A COMMENT