My question is simple, but I can't find a fine answer to it, I had an entity created by a command line :
php bin/console make:entity
this entity is User that has few attribute ( name - email - password )
After inserting the fields, I migrated, so my table has been created in the database using those commandlines :
php bin/console make:migration
php bin/console doctrine:migration:migrate
But now I want to just change the name to a username but I don't know how to do it.
I did not find anything in the documentation, so any help would be much appreciated.
Be careful: After any changes in your entities you must generate new migration file. In fact this file contains all of the changes which must be done on your database to be update.
To generate this file (new migration version) you can follow these commands:
$ bin/console doctrine:cache:clear-metadata
$ bin/console doctrine:migrations:diff
After above commands you generated your new version file successfully, now if you run following command, you can see you have a new and not executed version file:
$ bin/console doctrine:migrations:status
Finally, to execute the new version file and update your database, you must run the following command:
$ bin/console doctrine:migrations:migrate --all-or-nothing
now your database is update and in the table migration_versions you can see that new version has been added.
Be Successful.
You can follow a similar pattern:
Change the field (annotation) in your User-entity from name to username
Run bin/console doctrine:migrations:diff
Run bin/console doctrine:migrations:migrate
The doctrine:migrations:diff command should detect that the field changed and create a corresponding SQL query for you, which is then stored in a DoctrineMigrations-file right next to the original one.
The doctrine:migrations:migrate detects this new migration and that it was not previously executed, by checking against the doctrine_versions table, where all migration versions that were executed are stored.
See also: https://www.doctrine-project.org/projects/doctrine-migrations/en/2.1/reference/generating-migrations.html#diffing-using-the-orm
Related
I've just installed symfony 4 and after noticing the structure being slightly different, searching for some config files and editing around a bit, I was planning to go and generate my first entities and cruds.
However, I found that symfony 4 does no longer support the doctrine:generate:entity command.
Instead I found that symfony now offers the MakerBundle that comes with a range of simple commands to generate the most basic code snippets.
What I am wondering is if there is still a way to interactively generate an entity and/or crud.
I tried installing the SensioGeneratorBundle but that doesn't yet seem compatible with symfony 4.
Use MakerBundle:
composer req doctrine maker
For example, create your entity:
php bin/console make:entity Product
If you want use annotations, run:
composer req annotations
...then you need this informations:
Examples the commands for to works with entities (database)
Symfony use Doctrine.
If your don't have database, run this command:
php bin/console doctrine:database:create
If you want create entities in your database:
php bin/console doctrine:schema:create
If your need update your entities, run this command:
php bin/console doctrine:schema:update --force
For help, run command:
php bin/console list doctrine
So, your can generate entities if you already have database, see list doctrine.
Create a plain old PHP object, add some doctrine annotations to it for the properties which are columns, then do a doctrine:migrations:diff. A migration file will be created with the SQL required. Then you run doctrine:migrations:migrate and the SQL will be executed.
What I am wondering is if there is still a way to interactively generate an entity and/or crud.
Why not use maker, which you mentioned?
composer require maker --dev
Then, run:
bin/console make:entity
I just cloned a Symfony PHP project from Github but whenever I open a page containing the following code:
public function indexAction()
{
$em = $this->getDoctrine()->getManager();
$modules = $em->getRepository('AppBundle:Module')->findAll();
return $this->render('module/index.html.twig', array(
'modules' => $modules,
));
}
The page will return the following error:
ConnectionException: An exception occured in driver: SQLSTATE[HY000] [1049] Unknown database ''.
I am not really experienced with PHP Symfony projects yet and someone asked me to clone this and try to solve the error I will be returned with. Where do I need to look in order to fix the error? I suppose some sort of configuration file which points to database configuration?
This error mean you didn't got access to the database.
Run php app/console doctrine:database:create and after, php app/console doctrine:schema:update --force.
It will create the database on your own local server. (make sure to provide correct connection details on your app/parameter.yml file)
EDIT: Also, don't forget to update your project using composer install and composer update.
Since this is a github project, all dependencies are not pulled
You need to create database:
For Symfony2:
php app/console doctrine:database:create
For Symfony3:
php bin/console doctrine:database:create
You can see sql dump:
For Symfony2:
php app/console doctrine:schema:update --dump-sql
For Symfony3:
php bin/console doctrine:schema:update --dump-sql
Than create tables:
For Symfony2:
php app/console doctrine:schema:create
For Symfony3:
php bin/console doctrine:schema:create
Is there a way to display all commands of only one namespace? all commands of only doctrine for instance?
It'd be really nice since I have a lot of commands and a lot of command namespaces .
I do know | grep <namespace> but looking for a built-in symfony option if it exists
Given that I'm using symfony2.0
Enter only the beginning of the namespace to list the available commands:
$ php app/console doctrine
[InvalidArgumentException]
Command "doctrine" is not defined.
Did you mean one of these?
doctrine:query:sql
doctrine:query:dql
doctrine:schema:drop
doctrine:mapping:info
[…]
But it won't display all the commands if the number is low:
$ php app/console doctrine:cache
[InvalidArgumentException]
Command "doctrine:cache" is ambiguous (doctrine:cache:clear-result, doctrine:cache:clear-query and 1 more).
The list command can also be used, it will sort the commands and add a description:
$ php app/console list doctrine
Symfony version 2.3.28 - app/dev/debug
Usage:
command [options] [arguments]
Options:
--help -h Display this help message
[…]
Available commands for the "doctrine" namespace:
doctrine:cache:clear-metadata Clears all metadata cache for an entity manager
doctrine:cache:clear-query Clears all query cache for an entity manager
doctrine:cache:clear-result Clears result cache for an entity manager
doctrine:database:create Creates the configured databases
doctrine:database:drop Drops the configured databases
doctrine:ensure-production-settings Verify that Doctrine is properly configured for a production environment.
[…]
doctrine:schema:create Executes (or dumps) the SQL needed to generate the database schema
doctrine:schema:drop Executes (or dumps) the SQL needed to drop the current database schema
doctrine:schema:update Executes (or dumps) the SQL needed to update the database schema to match the current mapping metadata
doctrine:schema:validate Validates the doctrine mapping files
$ php app/console list doctrine:cache
Symfony version 2.3.28 - app/dev/debug
[…]
Available commands for the "doctrine:cache" namespace:
doctrine:cache:clear-metadata Clears all metadata cache for an entity manager
doctrine:cache:clear-query Clears all query cache for an entity manager
doctrine:cache:clear-result Clears result cache for an entity manager
Starting my first Symfony/doctrine project I created 2 entities (user & property) using:
$php app/console doctrine:generate:entity
This works fine and gives me the two needed php files with the classes and annotions.
After this I want to create the corresponding tables in my database using:
$php app/console doctrine:schema:update --force
Only the first table (user) is created. What's going wrong? What do i have to do to have both tables created. BTW: no error message received...
SOLVED! There was a syntax error in the property class. But doctrine:schema:update did not give any error... :-(
When trying doctrine:schema:create I got the error message. Corrected it and afterwards doctrine:schema:update worked well!
always backing up your entities before you update schema ..
for backing up entities write
>php app/console doctrine:generate:entities projectname
If there is Error In entity It will show you.
Try to do
php app/console doctrine:generate:entities
before
php app/console doctrine:schema:update --force
I have table "my_table" with some fields.
I want generate Entity in MyBundle used "my_table". But I don't want recreate all entities in MyBundle.
How can I do this?
Here is the way you can do it,
First step, ask Doctrine to introspect the database and generate the corresponding xml or yml metadata files.
php app/console doctrine:mapping:convert [xml|yml] Path/To/MyBundle/Resources/config/doctrine/metadata/orm --from-database --force --filter=MyTable
Second step, ask Doctrine to import the schema and build related entity classes by executing the following two commands.
php app/console doctrine:mapping:import MyBundle [xml|yml|annotation] --filter=MyTable
php app/console doctrine:generate:entities Path\To\MyBundle\EntityFolder\\MyTable
Take a look at the How to generate Entities from an Existing Database section of the documentation
Simple Working Solution for Symfony 2.7 option annotation and for [/xml/yml] see http://symfony.com/doc/current/cookbook/doctrine/reverse_engineering.html
do 3 commands in 3 steps:
Command #1:
$ php app/console doctrine:mapping:import --force AppBundle xml --filter="Meeting"
Output:
writing C:\xampp\htdocs\localxyz\src\AppBundle/Resources/config/doctrine/Meeting.orm.xml
Command #2:
$ php app/console doctrine:mapping:convert annotation ./src/AppBundle/Entity --from-database --filter="Meeting"
Output:
Processing entity "Meeting"
Exporting "annotation" mapping information to "C:\xampp\htdocs\localxyz\src\Entity"
Command #3:
$ php app/console doctrine:generate:entities AppBundle:Meeting --no-backup
Output:
Generating entity "AppBundle\Entity\Meeting"
generating AppBundle\Entity\Meeting
where:
AppBundle is exactly your "AppBundle" in 2.7 symfony
Meeting is the target table (case sensitive)
TO BE SURE, check this directory:
C:\xampp\htdocs\myproj\src\AppBundle/Resources/config/doctrine/Meeting.orm.xml
C:\xampp\htdocs\myproj\src\AppBundle/Resources/config/doctrine/MeetingOriginal.orm.xml
AND MAKING SURE you only have .xml files for the table you want to create entity class files and no others.
It works very well for me.
For explanation please read: http://symfony.com/doc/current/cookbook/doctrine/reverse_engineering.html
php app/console doctrine:mapping:import "MyCustomBundle" xml --filter=MyMatchedEntity
Although this is an old post but if someone gets following error,
Database does not have any mapping information.
Check
If your table name is blog_post then in filter option use BlogPost and not blog_post
Reference: https://stackoverflow.com/a/27019561/6504104
though this is covered by answers above but I missed it and was getting this error
So I wanted to make this explicit
Also in symfony >= 3.4 its' php bin/console e.g.
php bin/console doctrine:mapping:import --force AppBundle xml --filter="BlogPost"
and then
php bin/console doctrine:mapping:convert annotation ./src/AppBundle/Entity --from-database --filter="BlogPost"
Thanks...