Handling Doctrine Relations with a Read-Only View in Symfony

25 Views Asked by At

I am working on a Symfony project using Doctrine ORM, and I've encountered a challenge regarding entity relations with a read-only view. I have an entity named TelephoneQuestionnaire which is related to a read-only view Customer. Here are the relevant parts of my entity definitions:


        // TelephoneQuestionnaire Entity

/**
 * @ORM\ManyToOne(inversedBy: 'telephoneQuestionnaires')
 * @ORM\JoinColumn(name: "customer_id", referencedColumnName: "KOD_KLIENTA", nullable: false)
 */
private ?Customer $customer = null;

// Customer Entity (Read-Only View)

/**
 * @ORM\OneToMany(mappedBy: 'customer', targetEntity: TelephoneQuestionnaire::class)
 */
private Collection $telephoneQuestionnaires;

public function __construct()
{
    $this->telephoneQuestionnaires = new ArrayCollection();
}

In theory, everything seems fine. However, when I use Doctrine's migration tool, it tries to generate a foreign key constraint for the telephone_questionnaires table:

ALTER TABLE telephone_questionnaires ADD CONSTRAINT FK_23D75C8D9395C3F3 FOREIGN KEY (customer_id) REFERENCES TABLE_NAME (KOD_KLIENTA)

The problem is, it's not possible to create a foreign key constraint to a view, which is the case with my Customer entity.

I am looking for a solution or a workaround to handle this situation where Doctrine attempts to create a foreign key to a view, which is not feasible. Any suggestions on how to manage entity relationships in Symfony when one of the entities is a read-only view would be greatly appreciated.

0

There are 0 best solutions below