How to add multiple ID on an element in Twig

本小妞迷上赌 提交于 2021-01-07 02:21:48

问题


I need to add 2 different ID from 2 different table in my database (to get all informations about each id) in my URL.

I got this exception:

Neither the property "car" nor one of the methods "car()", "getcar()"/"iscar()"/"hascar()" or "__call()" exist and have public access in class "App\Entity\Quote".

I'm not good with CustomRepository so I am wondering if there is an other way.

This is my controller:

/**
 * @Route("/individualQuote/{id}", name="user_individualQuote", methods={"GET","POST"})
 **/
public function getIndividualQuote(Prospect $prospect,Car $car): Response
{
    return $this->render('user/_individualQuote.html.twig', [
        'prospect' => $prospect,

    ]);
}

/**
 * @Route("/carInfo/{id}", name="user_carInfo", methods={"GET","POST"})
 **/
public function getQuote(Car $car): Response
{
    return $this->render('user/_individualQuote.html.twig', [
        'car'=>$car

    ]);
}

This is my twig template :

<ul>
    {% for quote in agency.quotes %}
        {{ dump(quote.prospect.id) }}
        <li><a href="{{ path('user_individualQuote', {id: quote.prospect.id,id:quote.car.id}) }}"> {{ quote.prospect.fullname }}</a></li>
    {% endfor %}

</ul>

Database:
table 1 car entity

class Car
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $model;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $brand;

    /**
     * @ORM\ManyToOne(targetEntity=Prospect::class, inversedBy="cars")
     * @ORM\JoinColumn(nullable=false)
     */
    private $prospect;

    /**
     * @ORM\Column(type="smallint")
     */
    private $power;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $gas;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $registration;

    /**
     * @ORM\Column(type="boolean")
     */
    private $insuranceExist;

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

    public function getModel(): ?string
    {
        return $this->model;
    }

    public function setModel(string $model): self
    {
        $this->model = $model;

        return $this;
    }

    public function getBrand(): ?string
    {
        return $this->brand;
    }

    public function setBrand(string $brand): self
    {
        $this->brand = $brand;

        return $this;
    }

    public function getProspect(): ?Prospect
    {
        return $this->prospect;
    }

    public function setProspect(?Prospect $prospect): self
    {
        $this->prospect = $prospect;

        return $this;
    }

    public function getPower(): ?int
    {
        return $this->power;
    }

    public function setPower(int $power): self
    {
        $this->power = $power;

        return $this;
    }

    public function getGas(): ?string
    {
        return $this->gas;
    }

    public function setGas(string $gas): self
    {
        $this->gas = $gas;

        return $this;
    }

    public function getRegistration(): ?string
    {
        return $this->registration;
    }

    public function setRegistration(string $registration): self
    {
        $this->registration = $registration;

        return $this;
    }

    public function getInsuranceExist(): ?bool
    {
        return $this->insuranceExist;
    }

    public function setInsuranceExist(bool $insuranceExist): self
    {
        $this->insuranceExist = $insuranceExist;

        return $this;
    }

    public function __toString()
    {
        return $this->getBrand() . " " . $this->getModel();
    }
}

table 2 prospect entity

class Prospect
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="text")
     */
    private $lastName;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $firstName;

    /**
     * @ORM\Column(type="date")
     */
    private $birthdate;

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

    /**
     * @ORM\Column(type="text", nullable=true)
     */
    private $phoneNumber;

    /**
     * @ORM\Column(type="guid")
     *
     */
    private $guid;

    /**
     * @ORM\Column(type="datetime")
     */
    private $createdAt;

    /**
     * @ORM\OneToMany(targetEntity=Car::class, mappedBy="prospect", orphanRemoval=true)
     */
    private $cars;

    /**
     * @ORM\OneToMany(targetEntity=Quote::class, mappedBy="prospect")
     */
    private $quotes;

    /**
     * @ORM\ManyToOne(targetEntity=City::class, inversedBy="prospects")
     * @ORM\JoinColumn(nullable=false)
     */
    private $city;

    public function __construct()
    {
        $this->cars = new ArrayCollection();
        $this->quotes = new ArrayCollection();
    }

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

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

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

        return $this;
    }

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

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

        return $this;
    }


    public function getBirthdate(): ?\DateTimeInterface
    {
        return $this->birthdate;
    }

    public function setBirthdate(\DateTimeInterface $birthdate): self
    {
        $this->birthdate = $birthdate;

        return $this;
    }

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

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

        return $this;
    }

    public function getPhoneNumber(): ?string
    {
        return $this->phoneNumber;
    }

    public function setPhoneNumber(?string $phoneNumber): self
    {
        $this->phoneNumber = $phoneNumber;

        return $this;
    }

    public function getGuid(): ?string
    {
        return $this->guid;
    }

    public function setGuid(string $guid): self
    {
        $this->guid = $guid;

        return $this;
    }

    public function getCreatedAt(): ?\DateTimeInterface
    {
        return $this->createdAt;
    }

    public function setCreatedAt(\DateTimeInterface $createdAt): self
    {
        $this->createdAt = $createdAt;

        return $this;
    }


    /**
     * @ORM\PrePersist()
     */
    public function prePersist() {
        $this->setCreatedAt(new \DateTime());
    }

    /**
     * @return Collection|Car[]
     */
    public function getCars(): Collection
    {

        return $this->cars;
    }
}

Quote entity


回答1:


If you have no direct relation between the two entities then this is the example I found in the docs:

/**
 * @Route("/blog/{id}/comments/{comment_id}")
 * @Entity("comment", expr="repository.find(comment_id)")
 */
public function show(Post $post, Comment $comment)
{
}

It appears that only the first entity will be automatically found by {id}. You must specify how to find additional entities.

Try this:

/**
 * @Route("/individualQuote/{id}/{car_id}", name="user_individualQuote", methods={"GET","POST"})
 * @Entity("car", expr="repository.find(car_id)")
 */
public function getIndividualQuote(Prospect $prospect, Car $car): Response
{
}

And to build the path in twig:

{{ path('user_individualQuote', {id: prospect.id, car_id: car.id}) }}



回答2:


I'm not sure about your db structure but, from what you've shown I'm guessing you have a Quote entity. To keep it simple you could just use the Quote:

/**
 * @Route("/individualQuote/{id}", name="user_individualQuote", methods={"GET","POST"})
 */
public function getIndividualQuote(Quote $quote): Response
{    

    return $this->render('user/_individualQuote.html.twig', [
        'prospect' => $quote->getProspect(),
        'car'      => $quote->getCar(),    
    ]);
}

And in twig:

{{ path('user_individualQuote', {id: quote.id}) }}


来源:https://stackoverflow.com/questions/65374981/how-to-add-multiple-id-on-an-element-in-twig

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!