PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, March 4, 2022

[FIXED] How can I show another Entity linked to the main one on html.twig?

 March 04, 2022     entity, php, symfony, twig     No comments   

Issue

I have a problem with showcasing an Entity's property on another one's html.twig that is linked.

Basically, one entity called Cats, one Entity called Appointment relation ManyToOne (one cat can have several appointments but one appointment is linked to one cat only)

Cats entity:

/**
 * @ORM\OneToMany(targetEntity=Appointment::class, mappedBy="cat", orphanRemoval=true)
 */
private $appointments;

/**
 * @return Collection|Appointment[]
 */
public function getAppointments(): Collection
{
    return $this->appointments;
}

public function addAppointment(Appointment $appointment): self
{
    if (!$this->appointments->contains($appointment)) {
        $this->appointments[] = $appointment;
        $appointment->setCat($this);
    }

    return $this;
}

public function removeAppointment(Appointment $appointment): self
{
    if ($this->appointments->removeElement($appointment)) {
        // set the owning side to null (unless already changed)
        if ($appointment->getCat() === $this) {
            $appointment->setCat(null);
        }
    }

    return $this;
}

Appointment entity:

/**
 * @ORM\ManyToOne(targetEntity=Cats::class, inversedBy="appointments", cascade={"persist"} )
 * @ORM\JoinColumn(nullable=false)
 */
private $cat;

public function getCat(): ?Cats
{
    return $this->cat;
}

public function setCat(?Cats $cat): self
{
    $this->cat = $cat;

    return $this;
}

And here is what I tried to do in html.twig for appointment_show

{% extends 'base.html.twig' %}
{% block title %}Appointment{% endblock %}
{% block main %}

<h1>Appointment</h1>

{% for cat in appointment.cats %}
    <div>
        <td>{{ appointment.cat_id }}</td>
    </div>
{% endfor %}

So I keep getting for error:

Neither the property "cats" nor one of the methods "cats()", "getcats()"/"iscats()"/"hascats()" or "__call()" exist and have public access in class "App\Entity\Appointment".

Can you help?


Solution

Thank you, I changed my code to the following and it worked:

{% for appointment in appointments %}
    {% set cat = appointment.cat %}
        <div>
            {{ cat.id }}
        </div>
{% endfor %}


Answered By - Helene Lansiaux
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing