Is there a way to tell doctrine not to touch specific tables? - symfony

Every time I run doctrine:migrations:diff to generate migration for my changes it always includes removal of a few tables that are not handled by doctrine eg.:
$this->addSql('DROP TABLE messenger_messages');
$this->addSql('DROP TABLE monitoring');
Is there a way to tell doctrine that specific tables do not belong to him so doctrine will stop trying to drop them every time?

You can find your answer in the docs : https://symfony.com/doc/master/bundles/DoctrineMigrationsBundle/index.html#manual-tables
Short answer : add prefix to your custom tables, then configure this prefix (for instance if your custom tables start by 't_') :
doctrine:
dbal:
schema_filter: ~^(?!t_)~

You can use a regex to exclude tables from doctrine field of view.
To specify a list of tables that should not be touched by doctrine just add this to config:
doctrine:
dbal:
schema_filter: ~^(?!(messenger_messages|monitoring|foo|bar)$)~
This will prevent doctrine from manipulating those four tables:
messenger_messages
monitoring
foo
bar
Thanks #Diabetic Nephropathy for hinting the way with regex.

Related

Symfony: Doctrine cannnot find table with first letter Uppercase

I have this database in which all tables have this format:
Somedata
and the names of the columns are like this:
UserInformation
Basically, the first letter is capitalized, and Doctrine cannot find the table/column
I tried adding a naming strategy:
naming_strategy: doctrine.orm.naming_strategy.underscore
But that didn't work.
I also tried to set MySQL lower case table names to 1
lower_case_table_names = 1
That didn't work as well. If I change all the names of the tables and columns to lowercase, works as expected. However, I cannot do that in the production database, so how can make Doctrine find the tables with that format?
By the way, the tables are already created, and I cannot modify them
You have 2 choices here:
Implement custom naming strategy where you generate table names as you wish. Check the docs Doctrine Naming Strategy
Set desired table name in every entity: #ORM\Table(name="SomeName")
UPDATE:
Found the solution. I had to write the table name in the Entity like this:
#ORM\Table(name="`Somename`")
Source:
Doctrine - PostgreSQL - Uppercase and Spaces in Table / Field
names

Insert predefined rows with doctrine

I am using Doctrine ORM and I want to insert some rows when I update the current schema.
Which is the best way to achieve that?
You can use DoctrineFixturesBundle for load initial or test data
Fixtures are used to load a controlled set of data into a database.

How correctly insert values in table using Doctrine ORM?

I use Symfony2 and Doctrine ORM. I have table "articleType" where I keep all possible article types. I need insert several values to that table only once, when table is created. My question is how and where I should do that? Because I just can't insert that values in controller with every request to that controller right? Maybe I should write down manually that inserts in Doctrine migration class?
It depends, but most of the time Doctrine Migrations are the way to go. Each migration is supposed to be applied just once and that's exactly what you need.

Extra field in manyToMany relationship

I have two tables: users and estates
A user can be "linked" to multiple estates, and an estate can be "linked" to many users. To handle this manyToMany relationship, I created a "joinTable" estatesUsers in which the primary key is actualy a composite one involving the two foreign keys userId and estateId.
This was fine until I wanted to add extra fields to this table.
I read that when adding extra fields for such a table, the relationship is not a "manyToMany" anymore but two "oneToMany" relationship instead for each original entities.
My problem is that Doctrine still not consider this table (was the join table) and skip it when I run php app/console doctrine:mapping:import AppBundle yml
How can I get the command line tool to handle correctly those relationships and generate this new entity (the yml schema files) ?
Extra question: how do I command the tool (inside the yml schema files) to create a repository class for each entity before running php app/console doctrine:generate:entities MyBundle ?
Thank you !

Symfony2.2 generate entities reversely existing one without primary key

I'm trying to reverse generate some new entities in my application using --filter option to select only the new ones.
php app/console doctrine:mapping:convert yml ./src/MyProject/MyBundle/Resources/config/doctrine/metadata/orm --filter="NewTable" --from-database --force
And I get an error message from a preexisting table, already mapped and working (actually not explicitly mapped as it is a Many to many relation table)
Table your_other_table has no primary key
My questions are:
I know Doctrine has issues generating from tables without primary key but in this case I want to ignore it with the --filter param, I don't even need an entity for this, then why do I get this error?
I usually work with own primary keys for every table, even in relation tables, I think I was "forced" to eliminate primary key in this many to many relation table in order to make relation in entities work, is this correct? Doctrine eliminates primary keys in relation tables? (I mean, they have PK but it is composed by the 2 foreign keys).

Resources