1

A2lix required 'id' as referenceColumName. But I've entities with a different columName (e.g. ProductID instead of id). So I get error when I try to update my DB via doctrine:schema:update --force.

Example of my entity

class Period
{

    use ORMBehaviors\Translatable\Translatable,
        ORMBehaviors\Timestampable\Timestampable,
        ORMBehaviors\SoftDeletable\SoftDeletable
    ;

    /**
     * @var integer
     *
     * @ORM\Column(name="PeriodID", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

...

Part of the TranslatableSubscriber class of KnpDoctrineBehaviors Package

...

   private function mapTranslatable(ClassMetadata $classMetadata)
    {
        if (!$classMetadata->hasAssociation('translations')) {
            $classMetadata->mapOneToMany([
                'fieldName'     => 'translations',
                'mappedBy'      => 'translatable',
                'indexBy'       => 'locale',
                'cascade'       => ['persist', 'merge', 'remove'],
                'fetch'         => $this->translatableFetchMode,
                'targetEntity'  => $classMetadata->getReflectionClass()->getMethod('getTranslationEntityClass')->invoke(null),
                'orphanRemoval' => true
            ]);
        }
    }

    private function mapTranslation(ClassMetadata $classMetadata)
    {
        if (!$classMetadata->hasAssociation('translatable')) {

            $classMetadata->mapManyToOne([
                'fieldName'    => 'translatable',
                'inversedBy'   => 'translations',
                'fetch'         => $this->translationFetchMode,
                'joinColumns'  => [[
                    'name'                 => 'translatable_id',
                    'referencedColumnName' => 'id',
                    'onDelete'             => 'CASCADE'
                ]],
                'targetEntity' => $classMetadata->getReflectionClass()->getMethod('getTranslatableEntityClass')->invoke(null),
            ]);
        }

        $name = $classMetadata->getTableName().'_unique_translation';
        if (!$this->hasUniqueTranslationConstraint($classMetadata, $name)) {
            $classMetadata->table['uniqueConstraints'][$name] = [
                'columns' => ['translatable_id', 'locale' ]
            ];
        }

        if (!$classMetadata->hasField('locale')) {
            $classMetadata->mapField(array(
                'fieldName' => 'locale',
                'type'      => 'string'
            ));
        }

    }

...

Any idea or help would be appreciate.

Many thanks ;-)

Benjamin Lucas
  • 370
  • 5
  • 20
  • Hey Benjamin, I have the same requirement. Did u find a good way to set a custom 'referencedColumnName'? – vbenitogo May 28 '15 at 18:14

1 Answers1

1

You can use: association override

I tried to override properties but they are created by the subscriber and I got exceptions. You can also re-declare the annotation manyToOne on your entity class. It works for me.

R Picheta
  • 630
  • 9
  • 16
  • Can you be more specific in this answer? Which association are you actually overriding? on the provided sample code, how would this look like? – miqwit Jul 27 '22 at 13:43