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:

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

General

Jak se ti článek líbil?

Komentáře (0)

Přidat komentář