I am working with a legacy codebase, and a database that has no foreign key
constraints defined, nor has referential integrity.
And what I'm doing is I am modeling my Doctrine entities in PHP to match
MySQL tables, so that I can use Doctrine inside PHP code.
And so first I build my Entities to match the table and the data types in
the table. But frequently I come across a situation like so: I have two
Doctrine Entities: Item and Data
Actual schema & tables are:
item(id, data ... );
data(id, data, item_id); //where item_id is a foreign key to item
I also run these to help me generate the Entities:
vendor/bin/doctrine orm:convert-mapping --namespace="Entity\\" --force --
from-database annotation ./module/Operations/src/
vendor/bin/doctrine orm:generate-entities ./module/Operations/src/ --
generate-annotations=true
(^^ Doctrine reads MySQL tables and creates PHP code with Annotations and
getters and setters for me, and mostly gets it right)
Now, looking at the generated Data Entity, I see this:
class Data
{
/**
* @var integer
* @Column(name="item_id", type="integer", precision=0, scale=0,
nullable=false, unique=false)
*/
private $itemId;
}
That is all good. But I note that the foreign key is *not recognized. *There
is no relationship defined. My question is:
- Do I do anything about this? Do I leave it as is?
- Do I fix it up somehow? How? Make it into $item and define OneToMany
or ManyToOne relationships? Bidirectional, Onedirectional?
I know that Doctrine will handle things well either way I choose to go.
But I am not clear on which way is best for me.
I am looking for a best practice. For example, am I better in the long run
to always modify such foreign keys to define a Relationship of some type,
or do I leave them as-is, until some need arises?
I suppose not all foreign key situations are the same, but in this case, I
often run SQL queries like so:
SELECT * FROM data WHERE item_id = 3;
SELECT * FROM item JOIN data ON data.item_id = item.id;
Thanks!
Dennis
--
You received this message because you are subscribed to the Google Groups
"doctrine-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/doctrine-user.
For more options, visit https://groups.google.com/d/optout.