Using own database wrapper in Symfony - php

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.

Related

Use Symfony entities and doctrine mapping without database

We are migrating our project from custom CMS to Symfony 5.1. Instead of Database, we are using REST API. Currently we are using Entities without connecting them to any database (with no #ORM annotation or configuration file), and we put all REST logic inside a Repository folder
Currently, if we save our entity in SESSION
$example = new Example();
$request->getSession()->set('example', $example);
On every next request, we will get error:
Class "App\Entity\Example" is not a valid entity or mapped super class.
Of course, this could be fixed if we put correct #ORM annotations, but then it requires a database.
Can this be fixed, and if it's not possible, what would be proper architecture for this scenario?
Let doctrine be the tool to manage the data. In fact that there is no database, you will need something similar to let doctrine manage the data of the REST API.
Take a look at this bundle:
https://github.com/CircleOfNice/DoctrineRestDriver
Edit:
In addition to one of the comments, the DoctrineRestDriver is not maintained anymore. In fact, the bundle does not support Symfony 5.x.
To use the functionality of DoctrineRestDriver you could downgrade your symfony to the supported 4.x.
Your architectur to use doctrine to manage your rest-api can be solved by using the DoctrineRestDriver package.
If you don’t want to downgrade your symfony, you will have to implement your own „data-manager-solution“, to use the api as your database.
But I think the architectural problem with the rest api doesn’t relate directly to your current error. We need more code to understand, where your specific mapped super class error is from (repository, entity).

Using Doctrine in a custom Symfony command?

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

Where should I add my internal classes file in symfony2?

I'm porting an old project to symfony2 in order to start learning the framework and I cannot realize how to properly add some helper functions I have in a PHP file.
Services. Wrap those function in a class (like Helper), define the class as a service and then inject it where needed (controller or another service, or even into a cli command).
The namespace (thus the path, as you tagged the question with psr-0) is up to you.

Codeigniter with Doctrine for large application

I am about to start a project on PHP and selected Codeigniter as a framework to be used after receiving lot of plus comments from Codeigniter-users ;-)
I am not much clear about which ORM to be used with codeigniter. I was advised to use Doctrine. Is there any tool available for codeigniter to create models and db mapping with Doctrine on command line?
You can use Doctrine itself for creating models. Follow the cookbook
http://www.doctrine-project.org/projects/orm/1.2/docs/cookbook/code-igniter-and-doctrine/en
to setup, so then in /application/ you can use
$ ./doctrine [command]
to create models and mappings.
Note: in this moment I'm failing to access any page at the doctrine-project.org site. May be later...

How do I use Symfony's Doctrine model classes outside of Symfony?

Is there a way to use Doctrine using the model classes I've already setup for my Symfony applications without having to call Symfony with all that overhead?
This is more to satisfy a curiosity than anything else. For all the scripts I've used, I've just been able to instantiate Symfony (which typically turns out nice since I have all of the features that I'm used to working with on this particular project. But there has to be a way to load Doctrine and use the Symfony model classes without Symfony... Right?
Doctrine isn't dependet on symfony. Doctrine is a "framework" on its own. It has it's own autoloading and can therefore work with it's classes like a regular PHP app. You can integrate Doctrine with other frameworks if you want (like CodeIgniter or Zend). So you have every freedom you need without the need to do some tedious migration of your models/data/... from one system to another.
I've come to the conclusion there really isn't a way to use the model classes from Symfony elsewhere. With a little work, you can port over the classes to a new Doctrine model (even if you use the generator, since the main model class just extends the base which extends sfDoctrineRecord (from the API docs, you can see which functions will need to be removed).
Otherwise, there isn't a practical way of doing that.
Anytime I need to access the Symfony model, I'm making a task or plugin since I do typically need part of Symfony's functionality.
As far as Symfony2 goes, just looking at the documentation makes me want to run screaming. It's not mature in any form or fashion (but, then again, neither is Symfony "legacy"). So, I'm not sure if the process would be any easier there.

Categories