I created a custom Symfony command to manage entities in my DB but I don't know how to use doctrine in it (e.g. import it like in a controller).
Thanks!
Using a ContainerAwareCommand is fine.
$this->getContainer()->get('doctrine');
Using the container directly is not a good practice. Sometimes you will need to modify class code and when that time comes you will need to review the whole code to find any dependencies.
Register the command as a serivce and pass serivces that are actually will be used in the logic.
Check How to Define Commands as Services
Related
Our system is written in ZF2. I'm trying to call a ZFTool console command programmatically, if there's a way to do that. All documentation I have found so far points to call a Controller action from CLI, instead of CLI programmatically from a controller.
If there's no default way to do this, a workaround would be fine as long as it's testable. Thanks in advance.
PS: I'm new to ZF2, I come from Laravel where you have a facade class to execute commands from a controller class Artisan::call('my:command').
I am working on building an application which would be used to create new child applications which have their own databases.
I know we can use doctrine bundle to create tables from entity classes in symfony. I would like to know if there is a way to create a new database and some tables within the database programmatically or dynamically.
I know we can use php app/console doctrine:generate:database from the composer prompt, but i would like to do this from a class or a controller action.
Do let me know if there is a way possible.
You can either call exec() and drop the command in there or the better way, would be to follow the docs in symfony for calling console commands in a controller.
http://symfony.com/doc/current/cookbook/console/command_in_controller.html
Recently I started learning some Symfony as a side "project". I know with Doctrine you can use MySQL. However, I got my own database class using PDO. How can I implement and use my own database class on Symfony? And in which folder should I place it?
I used the download from here: http://symfony.com/download and the tutorial from http://symfony.com/doc/current/book/installation.html. I couldn't find anything about using an own database class.
You can use it like any other class in php, or you can create symfony service for that class (you have everything about services in symfony2 documentation)
According symfony2 best practices you can put your class in any folder in AppBundle. But if you want to reuse your class on multiple projects you should put it on packagist, and instal it with composer.
But if just you want to learn symfony you should consider using doctrine, because most of the symfony projects are using it.
I am a newbie in Symfony2 and I can't understand where I should make includes with my custom cross-projects functions (e.g. array_merge_overwrite, array_last, etc.)? I use both types of apps: web (MVC) and console (extends ContainerAwareCommand).
Or there is another "right way" for this?
Create a service and put your common functionality in it. For example, you can name it ArrayService and register it in the container as array.service. You can then access this service from controllers via
$this->get('array.service');
and from commands via
$this->getContainer()->get('array.service');
So, your code will look something like this:
$element = $this->get('array.service')->last($array); // or ->arrayLast($array)
If you need the same functionality across several projects, make a bundle with that service and add it to the deps file of each project. Then it will be installed when you run the bin/vendors install script.
You can convert your functions to static methods of some class to make them autoloadable. Or... well... Place them where you want and require() from where you need them every time.
When I use the zend CLI to create a form, like this, a form gets generated and gets added to the forms folder.
zf create form MyForm
I have a custom class that I'd like to create a custom command for it in the same way and would like to look at the zf create form command for guidance. I suspected it to be in Zend_Framework/bin where there's a shell, php, bat files, but I couldn't find any code there related to the form class creation, maybe i missed?
Anyway, is there a better to accomplish the same thing? like an Zend API that lets me create some of these commands myself? If not my only option would be to see how something like zf create form was created.
When you run zf, it invokes zf.php in the framework's bin folder. That in turn creates an instance of Zend_Tool_Framework_Client_Console, which jumps through some non-obvious hoops to load up all of the available actions and parse your command. Part of this process involves looking in the /Zend/Tool/Project/Provider/ directory, which houses the classes responsible for providing many of the project-oriented actions (like creating forms).
I haven't ever tried this myself, but there is (apparently somewhat outdated) documentation on creating your own providers, so hopefully following that will get you pointed in the right direction.
The ZendCast Integrating Zend_Tool Into Your Application is an excellent introduction to the whole business of creating Zend_Tool providers.
You need to create a provider. Look at this nice tutorial : http://weierophinney.net/matthew/archives/242-Creating-Zend_Tool-Providers.html