Use Symfony entities and doctrine mapping without database - php

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).

Related

Symfony2 manage multiple databases (from other framework)

I have a problem. My client wants to have a website in one symfony framework package and the CMS integrated in the other Symfony framework (Service Panel of the company).
So, we have Symfony A and B.
Now that there is a blog on the website (Symfony A) among a few other dynamic pages. All the Delete, add and update stuff needs to go into the service panel (Symfony B) with forms and such.
I know I can use multiple Entity Managers, but that'll only work if the website bundle is placed in the service panel framework (Symfony B), because of the mappings. I think it isn't possible, but they really want to have the CMS in their Service panel and the website in another symfony framework instance.
Is there a 'remote' solution, so that Symfony B (service panel) can change and manage the database of the website (Symfony A) without migrating the website bundle (Symfony A) into Symfony B? The website will run on the same machine as the service panel will. Only difference is they are completely separated of each other.
If I'm not clear enough, please tell me. I'm trying to be as clear as possible.
Hope somebody is able to help me with the issue.
So let's say we want to update a blog.
Symfony A would process these requests:
GET /blog/42 - Returns json rep of blog 42
PUT /blog/42 - Accepts and updates blog 42
Easy enough. FOSRestBundle and JMSSerializerBundle can help with some of the plumbing.
So Symfony B uses curl (probably) to GET /blog/42 and converts the json data to a php array.
At this point you have a major decision. If the entities are simple and stable the Symfony B could just use the array representation. Symfony forms accept arrays no problem. So this maybe all you need.
If however you want to convert the array data into a real Blog object that matches Symfony A then you need to make some more decisions. The Symfony A entities are really just plain objects. You could tweak Symfony B's autoload so it can see the Symfony A entities. So B can get the entities from A (and then map the array data to the entities) without having any direct links. Autoload just needs to know where the Symfony A entities live.
Or you could go one step further and make a BlogModelBundle in which the blog entities (without any doctrine stuff) are defined. You would then have both applications load this bundle. The A app would then extend the model and add the doctrine 2 stuff. The B app would just hydrate the models using json. This is probably the best approach for the long term.

Doctrine 2.2 Autogenerated Proxy Files cannot be Autoloaded

I have integrated Doctrine 2 with ZF1, using both the Bisna integration and my own custom integration.
The models work fine in unit tests via the shepp.
However when I run the web application, I get an error with the Proxies. The proxy for a model User.php is __GC_User.php and is placed in the correct location. However I get an error when the auto loader tries to load the Proxy class since its looking for \_GC__\User.php.
How can I fix this?
I thought that I would post an answer to help others dealing with Doctrine 2 proxies. My problem was that I was storing an entity into a Zend_Cache instance, and the entity had some Doctrine2 proxies attached.
So when I removed the caching then the issue went away. So the rule of thumb is do not store entities in Cache, just store identifiers

Using Symfony doctrine models in a REST framework

I have a web application which has been developed with symfony 1.4. I have a pretty large code base (and growing). Circa 80,000 lines of code (actions, forms, models, templates etc.)
I'm using the default doctrine version which ships with symfony 1.4.
I've just started developing a mobile version using Sencha touch. I don't wish to use symfony for the REST web services because:
REST services in Symfony 1.4 is not great. For example, If i want a PUT request I have to pass a 'sf_method' parameter specifiying that the request method is PUT. This isn't true REST and it's not ideal for Sencha touch.
I don't need all of the unnecessary symfony functionality(for example the plugins that are autoloaded in the ProjectConfiguration file, the form framework etc.) that you'd use to develop a standard web app. All I need is to define my REST routes and return the specified JSON (as everything that needs to be returned for Sencha touch will be JSON)
I want to keep my mobile app as bloated-free, efficient and quick as possible. And unfortunately for this task, Symfony 1.4 would not be the best choice for using as the backend architecture for my mobile app. If I had chosen symfony2 (it was in it's beta phase, alas) it would be a different story as symfony2 supports true REST functionality. What I do need, however, is the ability to use my current doctrine models (I have circa 90 models) in a chosen REST framework.
Basically, in a nutshell what I need is as simple as this:
Call a rest route->Query my doctrine models->return the JSON without using symfony.
So my question, what would be your advice? I don't want this to be a question of which is the best PHP rest framework, however, what I would like to know is what would be a good REST framework which i can develop efficiently and quickly REST service, make use of my doctrine models and is easily extendable.
Here at my employer, I've created a rather big application with a ExtJS frontend, and Symfony 1.4 backend. And two be honest, I don't feel limited by Symfony 1.4 in any way?
First of: I created my own base controller class (which extends sfActions). This controller can handle (render) different types of data. It has generic handling for Doctrine_Query, Doctrine_Collection, Doctrine_Model and array types.
Also the plugins make me help organize the code, and in some cases plugins are shared between differend projects, so that's also a big plus.
And the extra functionality like forms: it's only prepared for you in the autoloader, you don't have to use it. And I don't think it causes any real performance issues (at least not for me). But I like to use the extra sfValidator framework, to make sure data are correct.
The only real "problem" is indeed the HTTP REST-ful commands, especially PUT and DELETE. I just worked around this problem by generating a controller for each 'manageable' model, and implement specific get, list, create, update and delete actions. So when I would like to manage an Object, I call the objects controller, which has executeCreate, executeUpdate and executeDelete actions.
The reason I read, was that Symfony didn't and couldn't implement this feature because PHP has really bad support this. I don't know if this is true, but if this is your only 'real' issue, you could try to fix this in the Symfony core.
So my advice:
If the raw performance is your problem: try profiling your code, install a opcode (APC) cache, and profile your code (yes, that's double).
If the HTTP PUT command is your problem: I would either work around this (that's the way I solved it), or try to fix it in the core.

Zend 1.11 and Doctrine 2 Auto generate everything needed from already existing database

I am new to ORM and I am really keen to learn it. I successfully managed to install all classes and configurations for Doctrine 2.1 with Zend 1.11.x by following this tutorial.
http://www.zendcasts.com/unit-testing-doctrine-2-entities/2011/02/ Which uses Bisna plugin and doctrine scripts.
Now my problem is he is clearly explaining how to create entities and tables through doctrine classes but do not explain how to auto generate the proxies and repo classes from already existing database which helps me to select, insert and update. I always create my databases using MySQL Workbench.
I also followed the below tutorial as well
http://www.zend.com/en/webinar/Framework/70170000000bSrG-webinar-zf-v-1-doctrine-v-2-20101214.flv
My database is so complex with relationship flowing across every possible way. If I follow the steps which is explained in these tutorials I will never complete my project. Can any one please explain how to start using Doctrine after configuration. Considering I already have a database and my Model folders are empty. I have my folder sructure as below.
C:/zf/library/Doctrine
C:/zf/library/Symfony
C:/zf/library/ZC -- (my model which should contain the proxies and repo of Doctrine. At the moment it contains nothing.)
C:/zf/library/Zend
C:/zf/scripts/doctrine.php
Please help me!
I posted this same post yesterday and no one replied to my post. Please let me know if you need anymore information from me.
Thank you,
Karthik
According to Doctrine you should create your entities first yourself and then create your database schema from these entities.
But because you already have a database you probably don't want that. It is possible to convert your database to Doctrine2 entities in PHP, XML or Yaml.
You should take a closer look at the commandline tools Doctrine offers with the Bisna glue because there you can generate a lot of stuff.
To generate your entities FROM your database consider the following command:
php doctrine.php orm:convert-mapping --from-database php ../library/Application/Entity
You can also define the namespace and a base class which your entities have to extends with: --namespace=namespace and --extends=class.
Doctrine2 warns you to convert your database to entities because not everything could be auto detected or supported. For instance ENUM datatypes are not supported by default in Doctrine2 so converting your database will raise an error.
It's a good idea to check all your entities especially associations before you use them. Hope it helps you.
If I understand your question correctly, you have your entities already configured and need to auto-generate your proxy and repository classes.
Both can be created using the following Doctrine CLI commands from your application's root directory:
php scripts/doctrine.php orm:generate-proxies
php scripts/doctrine.php orm:generate-repositories library/
If you're looking for a way to auto-generate your entity classes, unfortunately I don't think a solution is available for this yet.
A support rep at ORM Designer said they are "working on" this feature and that it is "very demanded." Here's hoping it will be included in ORM Designer 2.0 since there is generally a lot of repetitive work involved in coding/mapping entity classes that could likely be automated.
You can use the orm:generate-entities command if you provide mapping information in either XML or YAML format.
See http://www.doctrine-project.org/docs/orm/2.1/en/reference/tools.html#entity-generation
For development, set proxy generation to be automatic in your config, otherwise, use the orm:generate-proxies command.
Unless you need to customise your repositories, generic ones are created in the entity manager when requested. To specify custom repositories, simply use the repository-class entity mapping attribute.
See http://www.doctrine-project.org/docs/orm/2.1/en/reference/xml-mapping.html#defining-an-entity for an example

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