PHP Symfony - link path() - how to get related id not entity
Hi,
I have a General entity in PHP Symfony and a OneToMany GeneralProperites relation to it. GeneralProperties has a general_id column in the related table. When I want to make a link via a path to get from the GeneralProperties show with an ID back to the General's parents via its ID, it ends in an error:
CHYBA:
Neither the property "generalId" nor one of the methods "generalId()", "getgeneralId()"/"isgeneralId()"/"hasgeneralId()" or "__call()" exist and have public access in class "App\Entity\GeneralProperties".
If link like this
ERROR:
An exception has been thrown during the rendering of a template ("Warning: preg_match() expects parameter 2 to be string, object given").
If i use dd()
so it refers to proxies, probably related to Lazy loading
Entity GeneralProperties
Hi,
To resolve an error in PHP Symfony between OneToMany General / GeneralProperites when you cannot get a general id from GeneralProperites, follow these steps:
Solution:
Add a getGeneralId () method to the GeneralProperties class
Entity/GeneralProperties.php
Twig template - link:
I have a General entity in PHP Symfony and a OneToMany GeneralProperites relation to it. GeneralProperties has a general_id column in the related table. When I want to make a link via a path to get from the GeneralProperties show with an ID back to the General's parents via its ID, it ends in an error:
< a href="{{ path('general_show', {id: generalProperties.generalId }) }}" class="">
CHYBA:
Neither the property "generalId" nor one of the methods "generalId()", "getgeneralId()"/"isgeneralId()"/"hasgeneralId()" or "__call()" exist and have public access in class "App\Entity\GeneralProperties".
If link like this
< a href="{{ path('general_show', {id: generalProperties.general }) }}" class="">
ERROR:
An exception has been thrown during the rendering of a template ("Warning: preg_match() expects parameter 2 to be string, object given").
If i use dd()
dd($generalProperties);
so it refers to proxies, probably related to Lazy loading
GeneralPropertiesController.php on line 56:
App\Entity\GeneralProperties {#991 ▼
-id: 4
-general: Proxies\__CG__\App\Entity\General {#1022 ▶}
-name: "Kč"
-value: "test related"
}
Entity GeneralProperties
namespace App\Entity;
use App\Repository\GeneralPropertiesRepository;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=GeneralPropertiesRepository::class)
*/
class GeneralProperties
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity=General::class, inversedBy="generalProperties")
* @ORM\JoinColumn(nullable=false)
*/
private $general;
.....................
public function getGeneral(): ?General
{
return $this->general;
}
public function setGeneral(?General $general): self
{
$this->general = $general;
return $this;
}
REPLY
Hi,
To resolve an error in PHP Symfony between OneToMany General / GeneralProperites when you cannot get a general id from GeneralProperites, follow these steps:
Solution:
Add a getGeneralId () method to the GeneralProperties class
Entity/GeneralProperties.php
namespace App\Entity;
use App\Repository\GeneralPropertiesRepository;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=GeneralPropertiesRepository::class)
*/
class GeneralProperties
{
public function getGeneralId()
{
$general = $this->general;
return $general->getId();
}
Twig template - link:
< a href="{{ path('general_show', {id: generalProperties.generalId }) }}" class="">
General
< /a>