Extra field in manyToMany relationship - symfony

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 !

Related

How to scaffold AspNetUserRoles table?

I have tried to scaffold a AspNetUserRoles table with AspNetUser, AspNetRoles and others tables that I need.
The scaffolding has a problem with many to many relationship of AspNetUserRoles and there is an error when application runs
System.InvalidOperationException: 'Cannot use table 'AspNetRoles' for entity type 'IdentityRole' since it is being used for entity type 'AspNetRole' and potentially other entity types, but there is no linking relationship. Add a foreign key to 'IdentityRole' on the primary key properties and pointing to the primary key on another entity type mapped to 'AspNetRoles'.'
Or other similar errors. I tried to handle AspNetRoles for a day.
I could scaffold a ASPNetUserRoles table only and copy paste it's fragment to my DbContext.
Is it the right way to add ASPNetUserRoles CRUD pages?

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

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.

How do I generate an initial/base migration for existing Symfony+Doctrine application?

If I already use migrations, I can easily generate incremental one using:
app/console doctrine:migrations:diff.
But assume I have an existing application that does not use migrations yet. doctrine:migrations:diff will just generate a diff between the current database schema and doctrine entities. The problem is I need to have an initial/first migration consisting of CREATE TABLE for every entity created up to this point. My current workaround is to create an empty database, switch credentials in parameters.yml, and run doctrine:migrations:diff then.
I don't like this solution - is there a better one?
you could use doctrine:schema:create --dump-sql to generate the sql for creation and put this into the first migration version
http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/tools.html#database-schema-generation
If the table does not exist as a Doctrine Entity, you'll need to manually create a migration class for it, as well as any separate Fixtures.
<?php
namespace DoctrineMigrations;
use DoctrineDBALMigrationsAbstractMigration,
DoctrineDBALSchemaSchema;
class Version00001 extends AbstractMigration
{
public function up(Schema $schema)
{
$this->addSql('CREATE TABLE MY_CUSTOM_PREFIX_example (id INT NOT NULL, name VARCHAR(255) NOT NULL, PRIMARY KEY(id)) ENGINE = InnoDB');
}
public function down(Schema $schema)
{
$this->addSql('DROP TABLE MY_CUSTOM_PREFIX_example');
}
}
Also make sure that you exclude these custom tables using filters within your Doctrine configuration (#see: http://symfony.com/doc/current/bundles/DoctrineMigrationsBundle/index.html#manual-tables)
For this example you'd replace t_ with MY_CUSTOM_PREFIX_
And what about a little :
bin/console doctrine:migrations:diff --from-empty-schema
With the flag --from-empty-schema it will do exactly what you're asking for.
After that you can (must) manually add an entry in the doctrine_migration_versions table if the DB is already set up and you want to (have to) run /bin/console doctrine:migrations:migrate. Like that (screenshot from adminer):

Symfony2: Create table from command line

I am new to symfony2.
Is it possible to create table (fields with relevant data types) from command line without creating entity first?
php app/console doctrine:generate:entity --entity="BundleName:EntityName" --fields="name:string(255)"

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