Does Doctrine 2 required foreign keys to be defined? - php

I have a relational database, however, there aren't any foreign keys defined, and many of the tables are not InnoDB, so can't support foreign keys anyway.
I would like to start experimenting with Doctrine on this database, but there is a lot of reference to foreign keys in the documentation, so I'm not sure if this is possible.
I'm anxious about diving in and changing table storage engines to InnoDB and defining tons of foreign keys, because I don't want to impact performance if I don't have to.
Does Doctrine require foreign keys to be defined in order to manage associations between entities?
If it isn't required but optional, what features of Doctrine do I sacrifice if I don't have foreign keys defined?

There is no reason for you to switch to InnoDB for Doctrine.
No.
I find the Doctrine's CLI tools for updating and generating the database schema will often try to generate the SQL with foreign keys. You can manually set up the database, or modify the code it generates, to avoid this.
So, no, it is not a problem.

If you define some Associations mapping for your entities it may convert to foreign keys by doctrine automatically
http://docs.doctrine-project.org/en/latest/reference/association-mapping.html
i you want those foreign keys in your db too you have to use doctrine CLI for creating your schema
for more info about using doctrine cli :
http://docs.doctrine-project.org/en/latest/reference/tools.html

Related

Eloquent Relationship without defining foreign key constraint

I have some query related to Eloquent Relationships in Laravel.
If I didn't specify a foreign key when using any relationship defined by laravel so laravel makes it work by assuming the foreign key based upon the parent model name as written in laravel 8 documentation.
So what I want to ask is that if laravel is only assuming means laravel will make the relationship work but data consistency will not be maintained by laravel as what foreign key does? Am I right?
And if I am right then the solution will be to explicitly define the foreign key in migration file when using relationships?
I guess, you're mixing two things, the database's DDL/schema and the ORM/database abstraction layer:
The database's DDL/schema defines, how the database itself is structured and works. In Laravel, this can be controlled via migrations.
The ORM/database abstraction layer normally doesn't alter the schema, it just does CRUD (which is DML not DDL). In Laravel, this can be done with either the query builder or Eloquent, which is an ORM.
That means, the former creates the foreign key constraints, the latter only assumes a column name to put in the SQL query it creates

MySQL InnoDB foreign key between different port databases [duplicate]

I know, I know, putting two related tables on different databases isn't exactly the best design practice. But for whatever's sake, suppose that I have to do it absolutely. And I have to break up two foreign-key-related tables that were previously located in a database into two databases, that are located on two different servers, but I still want to maintain the database(s) integrity. What is the best way to do this?
Edit: I am using MySQL and Symfony
I can't think of any way to do this with standard MySQL.
You could write a plugin for MySQL Proxy, that manages referential integrity between the parent and child tables on different servers:
Intercept INSERT and UPDATE against child table. Query for matching row in parent table. Fail INSERT/UPDATE if no match found in parent table.
Intercept DELETE against parent table. Query for dependent rows in child table. Fail DELETE if any dependent rows found in child table. If the constraint is intended to support cascading behavior, do that instead of failing.
Intercept UPDATE against parent table. If the primary key value is changing as part of the update, query for dependent rows found in child table. Fail UPDATE if any dependent rows found in child table. If the constraint is intended to support cascading behavior, do that instead of failing.
Note that you'd have to keep information about the referential integrity constraints in your MySQL Proxy plugin (or write a custom config file for your plugin that records the relationships). You can't use conventional FOREIGN KEY syntax to declare such constraints across MySQL instances.
Have you considered Federated tables? These are basically links to tables which are hosted on a different databases on a different/same host.
You can create a federated table locally and use that to enforce referential integrity. However, I cannot overemphasize the fact that this approach is fraught with future gotchas and not at all recommended.

is there a way to disable symfony2/doctrine constraints?

I get that foreign key constraints great for integrity of a database and all, but it's also a huge overhead to use constraints when dealing with tables that are in the millions and growing.
I want to remove foreign key constraints from my application. In past symfony2 projects I've removed constraints manually, but I'm assuming maybe there is a way to simply tell symfony2 to do this?
If anyone is aware of a way to do this within the framework please let me know :)
edit:
Let's say for example in a manyToMany relationship, it auto-creates the relationship table with the proper indexes but it also puts a foreign key constraint on both columns as well, or if i have a oneToMany relationship it puts a foreign key constraint on that relationship.
I don't want these foreign key constraints to be created.
Found in Doctrine's JIRA:
You can disable the exporting of foreign keys for specific models:
User:
attributes:
export: tables
columns:
or with php:
$userTable->setAttribute(Doctrine::ATTR_EXPORT, Doctrine::EXPORT_TABLES);
Now it will only export the table definition and none of the foreign keys. You can use: none, tables, constraints, plugins, or all.

MySQL foreign key relations vs mysql_insert_id to relate tables

This is for a sort of proof of concept draft to get things working, but don't want to have completely crap code. For my database, I tried to get true foreign key relations going using innoDB, but couldn't get it.
Instead of using foreign keys, I decided to just pull mysql_insert_id() after inserts, saving it as a variable, then putting that variable into the related table.
Is this horrible? Everything seems to work well, and I'm able to connect and relate ID's as needed. What benefits would using foreign keys give me over my method (besides updates/deletes cascading)?
To create a relation (master->detail), you have to always supply the keys by yourself, either using mysql_insert_id, natural keys or key generated by your applications. The FOREIGN KEY is not going to make that work for you.
What FOREIGN KEY does is
Helping you enforce the relationship/the integrity of your data (so the "detail" record does not point to an invalid parent)
Handles deletion or key alterations of master records (ON DELETE ..., ON UPDATE ...).
It's also creating an index in your "detail"-table for the "master_id"-row if it doesn't exist yet (okay, you could also do that without FOREIGN KEY)
Has also some kind of documenting purpose for example an ERM-tool could reengineer the relationship model from your schema (okay, this point is a slight long shot)
The cost of adding the FOREIGN KEY constraint statement is small compared to its benefits.

Zend_Db. Am I required to manually set primary and foreign key when building db table?

As I read J.Gilmore Zend Book (Models section):
class Game extends Zend_Db_Table_Abstract
{
protected $_primary='id'; //line 4
}
[..]Line 4 identifies the table's primary key.By default the framework will
presume the primary key is an automatically incrementing integer named id,so
this line is not necessary [..]
I have a question:
Do I need to manually set primary and foreign key while building a table
(Ex. in phpmyadmin with something like "PRIMARY KEY (id),FOREIGN KEY (post) REFERENCES users (id)
ON DELETE CASCADE")?
Or I can handle tables relationships and fields nature just by referring to Zend code using $_primary,$_dependentTable,$_referenceMap and so on?
thanks
Luca
Both. Although you can get away with an ORM dealing with relations, the database ensures at low-level those relations are respected. Always let the database do its job as much as possible, it is built for handling relations and preventing data corruption. What if your ORM has a bug?
As a somewhat related example, say you have a field declared as int in database, you are responsible as a developper for making sure you use int's in your queries, but the database enforces that rule at a lower level, protecting your data in case you don`t.
You should define the primary/unique keys and any other indexes properly when you create the table.
After you've done this in 99% of cases Zend_Db will understand what's going on as it's able to read the table metadata and derive the primary key from that.

Categories