PHP Symfony - link path() - jak získat vazební ID, nikoliv celou related entitu

PHP Symfony - link path() - jak získat vazební ID, nikoliv celou related entitu

Dobrý den,

mám v PHP Symfony entitu General a k ní relation OneToMany GeneralProperites. GeneralProperties má v tabulce vazební (related) sloupec general_id. Když chci udělat link přes path, abych se z show GeneralProperties s nějakým ID dostal zpět na rodiče General přes jeho ID, tak to končí chybou:

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


Když je link ve tvaru
< a href="{{ path('general_show', {id: generalProperties.general }) }}" class="">
CHYBA:
An exception has been thrown during the rendering of a template ("Warning: preg_match() expects parameter 2 to be string, object given").

Když udělam dd() a zajímá mě "general"
dd($generalProperties);
tak se tam odkazuje na proxies, asi souvislost s 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"
}


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




ODPOVĚĎ


Dobrý den,

chybu v PHP Symfony mezi relation OneToMany General / GeneralProperites, kdy nemůžete získat general id z GeneralProperites, vyřešíte takto:

Řešení:
Do třídy GeneralProperties přidat metodu getGeneralId()


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 šablona - link:

< a href="{{ path('general_show', {id: generalProperties.generalId }) }}" class="">
General
< /a>


Show english version

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.