【问题标题】:How to add multiple ID on an element in Twig如何在 Twig 中的元素上添加多个 ID
【发布时间】:2021-03-30 04:00:11
【问题描述】:

我需要在我的 URL 中添加来自我数据库中 2 个不同表的 2 个不同 ID(以获取有关每个 id 的所有信息)。

我遇到了这个异常:

属性“car”和方法“car()”、“getcar()”/“iscar()”/“hascar()”或“__call()”都不存在并且在类中具有公共访问权限“应用\实体\报价”。

我不擅长CustomRepository,所以我想知道是否有其他方法。

这是我的控制器:

/**
 * @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

    ]);
}

这是我的树枝模板:

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

数据库:
表 1 汽车实体

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

表 2 潜在实体

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

引用实体

【问题讨论】:

  • 您不能将相同的参数id 传递两次。我建议您重新编写代码以获得更具体的参数,例如prospect_idcar_id.
  • 当我更改我的参数:"@Route("/individualQuote/{prospect_id}"" 和我的树枝模板:"&lt;a href="{{ path('user_individualQuote', {prospect_id: quote.prospect.id}) }}" 我收到此错误:"无法猜测如何从参数 "prospect" 的请求信息中获取 Doctrine 实例。”
  • 表1和表2分别代表什么?哪些实体?
  • @MAZux 我刚刚编辑了我的帖子:汽车和潜在客户实体
  • 是特定汽车的报价单吗? Car 和 Quote 之间没有关系,只是它们具有相同的 Prospect,每个 Prospect 可以有很多。您希望如何匹配它们?

标签: php sql symfony twig


【解决方案1】:

如果您在这两个实体之间没有直接关系,那么这是我在docs 中找到的示例:

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

{id} 似乎只会自动找到第一个实体。您必须指定如何查找其他实体。

试试这个:

/**
 * @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
{
}

并在树枝中构建路径:

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

【讨论】:

  • 如您所见,我正在使用 for 循环。这个 for 循环与 Quoteagencies.quotes 一起工作,就像不同机构中的潜在客户过滤器(机构是一个实体)。 {id: quote.prospect.id}) }}"&gt; {{ quote.prospect.fullname }} 是获取我想要的潜在客户列表的必要条件。
  • 但我尝试了你的解决方案,但我遇到了一个异常'变量“汽车”不存在。'
【解决方案2】:

我不确定您的数据库结构,但是根据您所显示的内容,我猜您有一个 Quote 实体。为简单起见,您可以使用 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(),    
    ]);
}

在树枝上:

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

【讨论】:

  • 事实上我需要做这样的事情:{{ path('user_individualQuote', {id: quote.prospect.car.id}) }}但我得到“@ParamConverter 注释找不到 App\Entity\Car 对象。”异常
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-01-23
  • 1970-01-01
  • 2019-01-28
  • 1970-01-01
  • 2018-11-04
  • 2021-02-09
  • 1970-01-01
相关资源
最近更新 更多