Map an entity in symfony 4 - symfony

How about, I have a problem and it is before in symfony3 was run in the console:
php bin/console doctrine:mapping:import MiBundle yml
and generated and map an entity of the database but in Symfony 4 the command in the console is always the same, but the bundles are no longer occupied in the latest version so the previous command as it is does not work anymore, Someone could help me...
likewise generate the get and set

When using the new Symfony 4 directory structure without bundles the commands for importing the mapping and creating entities from an existing schema in the DoctrineBundle will no longer work properly. There is currently an ongoing discussion whether to update them, but the Doctrine team considers those tools counterproductive. You are not meant to blindly map the schema 1:1 to your domain model.
The best advice I can give for now is to temporarily create a bundle and then move the resulting files. This is also the workaround suggested in the github-issue regarding this: https://github.com/doctrine/DoctrineBundle/issues/729
The Symfony team is moving some of those commands into their own MakeBundle, but I don't think this command is already in there. Maybe you want to follow their progress.

Related

Symfony 4, a way for generate Entities from an Existing Database?

With Symfony 3 and its console, we can generate entities from an already existing database via the command "php bin/console doctrine:mapping:import" (very usefull !).
From symfony 4, the command "./bin/console doctrine:mapping:import" needs a bundle name but symfony 4 doesn't work with bundle now.
With the new version of symfony, is there a way I didn't see for generate entities from an existing Database (mysql by example) ? Or must I wait a new version of doctrine for have a "doctrine:mapping:import" compatible with Symfony 4 ?
I found a(n) (ugly) solution yet. I deploy a disposable symfony 3, I link the symfony 3 to my database and I generate entities in a bundle. Then I copy generates files to symfony 4.
It's ugly but it works haha
You can use
php bin/console doctrine:mapping:convert --from-database annotation ./src/Entity
which should create the entities based on the database setting. Don’t forget to add the namespaces, and you will still need to add the getters and setters, but the bulk of the properties, including annotations and some of the relationships are already included. (Source)
Please also note, that Doctrine will not support this anymore in the next Doctrine version. As written in the Symfony docs
Moreover, this feature to generate entities from existing databases will be completely removed in the next Doctrine version.

Equivalent of Laravel Seeders on Symfony?

I'm developing a web site with Symfony. I'm new on this framework. Before i used Laravel 5.0 and I need to have a database with rows.
I create my db with command prompt but now I don't find how to seed it.
There is a equivalent of Laravel seeders on Symfony?
No. Seeding was a feature added by Laravel. You’ll need to use a third-party package to load seeds/fixtures into your application: http://www.sitepoint.com/data-fixtures-symfony2/
All the answers here are a bit outdated and this question is the first result on google so for future readers:
Since Symfony 3 there is an official bundle for this very purpose
Installation: composer require --dev doctrine/doctrine-fixtures-bundle
Then write your fixtures in src/DataFixtures and run php bin/console doctrine:fixtures:load
Try this package https://packagist.org/packages/evotodi/seed-bundle. Looks like it's what you need.
Their readme
Symfony/Doctrine Seed Bundle
Used to load/unload seed data from the database. Example would be to load a table with a list of states and abbreviations, or populate the users table with initial admin user(s). Unlike the DoctrineFixturesBundle which is mainly for development this bundle is for seeding the database before the initial push to production.

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.

Installing sfGuard without changing db

I am to integrate sfGuard plugin with existing application. What I want to do is to keep as much code as possible untouched. Any guides? It'd be perfect if I can use actual database schema, or bind it somehow to be used by sfGuard. I know about setting userProfile class but I'm not sure how should I get to it, not to destroy my app.
Greetings
Just install plugin. And try make migrations. doctrine::generate-migrations-diff
php symfony doctrine:generate-migrations-diff
And migrate php symfony doctrine:migrate :
php symfony doctrine:migrate
Check out question: Rebuild model without loss data in MySQL for Symfony

Doctrine uploadable extension in Symfony

I read this doc in order to understand how the doctrine uploadable extension works so I can use it in my Symfony projects.
The problem is at the usage example, where I see an object called $listener and I really can not figure out where does it come from.
I intend to use a similar piece of code in one of my controllers, but I don't know to instantiate that listener, or where to grab it from.
If you look into the github project in question, you can see that they have a documentation in how to install and use them with symfony 2:
Install Gedmo Doctrine2 extensions in Symfony2
And if you don't want to do the hard work, there is also a pre-made bundle:
Integration bundle for DoctrineExtensions by l3pp4rd in Symfony2 (documentation)
Please note that while the bundle should be easier to install, it is made by a third party, not by the extensions developer, and it might not be as up to date.

Resources