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

Autor Phoenix
Publikováno 28.10.2020
Kategorie programovani
Stav Vyřešeno
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ědi na dotaz

The English language is translated by machine - the translator can modify eg cited codes = it is better to use codes from the Czech original.

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


RE: PHP Symfony - link path() - jak získat vazební ID…

Autor Hanz
Publikováno 02.11.2020
Kategorie programovani
Stav Zkontrolováno
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>



The English language is translated by machine - the translator can modify eg cited codes = it is better to use codes from the Czech original.

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

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>


Tento web používá k poskytování služeb, personalizaci reklam a analýze návštěvnosti soubory cookie. Používáním tohoto webu s tím souhlasíte. Další informace