I'm having a problem with Symfony2 / Doctrine.
I'm starting to think that I'm trying to do the impossible, joining 2 seperate databases together via a relationship.
I've got a mysql database, and an sqlite database.
I've got 2 bundles (each talking to one of the databases each)
I've got 2x Entity Managers, with mappings working correctly.
I can access each database, via it's own Entity Manager just fine, Each bundle can access the other bundles Entity Manager, and it's all working well on that front.
The Sqlite database is locked-down to another application, and I have to use it as-is. The mysql-database is mine to do whatever I need with.
In the sqlite database I've got a "Projects" table
In the mysql database I've got a "Tasks" table.
One project can have many tasks.
My tasks table has a project_id field, which is populated with the ID from the projects table. What I'm trying to do is to make this relationship work properly, so that I can use twig to do what you normally can do with twig in more normal situations. I.e. call something like {{ project.tasks }} or {{ tasks.projects }}.
At the moment I've got some code in the Project controller, passing the Tasks to the views and vice versa. This does work, but it's quite cumbersome. What I'd really like to do is to have the ORM mapping working correctly between each entity.
Can Doctrine/Symfony2 do this, or am I trying to do the impossible?
Any assistance would be greatly appreciated.
Here's an extract from my config.yml file.
orm:
auto_generate_proxy_classes: %kernel.debug%
default_entity_manager: agile
entity_managers:
glue:
connection: glue
mappings:
WebplaceGlueBundle: ~
agile:
connection: agile
mappings:
WebplaceAgileBundle: ~
We have a similar problem at OpenSky: we have some data stored in MongoDB, and some stored in MySQL. We use an extension to stof's DoctrineExtensions bundle:
https://github.com/opensky/DoctrineExtensionsBundle/tree/orm2odm_references_current
This lets us add #Gedmo\ReferenceOne annotations between ODM and ORM. You might be able to use this directly between two ORM connections, but if not it'll give you a starting point for handling relationships between different persistence layers...
/**
* #Gedmo\ReferenceOne(
* type="document",
* class="MyBundle\Document\User",
* identifier="userId"
* )
*/
Related
Is it possible to check if the relations between models are correct?
Im looking for cli command or something like Symfony2 profiler which shows wrong relations.
There is a build-in command that Validate that the mapping files are correct and in sync with the database:
./bin/doctrine help orm:validate-schema
'Validate that the mapping files are correct and in sync with the
database.'
In the symfony2 doctrine bundle exists two command instead:
doctrine:schema:validate
The doctrine:schema:validate checks the current mappings for valid
forward and reverse mappings.
and
doctrine:mapping:info
The doctrine:mapping:info shows basic information about which
entities exist and possibly if their mapping information contains
errors or not.
"Using custom column definition trigger schema update every time":
"This is a known limitation that we cannot fix."
https://github.com/doctrine/dbal/issues/2666#issuecomment-283772609
When openerp modules are uninstalled the associated tables in the postgres database still exist. Is there anyway to synchronize the OpenERP modules with the ORM model to remove those tables and assigned user to it only. B/c it gives no sense to keep the tables without the application which access it.
You could write a routine that starts with the ir_model table and look for tables in the schema that don't have a matching entry in ir_model. You will have to be careful of transient (osv_memory in version 6 parlance) tables and also models with _auto = False which is usually done to create database views.
I have a SQL Server view CyclesList on the table Cycle. Cycle table contains a few columns, and CyclesList view add some more data that can be computed on database level.
And now, I have a NHibernate mapping that points to CyclesList:
<class name="Cycle" table="CyclesList">
However, I would still like to work with Cycle class, and perform Create/Update operations , but I have to use stored procedure that will access Cycle table directly. Is there a way to achieve it in NHibernate? I would appriciate a sample mapping/links to resources with samples. Thanks
You find some information in the docs under "Native-Sql -> Custom SQL for create, update and delete". Basically, you need the "sql-insert", "sql-delete" and "sql-update" elements in the mapping file.
There is also an example on Ayendes blog.
I'm using Fluent NHibernate (and I'm a newbie). I have mapped a read-only table that already exists in the database (it's actually a view in the db). In addition, I have mapped new classes for which I want to create tables using SchemaExport.Create().
In my fluent mapping, I have specified "ReadOnly()" to mark the view as immutable. However, when I execute SchemaExport.Create(), it still tries to create the table so I get the error "There is already an object named 'vw_Existing'".
Is there a way to prevent NHibernate from trying to create that specific table?
I supposed I could export and modify the sql (SetOutputFile), but it would be nice to use SchemaExport.Create().
Thanks.
You're looking for
SchemaAction.None();
We're developing a Doctrine backed website using YAML to define our schema. Our schema changes regularly (including fk relations) so we need to do a lot of:
Doctrine::generateModelsFromYaml(APPPATH . 'models/yaml', APPPATH . 'models', array('generateTableClasses' => true));
Doctrine::dropDatabases();
Doctrine::createDatabases();
Doctrine::createTablesFromModels();
We would like to keep existing data and store it back in the re-created database. So I copy the data into a temporary database before the main db is dropped.
How do I get the data from the "old-scheme DB copy" to the "new-scheme DB"? (the new scheme only contains NEW columns, NO COLUMNS ARE REMOVED)
NOTE:
This obviously doesn't work because the column count doesn't match.
SELECT * FROM copy.Table INTO newscheme.Table
This obviously does work, however this is consuming too much time to write for every table:
SELECT old.col, old.col2, old.col3,'somenewdefaultvalue' FROM copy.Table as old INTO newscheme.Table
Have you looked into Migrations? They allow you to alter your database schema in programmatical way. WIthout losing data (unless you remove colums, of course)
How about writing a script (using the Doctrine classes for example) which parses the yaml schema files (both the previous version and the "next" version) and generates the sql scripts to run? It would be a one-time job and not require that much work. The benefit of generating manual migration scripts is that you can easily store them in the version control system and replay version steps later on. If that's not something you need, you can just gather up changes in the code and do it directly through the database driver.
Of course, the more fancy your schema changes becomes, the harder the maintenance will get i.e. column name changes, null to not null etc.