Error when generating a doctrine 2 entity - symfony

I have already created entities initiall on my projects creationy and now have a table i would like to automap a single entity for.
i ran: php app/console doctrine:mapping:convert xml ./src/MyBundle/Resources/config --from-database --filter="clicks"
and get:
No Metadata Classes to process.
I am looking to just generate the entity of that one table "clicks"
Can anyone explain to me why this might be happening and how I can get this command to work?

If you've successfully use this command to generate entities before, it should normally work.
You can get this error, when the table does not actually exist in the database.
Double check if the table "clicks" is in the database.

Related

How to define entire database schema in Symfony 4?

Is there a way to define the complete database schema in one go in Symfony 4?
I understand that individual entities/objects can be created using the make:entity and make:migration commands but I'm wondering if I could just define the entire schema in one sitting and then use it to build the associated entities and database.
I recall that in earlier versions of Symfony it was possible to define the entire schema in a YAML file and then just issue a build command.
Yes, you can create complete database schema mappings using any of supported mapping formats (e.g. YAML or XML) and declare mappings location in Doctrine configuration. After that you will be able to use any Doctrine console tools to generate and update schema. You can also use tools for reverse engineering mappings from already available database and to convert mappings between formats
Please notice that Doctrine commands names in Symfony application are different from ones that natively provided by Doctrine. You need to use:
doctrine:schema:validate for schema validation
doctrine:schema:create for initial schema generation with subsequent calls of doctrine:schema:update with either --dump-sql or --force depending on your needs
doctrine:mapping:convert to reverse engineer available database (with use of --from-database option) or convert between mapping types in a case if you want to.

How a bundle can provide "default data" i.e. pre-filled tables in Symfony 2?

I think I've a good understanding of Symfony and how bundle works.
However I've never found how to solve a simple problem: make a reusable bundle that provides data like tables/Doctrine entities pre-filled with (i.e.) all country names in the world, all provinces of Italy, tax rates history in England and so on.
Of course the purpose is to provide forms, services and controllers relying on this data source, without the need to copy and paste tables and entities across projects.
How would you do that?
Data fixtures IMHO are not an option because an obvious reason: you are going to purge your database while it's running.
A custom command reading from a static data-source (json, YAML) and performing inserts/updates?
First step is declaring a Doctrine entity in your Bundle. I think you should create DataFixtures to populate your datas into db.
You maybe should consider to use Seeds instead of Fixtures.
Fixtures are fake datas, used to test your application
Seeds are the minimal datas required for your application to work.
Technically, these are exactly the same thing, you declare it under the "DataFixtures/" folder and you import them with the "doctrine:fixtures:load" command.
You can create a folder "Fixtures/", and a folder "Seeds/" under the folder "DataFixtures", then load your seeds with the command
php app/console doctrine:fixtures:load --fixtures=/path/to/seeds/folder --append
It was suggested in the comments that it may be safer, especially in production environment, to create a custom Symfony2 command to force the "--append" mode. Without this mode, your database will be purged, and you could loose your production data.
This answer assumes you're using composer to install your bundles (and you really exclude fixtures as an option).
What you can do, is make an SQL export of the data you want, and make sure it uses INSERT IGNORE INTO, and get the correct unique constraints.
Then you save that file somewhere in your bundle, in a "data" or "fixtures" folder.
so your path to that file will be like:
"vendor/company/epicbundle/data/countries.sql"
What you then can do, is add post-insert and post-update commands in your composer.json, that looks like this:
"post-install-cmd": [
"php app/console doctrine:query:sql \"$(cat vendor/company/epicbundle/data/countries.sql)\""
]
If you only want it to run on install, you only add it there, if you sometimes update the sql file, you also add it to the post-update-cmd.
Please note that this solution only works if people don't temper with the table names, otherwise the queries will fail.
If you want a more save/stable solution, you can write your own post-install script in Symfony that uses the entity manager, and there you can use, for example, a csv file, and insert/update it row by row.
Basically, anything you could implement would surely rely on persistence mechanisms used in your ORM/ODM/whatever. So, you'll end up implementing a typical fixture loading mechanism, at least partially: you'd execute code that saves some provided data; if it's serialized you'd do XML/JSON/YAML parsing (but this is just a technicality) and persist the results into the database.
Thus, it's not bad to stick with Doctrine Fixtures. They are programmable and extensible (you can even fetch your data from the web upon loading).
As stated in #paul-andrieux's answer, if you are worried about data loss (e.g. your bundle's seeds are loaded when the end user's DB is already up), you should use doctrine:fixtures:load --append and let the constraints do their job (like, in a country names table you'd have a unique constraint on country name or even a 'slug') so that inserting duplicate rows silently fails inserting a single entity, in case if your bundle has updated the country list, and the end user had a previous version.
If you really worry about your end users' data you could write a wrapper for the doctrine:fixtures:load command that would have the --append flag always on and register it as a separate command. (You could run needed migrations there, too)
#lxg's hard-coded IDs problem is solvable, too. Try using natural keys where applicable (e.g. the countries table would have a slug primary key that would be great-britain for Grean Britain). This way your searches would be pretty easy: $em->find('\MyBundle\Country', 'great-britain');. If you cannot come up with a natural key, then maybe the entity is not really needed for the end user.
UPD. Here's an article that could be useful: http://www.craftitonline.com/2014/09/doctrine-migrations-with-schema-api-without-symfony-symfony-cmf-seobundle-sylius-example/
Generally speaking, the bundle embedded the entities that will be loaded via the ORM/ODM using their built-in commands (like doctrine:schema:update, doctrine:migration:diff, ...) and provides a custom command that load the required fixtures using the ODM/ORM
This command can read the fixtures in multiple way (parsing yaml, xml, raw sql, dql, ...), it is just a matter of taste. Tones of bundles, parser, ... exist for those tasks.
In your documentation, you just have to state in a clear way that the developer must run this command after your bundle installation and schema update.

Doctrine does not see my custom Entity

I'm new to Symfony/Doctrine. I've create an entity "Merchant" based on a table I had created. It worked perfectly.
Now I've created an entity "Provider" and would like to generate the table for it.
I've used doctrine:mapping:import and it only imports the Merchant one. The Provider is nowhere to be found.
Thanks!
Edit : I've tried using "shema:update" but it returns "Nothing to update - Your database is already in sync".
Make sure your "Provider" class "name" or "namespace" is right and also do't forget to use
use Doctrine\ORM\Mapping as ORM;
if your entity class is in the annotation format ..
Sometimes may be it's file name is "Provide" rather than "Provider.php" and may be class name is not as per its namespace or also you have forgotten to use namespace ..
If you are trying create database schema based on your entities you should use different command.
Following one will drop and create the schema (pay attention that this will remove all your database data):
app/console doctrine:schema:drop --force
app/console doctrine:schema:create
Or you can just update existing schema:
app/console doctrine:schema:update --force
In addition to above command you can just look at sql changes that doctrine is going to apply to your db, without applying them:
app/console doctrine:schema:update --dump-sql
Make sure your database config file has your database name that matches your Entity's schema name.
If they are mismatched, no differences are notec, no SQL gets created
I had same problem. Console restart worked for me.

Symfony Doctrine Mapping:Import & Table Association

I am working with Symfony 2.3 on a new project using an existing database with numerous associations - many-to-many, one-to-many etc. During my initial import last week, I found somewhere in the docs stipulated that a doctrine:mapping:import would generate orm.yml files of my database which it did without a hitch. However, I also see that only ManytoOne relationships are generated in the yml files ... not any other kind of associations.
My statement was:
$ php app/console doctrine:mapping:import –em=buv DBImportTestBundle yml
Also, I did a generate entities to create classes and basic CRUD for each table using:
$ php app/console doctrine:generate:entities DBImportTestBundle
This also worked EXCEPT that I do not see any annotated associations generated in the doc blocks for any of the entity properties.
I'm looking through the docs but do not see any specific information on the exact requirements for associations on imported dbs. It could be I'm not looking in the right place.
I'm trying to determine the most efficient way to maintain my db schema within symfony/doctrine ... My understanding was that I would need to explicitly define certain associations manually but I'm not sure what the exact requirements would be OR if perhaps I'm simply not passing in the correct arguments to create my annotated associations via generate:entities.
Can someone point me to any docs that refer to what I'm talking about or explain the proper approach to defining complex associations within doctrine? Thank you.
$ php app/console doctrine:mapping:convert annotation ./src to generate entity classes with annotation mappings, before running :
$ php app/console doctrine:generate:entities DBImportTestBundle
there is a cookbook for that

Doctrine2 does not generate getter/setter

I'm trying to reverse engineer a db schema using Doctrine.
If i do
php app/console doctrine:generate:entities NS/MyBundle/Entity/MyClassName
it successfully creates a class with private properties taken from DB, but there are not getter/setter method, so that i cannot use it. What am i doing wrong?
Please follow How to generate entities from existing database? As I don't think what you are doing is really going to work.

Resources