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)"
Related
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.
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):
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 !
I defined an Symfony2 entity class and I want to create the corresponding table in my database. This works fine thanks to the command 'php app/console doctrine:schema:update --force'.
But the order of the columns isn't equal to the order in my class. Any way to get them in the same order?
Can we create multiple schemas for a particular user? I am currently logged in as X/Y user and when I tried creating a schema using create schema authorization sample_schema, I got the error the schema name is missing or is incorrect in an authorization clause of a create schema statement. I do know that a default schema X would have been created.
CREATE SCHEMA in Oracle does - contrary to its name - not create a new schema.
It is merely a shorthand to create several tables in a single statement.
Quote from the manual:
Use the CREATE SCHEMA statement to create multiple tables and views and perform multiple grants in your own schema in a single transaction
and further down the explanation on what the "schema" name parameter is:
The schema name must be the same as your Oracle Database username.
Well you could create a user named sample_schema (From the above example) and give user X/Y permission to use sample_schema tablespace.