I am new to Symfony2 and I am not sure where I should save a class that updated multiple tables(entities).
From reading documentation and tutorials it says I should not put any other tables reference within the entity class; I could put it within the controller class, but again many people have said this class should be as simple as possible and not include business logic; Not in repositories, because these are used for query data and not for update or inserting.
Is there a standard folder structure where another type of class for working with multiple entities(tables) should be saved? Should the business logic really be stored in the controller classes?
Symfony2 is very flexible in this regard.
You're right, entities are for one "table" only.
I would suggest you look into Services, as they are a good way to move your code from a controller to a separate class. You basically call your service and use the functions it provides. This will slim your controller down.
Related
I am developing a little application with PHP web framework Laravel v6 and MongoDB (with jenssegers moloquent) as database engine. This is my first encounter with any MVC framework. I have the following collections in my database:
allPaintingsCollection
paintingHistoriesCollection
paintingCategoriesCollection
artGalleriesCollection
paintingArtistsCollection
supervisorArtistsCollection
smPlatformsCollection
nonSmPlatformsCollection
targetSchoolsCollection
I have been following this tutorial. I have the following two questions:
Do I have to create a separate Model (separate class in a separate file) for each collection above?
Do I have to create a separate Controller (separate class in a separate file) for each collection above?
New Answer:
1. Yes, you will need to create a separate model to use the ORM. Ideally, the model should only support a single collection, doing so you can create custom logic. Remember, One Model One Collection/Table.
For Controllers, you can write it any way you like, you can use all models inside a single controller. But what I have learned so far using Laravel, you should create a single controller for each model to fully and logically support the separation of concerns.
Know the BTS:
Laravel heavily uses PHP Composer's autoloader functionality, so having multiple classes in a single file won't work.
For example, when autoloader starts, it would look for User class in \App\Models\User.php file. Having multiple classes in a single file won't help in this case.
Latest Laravel's version follow PSR-4 standard. You can have a look at it for more understanding.
For more PHP coding standards you can have a look at PSR-2 standards.
I have 2 projects accessing the same DB. One is with CodeIgniter, the other is a home made mvc framework. I'd like to use the same Model layer.
Is it possible to access Codeigniter Models from another project?
I'm not exactly sure why you would want to be accessing the same DB from two different frameworks at the same time (sounds like a recipe for disaster), but in general, I would say, "no."
What you actually want is not the model itself, You actually want the active records class located in /system/database/DB_active_rec, as that's the most common usage.
That class extends CI_DB_driver
This class accepts the config parameter for the DB(connection information).
You then want to extract the drivers themselves being used for the specific database you're working, the drivers can found at /system/database/drivers.
CI->model simply loads the DB_active_rec, that's why you need to do $this->db->insert()
I've never did so myself, But I don't see any major dependencies in the files themselves. I might have missed something though
Recently I began to study SF2 but there are some doubts which I'd like to solve:
Let's say that I have a lot (more than 100) of Models which extend one generic model(Entity). Each model has unique functions (rules for parsing content from model source), what is the best way to organize it?
In ZF1 I would create Model/ModelType/ModelName.php file for every model which need customization, in SF2 I'm not sure what kind of entity should I use: One service container which calls entities and call their custom functions (implemented through interface), or maybe one huge Service which has different functions for every single model?
Thanks in advance.
If parsing functions based only on current entity state (use only entity property) it's better to use entity method. Entity aslo can implement some interface, e.g. ParsableInterface.
If paring function need to use another entities it's better to use standalone services handle parsing
I do have an module e.g. account. Of course you will find a file called in acount/actions/action.class.php.
The PHP-file action.class.php is getting big. Is it possible to extend it?
for an example:
/account/action/action.class.php
/account/action/action2.class.php
If it is possible, how does the code look like in action.class.php and in action2.class.php? And/or where should I enter something in a config-ymal-file?
Thanks in advance!
Craphunter
Symfony actions can be declare in two flavors:
A big actios.class.php file that inherit from sfActions
Multiple xxxAction.class.php file that inherit from sfAction
The are both basically the same but these cant be mixed (you must decide if you want 123123 files, 1 per action, or one big fat file).
Here its the symfony reference on this matters: Symfony Front Controller - Actions check the Action section.
It sounds like the action class is getting too big because you have too much stuff in it. I would suggest breaking it into multiple modules or moving relevant parts of the logic code into your models.
Adding an include for an action2 file is not how Symfony 1.4 is intended to be used and will likely just lead to all kinds of other headache.
PHP doesn't support 'partial classes' like C#. So you have two options:
Create an inheritance chain, by creating a baseActions class which inherits from sfAction. Then create your MainActions class which inherits from baseActions. You can add different layers of inheritance.
Group functions in seperate classes. Override the execute() function in your actions file, and manually call the function on the class you need.
Also consider if some of the behavior you're implementing in the action (controller) belongs to the model (or the view) instead. If the really belongs to the action just follow guiman's advice and create an action per class inheriting sfAction.
If I have a CRUD module based on a model (as generated by the CLI), and I have to define additional behaviors for that model, I tend to create another module to contain that behaviours. E.g., considering a model Article. I could create an article module for the CRUD behaviors (generated), article_support for additional behaviors like moderation, activation, etc. and perhaps article_ajax for async requests.
What I have is the following db structure(tables):
lists[name,id]
list_items[title,list_id,content]
I've created the needed files and code(the MVC) needed to manage the first table(lists).
I also added the hasMany to the model class. At that point I am stuck.
What I need is a solution for managing each item (basic CRUD, I assume that complex management is just an advanced CRUD that I will find out how to do by myself).
I will be specific: since it's a content that have no place (but the admin) that it will be used by itself, should I -
create a full mvc structure for it? (can or should I implement it somehow[how?] in the lists package?
if not, how can I attach the tables? (since the use is about to be dropped in version 2)
would an element(cake concept/context) will be the appropriate way to create a view for such situation?
ANY insight will be appreciated.
If I undertant correctly, you want to create a CRUD part of this tables by yourself, without bake.
You need to write all the MVC estrucure and be carefull with the naming combention of cakephp http://cakebaker.42dh.com/2006/02/18/cakephp-conventions/
You need the model into app/models and also a a controller into app/controllers (remember naming combentions) and for each model you need a folder into /app/views.
Alfo, every, every function in your controller needs a view, even if this action doesn´t write anything to screen
I hope this was usefull.
Have you tried using Cake's bake feature? Your CRUD will be automatically created in about 2 seconds. I would also recommend you do the Blog tutorial to get a feel for scaffolding.
CakePHP is all about convention over configuration. Eg naming conventions for tables, controllers, models etc.. So much can be done automagically.