Doctrine – rozširenie entity o column

sucho
Člen | 57
+
0
-

Zdravím @FilipProcházka máme modulárnu appku a nadišiel čas na rozšírenie entity

Potrebovali by sme rozšíriť OfferEntity o stĺpec company_id s cudzím kľúčom ManyToOne

Našiel som príklad https://www.theodo.fr/…nd-entities/ s tým že tam rieši ManyToMany

Vytvoril som class

class OfferEntityExtension extends Object implements Subscriber
{
    /** {@inheritDoc} */
    public function getSubscribedEvents()
    {
        return [Events::loadClassMetadata];
    }


    /**
     * @param LoadClassMetadataEventArgs $eventArgs
     */
    public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs)
    {
        $metadata = $eventArgs->getClassMetadata();

        if ($metadata->getName() == OfferEntity::class) $this->extendEntity($eventArgs, $metadata);
    }


    /**
     * @param LoadClassMetadataEventArgs $eventArgs
     * @param ClassMetadata $metadata
     *
     * @return ClassMetadata
     */
    private function extendEntity($eventArgs, $metadata)
    {
        return $metadata->mapManyToOne([
            'targetEntity' => CompanyEntity::class,
            'fieldName' => 'company',
            'inversedBy' => 'company',
            'cascade' => ['persist'],
            'joinTable' => [
                'name' => 'company',
                'joinColumns' => [
                    [
                        'name' => 'company_id',
                        'referencedColumnName' => 'id',
                        'onDelete' => 'SET NULL'
                    ]
                ]
            ]
        ]);
    }

}

Event je v configu registrovaný správne spúšťa sa (keď ho zakomentujem tak všetko funguje ale bez toho stĺpca)

services:
    - {class: Wame\OfferCompanyPlugin\Entities\OfferEntityExtension(), tags: [kdyby.subscriber]}

No vždy to končí na tejto chybe (hodil som sem exception)

Čo je divné že v tabuľke je uložené

create_user_id => 1
company_id => 1

ale do $hints dáva

createUser => 2
company => 2
David Matějka
Moderator | 6445
+
0
-

tohle nepujde, viz ten clanek:

Since a relation has been added to the BlogArticle, Symfony will expect that the corresponding setters and getters exist. This is a limitation of the dynamic mapping, you will still have to define them manually:

(jen teda s tim rozdilem, ze se autor myli a musi existovat property, ne getter a setter)

sucho
Člen | 57
+
0
-

A existuje iné riešenie?
alebo to urobiť cez relačnú tabuľku?

David Matějka
Moderator | 6445
+
0
-

relacni tabulka/entita muze byt reseni. pripadne mrkni sem, kde se ukazuji moznosti dedeni entit za pomoci „target entity mappings“

sucho
Člen | 57
+
0
-

Zabudol som že už máme nejaký EntitiyModifier vytvorený nikto ho nejak moc nepoužíval

je to Extension ktorý cez Nette\PhpGenerator vygeneruje vlastný súbor do temp/entities
potom sa traity s columnami pridávajú nejak takto

entityModifier:
    traits:
    	- {class: ArticleModule\Entities\ArticleEntity, trait: Core\Entities\Columns\Parameters}

chcelo by to trošku prečistiť a mohlo by sa to vypustiť aj von