I have a database table which has a lot of fields. I want to create an Entity which maps only to few of those fields.
Is there a practical way to achieve that?
When I try to make an entity and mapping it via annotations, the doctrine:schema:validate command says that the entity is not in sync, and is right.
When I try to make a doctrine:schema:update it automatically drops all the fields that the entity doesn't have. I want that the schema update command updates only the fields written in my entity class.
With Doctrine ORM you map your database table to a PHP class and a row from that table is mapped to an instance of the entity class.
So i am pretty sure that you have to map all your fields. Otherwise if you dont want to - user ActiveRecord, it is possible there.
Related
I have created my database in Symfony with
php bin/console doctrine:mapping:import "App\Entity" annotation --path=src/Entity
I have created my repository too.
But when I look at the entity directory, one important table is missing.
In this table there is a lot of important information for my website, but I can't access it, because the entity does not exist.
I have no error with the import and not error in the database (I think).
Table on HeidiSQL:
The entity on the Symfony web site:
I have not demande_etape.
Why and how i can make this table in entity ?
I don't no why but if symfony not detect ID key, he can't import the table and make joins in table on both side.
So I created my entity myself and i put id_Demande field and id_Etape field in ID.
It's wotking !
There does not seem to be proper documentation available on how to configure and use database views with the doctrine migrations bundle.
One probably is not able to map SQL statements which will end up creating/updating a database view (from the sql given somewhere) when migrations:diff and migrations:migrate are run.
If an entity is mapped to a database view with the #table(name="view_name") markup, it ends up causing an error / new table being attempted, instead of understanding that its a database view being used.
Is there a solution? Am I missing something?
I'm not sure that doctrine can get out of the box views. For all I know, you'll have to cheat.
Or:
I think you have to write the migration script yourself.You can generate an empty one and then write the create-statements into it.
In the repository you integrate native sql. The result you map to your entity or DTO.
https://www.doctrine-project.org/projects/doctrine-orm/en/2.7/reference/native-sql.html
If I run
php app/console doctrine:schema:update --force
It will update my database from all entities.
I need to update database only for the User entity, what is the solution?
One solution is to define a custom entity manager and then pass that entity manager to
php app/console doctrine:schema:update --force --em="custom"
But maybe it exists something faster without defining a custom entity manager?
According to the command documentation, there is no such option. If this is something you have to do only once, I'd suggest to use the --dump-sql option to get the required SQL instructions, and manually run the ones you need.
P.S. I fail to understand what's the reason to only update the schema for an entity, and leave all the rest of entities not sync'd with the database. That sounds like a recipe for getting db errors.
You can manually use the Doctrine SchemaTool class to generate an SQL diff based only on a single entity. You’ll need access to Doctrine’s EntityManager – the example below assumes access to Symfony’s container.
use Doctrine\ORM\Tools\SchemaTool;
$em = $this->container->get('doctrine')->getManager();
$schemaTool = new SchemaTool($em);
$metadata = $em->getClassMetadata(YourEntity::class);
$sqlDiff = $schemaTool->getUpdateSchemaSql([$metadata], true);
The variable $sqlDiff will now contain an array of SQL statements that are needed to bring the database schema up to date with your entity mapping.
The second argument passed to SchemaTool::getUpdateSchemaSql() is important – without it, the default behaviour would be to generate DROP TABLE statements for every other table that appears in your database.
I've now read kinda half of the Doctrine 2 documentation but I can't find a solution: how do I create a table for a class automatically using Doctrine?
Do I really need to work with XML/YAML or some other stuff than PHP itself? Do I really need DQL for that? Doesn't Doctrine find the names and all this stuff for me?
First of all, you have to understand that in Doctrine 2 there are three elements that play together:
entities (just plain PHP classes)
mappings (additional markup that you place on entities or in related classes)
database
Doctrine reads your entities and your mappings and connects every entity and its fields to the related database fields.
The generation of the database is done by the Doctrine\ORM\Tools\SchemaTool (SchemaTool) class, which can read metadata and define how your schema should like.
Doctrine's CLI, as said by #Marcin, provides the orm:schema-tool:create and orm:schema-tool:update commands, which are just wrappers for the SchemaTool. They help you getting started fast and keep your schema in sync with your entity definitions.
I'm not sure if I understood you correctly.
If you want to create a structure in a database, use the console function orm:schema-tool:create
What is the best way to rebuild a model without loss data in MySQL for Symfony?
What if I have a big site, and a lot of data in the database and I would like after six months to add few new fields to database?
You can use migration.
Doctine manual
Symfony task for migrations
Slideshare presentation
Slideshare presentation
So you need write migrations, migrate, and build your models, forms, etc.
I suggest you use #denys281 for Symfony1.4 ....in Symfony2 however its VERY simple ... just use the command :
php app/console doctrine:schema:update --force
It compares what your database should look like (based on the mapping information of your entities) with how it actually looks, and generates the SQL statements needed to update the database to where it should be. In other words, if you add a new property with mapping metadata to Product and run this task again, it will generate the "alter table" statement needed to add that new column to the existing product table. So it doesnt remove any data
There is also a DoctrineMigrations bundle for Symfony2 if you fancy that route -> http://symfony.com/doc/current/bundles/DoctrineMigrationsBundle/index.html