Is there a reason why Create and Update are distinct when making MVC application in PHP?
I see why they are distinct in theoretical explanations, but is it worth keeping separate actions and views for create and update in CRUD controller? If I only need to change the title of save button it seems logical to have one action for both create and update operations. which will save object as new DB row or update existing depending on the id it has (or hasn't).
PS. I've always merged them into one action and recently when started searching for information about Zend Framework I've found that every tutorial with some CRUD functionality has distinct create and update actions and views that have almost the same code. It seems strange why authors copy-paste 90% of the code from one method to another.
I think you're asking more of a style question than a purely functional one. For me it's separation of concerns - there is no guarantee that create and update will truly overlap, and there may well be functional differences, so it's a potential landmine in the future to lump them together.
Related
I just started to work in symfony 3.4 and i'm pretty lost.
I want to make a CRUD for Products.The products table have multiple columns(name,description,CATEGORY_ID).
In create page, for this category_id, i want to make a select with the names of the categories that are stored in another table(id and category_name).
For this i need to select all the categories, but i don't really know how to do that.
I worked in codeigniter before and there i would make a function getAllCategories in category_model and i would call it in Controller.But here...i don't know how to do this.
Can you explain to me please how this should work?Where should i make the sql functions?
Your seem lost on things that are very basic for Symfony.
You probably have to start reading a lot and from the start.
You have two parts that you have to cover.
One is doctrine and how you can declare entities and manage them with the entity manager, and the second is Forms and how you can use them to render your form (including the foreign entity relation), to handle and validate your submitted data, and to pass the validated data to the database through the entity manager.
Your example is pretty simple, so it should be very easy to follow a tutorial while modifying it to fit your code.
I would like to know, if I am following the best practices of development in CakePHP framework.
If you have for a example an Invoices Table, and you want to display the status of the Invoice, let's say you want a green label for the status of the invoice like this:
<span class="green">Paid</span>
What i do is the following. I create a virtualfield on my model named statuslabel, in that virtualfield i call a Helper that displays an element.
so it would be Model->Helper->Element
That way anywhere in my application i can call statuslabel like it was a field from that model and i would get the statuslabel.
My big question is if this is the right way to do it or i am doing it all wrong?
You should avoid violating the MVC principles in your code, in this case specifically containing presentation logic in the model code. This equally applies to CakePHP and other MVC frameworks.
You might still go ahead and create a virtual field called status which can calculate the status of your invoice based on other entity fields. If you have the status stored in the database already as a normal entity field, then you can skip the virtual field.
The view layer is where you want all your presentation code. You can either do it directly in views, or move the logic to the helper. The appropriate function in the helper would take your status field as input and output the appropriate classes into the template based on the status value.
The advantage of keeping presentation code in the view layer (template and helper classes in CakePHP) is that it is easy to replace it later on by updating your templates. CakePHP 3 also gives you the option to create themes via plugins, which is how you can easily change the UI of your application on the fly, but only if you stick to the design principles of MVC.
Generally using virtual fields are not an bad idea. Sometimes the logic behind simple answer is quite complex that it can not be done via SQL or with reasonable amount of SQL.
It is important to remember, that most of the time, using queries directly is much faster without extra PHP processing. So for what comes to your question, if you don't have any PHP logic (besides generating SQL expressions), I would say go with it but...
...There is also point for discussion about database design.
I can't really say about your database schema behind the question, but for me such simple thing than invoice status should definitely be in database directly as and int type field. So what comes to my experience about invoicing systems, database schema could be something like:
invoices
id
invoice_status_id
invoice_statuses
id
title
If you create your model associations and query your database correctly, you end up having invoice status in query results and you can use it just like described.
Btw. most of the time, using reusable elements is good practice. Less code to maintain :D
Which way is better to do tests in Symfony2 RESTful application or in any others applications.
Example:
I want to test PostController, which contains actions: getPostsAction(), getPostAction(), putPostAction(), postPostAction(), deletePostAction().
So I've created class PostControllerTest.
Question is:
a) Should I create individual test for each action? Ex - testing updating:
first load posts from fixture
get id of one of this added posts
update this post through PostController#putPostAction
check updating result with entity manager
b) Or test few actions in one test(every through controller)? Ex:
first test creating
next listing(expecting added post)
updating
listing(expecting updated post)
deleting
listing(expecting 0 rows).
I think first option is OK, because I test only one controller action in one single test. I exactly know what I want to test.
But in second case I can do tests faster and don't need use fixtures and entity manager.
Which way will be better and why?
I cannot say for sure what's the best solution for you but I think a good idea is to make it more functional as you said.
You can make your tests to follow a flow, like:
Create user
Update user
Delete user
Making a complete cycle without leaving trash data.
I have an opensource project which I do exactly like this, and I also use some traits to help me on creating objects and validating responses from my Rest calls.
You can see a real test in here: https://github.com/renatomefidf/sammui/blob/master/src/Renatomefi/TranslateBundle/Tests/Controller/ManageControllerTest.php
By using the #depends annotation from phpunit you can determine the flow and make sure you have a logical path for your tests.
I hope this helps you, and if you have more questions let me know!
I have rather theoretical question about proper approach of using Symfony though I believe the approach is to be same for any other PHP framework.
I have tariff objects stored in database. I want to provide a cost model for each tariff basing on user input.
My initial approach was to create an array, one's each element would contain data from corresponding tariff object and calculated data. All of that was done in controller's action method.
Later I have created another class CostModel and then created an array CostModel[], which than was passed to $this->render() method. Again it's done in controller.
This approach works well enough. However, since I have not much experience with Symfony, I have doubts that this approach - performing calculations in controller - is good one.
Is there any better way for this?
Well, your question could have more than one answer as it is very opinion-based.
What I can say without any doubt about controller's code is that the less it is the more is good. Why I said that? Because controller code isn't reusable, because controllers are made to "connected" views and business logic (keep attention: connect, not encapsulate) and a general rule that I follow when I develop with Symfony2, is to write, into controller, lines of code for "objects" that are directly accessible from controller (form, request, views, and so on); all code that isn't related to these concepts should be migrated elsewhere.
Your solution is a good starting point but we cannot judge as we haven't more details and we don't know the architecture of your software. What can I say - and I hope you already know - is that you can pass to render (so to view templating system; I suppose you are using twig) directly the ArrayCollection you've obtained querying the database (so basically you don't neeed CostModel[] array). So, maybe, your approach is good but not the best: maybe you can take advantage of Repository facility, write a good query that can extract and calculate data for you (in a more optimized way) and use repository directly into controller. That way you could at the same time, migrate code where it should stay, write less number of code lines, do some optimitazion (or better, let Doctrine do for you) and you don't need to create a brand new class (model).
I'm using Codeigniter to flesh out a pretty large project (especially for a n00b). One issue I'm having is how to organise my files and methods. I've broken my project down into features - the app is a task management software so already we have basic features such as "Task", "Project", "User" etc.
The way I intend to do this is by creating controllers for each and then following CRUD methodology in each. So for example in Task we would have the following methods:
create()
read()
update()
delete()
This makes sense in my head. Now in terms of Views, should I have multiple views, or should I combine create and update into the same form? Also, where does non-View functionality go, such as setting cookies etc?
This is quite a specific question but if anybody has any more holistic guides on general structure convention for CodeIgniter projects I'd be very grateful.
I'd say you got it right. This is what I do.
I tend to use the same view for create and update, keep it DRY (don't repeat yourself) if you can.
Non-view related stuff that does not handle anything business-related goes in what I call helper-classes. If it's business related, I put all the logic into services, so I can unit-test them without being dependant of any framework (not sure how new you are at this, but oh well :) ).
You can also use Grocery Crud, a library that provides out of the box CRUD functionality for codeigniter.
It handles pretty good 1->n and n->n relationships so its convenient for small projects.
I don't know if you are familiar with it. If not give it a try. It will save you tons of time and effort.
My controller consists of these methods, which follows REST API guidelines:
read -> get all records.
find -> find record by primary key/id.
create -> show the form view.
store -> insert data from the form into the database.
edit -> show the form view, populated with current records' data.
update -> update data from the form into the database.
delete -> delete data from the database.
This is called Resourceful Controllers.
Yes, you can combine create and edit in same form. However, there are cases those require you to use different create and edit form. In that case, just make 2 separate forms.
And... like #Theodore suggested, GroceryCRUD is worth a try if you don't need too many customizations.