2

Without scrutinizing why I want this (it may sound like a bad approach, but I have good reason) I want to know if there is a way in the standard-framework-edition 3.1+ to create a relational association to an entity that may not exist...

Firstly I do realize this determines the schema and that's fine. So if an entity does not exist, it doesn't create a foreign key and the field is always null, or if the target entity does exist, it creates the foreign key and the field works like a normal association...

Secondly, this only changes project to project, and may change down the line as an update to which I realize a manual schema update could be necessary.

Preferably without 3rd party bundle dependencies... hoping for the standard framework to do this,

Anybody? Thanks in advance

Edit

I am using annotations in my entities with doctrine ORM

Furthermore

The simplest version of why I am doing this is because certain bundles are optional project-to-project, and bundle A may make use of entities in bundle B only if it is present. I have considered using services and if container->has then container->get, or the XML on-invalid="null" approach, but that doesn't address property persistence. I was happy with storing a non-mapped value as a custom relational field, which is fine, just lengthier and wondered if perhaps there was a way Doctrine could ignore a missing targetEntity...

2 Answers 2

3

Hm, perhaps I misunderstand your question, but this sounds like a normal 'nullable' association to me?

Create your assocation via annotation:

/** * * @var Child * @ORM\ManyToOne(targetEntity="Child") */ private $child; 

and use

setChild(Child $child = null) { $this->child = $child; } 

as a Setter to allow nullable values.

And your getter might look like:

getChild() { return $this->child; } 

In case there isn't any child it will return null.

Sign up to request clarification or add additional context in comments.

7 Comments

Nope, not quite that simple, the entity in targetEntity= may or may not exist, and if it doesn't, an exception is understandably raised, so I want the ability for doctrine to ignore that...?
so you'll have to rephrase your question and indeed explain why you want to do that. to me it does not make sense at all. why not creating the entity and if you don't want to use it, simple don't use it.
My question explains that the entity may not exist, and it also says there's no need to scrutinize why I'm doing it, the questions is, can it be done... sorry to be blunt, but it's all there
Thanks for that, didn't know about it. But it's still going to fail, because on the case that AppBundle\Entity\Customer doesn't exist, I am still going to get an exception. Added some info to my question, to vaguely explain myself... hopefully makes sense...
|
1

I will keep the other answer as it responds to the question for a 'nullable association target' live data.

This is the answer for a 'nullable association target' meta data which is a different thing.

OP asks to provide a targetEntity in the metadata which cannot exist in his case, e.g. is not there in a different bundle (or whatever OP's mysterious reason might be).

In that case I recommend to build upon Doctrine's TargetEntityListener which is able to resolve the targetEntity during runtime and targetEntity can be set to an Abstract Class or an Interface:

 /** * @ORM\ManyToOne(targetEntity="Acme\InvoiceBundle\Model\InvoiceSubjectInterface") * @var InvoiceSubjectInterface */ protected $subject; 

InvoiceSubjectInterface will then be replaced during runtime by a specific class provided by config e.g.:

# app/config/config.yml doctrine: # ... orm: # ... resolve_target_entities: Acme\InvoiceBundle\Model\InvoiceSubjectInterface: AppBundle\Entity\Customer 

So this should be eiter an extendable behaviour for providing no class or implementing an own solution.

1 Comment

This is a great suggestion thanks, will explore and see if it suits my needs... you are a legend sir

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.