PHP Symfony - link path() - how to get related id not entity

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


Související obsah

programovani

symfony

php

twig

doctrine

sql

Komentáře

Vaše reakce na PHP Symfony - link path() - jak získat vazební ID, nikoliv celou related entitu

Reference

Podívejte se na naše reference

Prohlédnout

Aplikace

Podívejte se na naše aplikace

Prohlédnout

Co umíme?

Podívejte se co umíme

Prohlédnout

Co umíme?

Vytváříme sofistikované aplikace pro náročné

Od webových aplikací přes android až po převodové můstky či složité informační systémy.

Podívejte se k nám

Máte ještě čas? Podívejte se na další rubriky

Tento web používá soubory cookie. Dalším procházením tohoto webu vyjadřujete souhlas s jejich používáním.. Více informací zde.