PHP - best practice for displaying a select list - php

I am using the MVC concept (as per http://r.je/view-helpers.html) in my PHP project:
Controller - processes user interaction;
View - displays any data and or fields;
Model - handles all business logic, including access to data access objects.
A front controller is instantiating the MVC triads. In this structure I need the View to display html select option lists that are populated from the database, along with any user fields the various domain objects require data from. I hope this is clear.
My question is: the View cannot ask the database for the data to populate the select option list, so what is the generic application flow to "pass" the View the required form data to correctly populate the required select option list?
NOTE: The View currently gets user data (in a Request object) from the Model, and any validation errors the View has asked the Model for.

Related

Symfony 2 Forms one to many without collection (only add one item)

I'm building a simple form having a one-to-many relation from an User entity :
User 1..* actions
So basically, in my user form, i have to set the actions property to the collection form type.
I don't want the client to be able to add multiple actions within the form, but only one when editing the user.
The problem is that Symfony 2 form expect to get all the actions from the form on submit, so existing actions are removed from database on flush and only the new one remain.
Any idea ?
If your entity User has an array of Actions, and you create a form type UserType with data_class => User, Symfony expects you to add to that form a collection field for actions, because that's what the entity has.
That's the way Symfony works. Personally I don't like this tight coupling between entities in the model and forms, but it works pretty well for simple apps.
If you want to have a form type that does not match any entity, things won't be so simple, but you'll have more control of things.
You have some options (as far as I know, maybe there're other):
You can move to a task-based UI, by creating another class that represents the data you need to perform an action - in your case you want to edit an user, so you can create a EditUserCommand, which contains 1 and only 1 Action, as long as other fields, most of them probably exactly the same as your User class.
Now you can map your form to this EditUserCommand class, which will contain only 1 action as you want. Obviously later you need to use this command class to get the data you need to edit the user.
Use the empty_data option with a closure that receives the submitted FormInterface, so that you can get the submitted data from there and use it as you want to update the user.
In both options you're breaking the coupling between your User class and your EditUserType form, which means you need to do some more work, but imo it has many benefits.
You can find further reading on this subject here.

Is there such a thing as a partial domain model?

I’ve built a PHP ‘personal profile based’ website that has a number of ‘Category’ pages, each of which list a number of Profiles.
Category page: Lists profiles (images/names in a gallery-like layout)
Profile page: Shows in-depth information about one person/profile
Im using a kinda-MVC-esque architecture here. So, when a visitor goes to a Profile page, the appropriate Controller asks the ProfileDataMapper to fetch the relevant profile. The ProfileDataMapper returns a fully formed Profile Domain Object to the Controller which then sends it to a Template View.
The domain objects are all populated from arguments to their constructors, and have validation checks in them so that if any arguments are missing or bad, they will throw an error and not be instantiated.
The problems start when I want to list a number of Profiles on a Category page. Here I am asking the DataMapper for an array of Profiles. But it is returning a list of whole complete Profile Objects when I know that in this case I only need perhaps a Name and URL. It is putting too much strain on the MySQL DB behind the mapper.
The obvious solution would be to simple make the DataMapper pull just the few fields I really need for the list. But then my Domain Model validation tests would fail due to missing data (they are expecting all fields).
So now I am thinking I should create a new Domain model called PartialProfile or ListableProfile. This domain model would just have a name/url and would only validate those two fields. The ProfileDataMapper would return PartialProfiles in its list method instead of Profiles. Maybe the Profile could even extend from PartialProfile.
Is this 'partial model' a good way to go? Or should I refactor my validation code somehow? It seems to me I need different 'levels' of validation. Sometimes I need the object with all data. Sometimes I don't.
Thanks kind people!

Creation of dynamic datafields cakephp

I have planned to create tables for dynamic datafields. My concept is to store a dynamic form data inside tables(MySQL). The user can select any number of fields(datatypes) from form.The form is a dynamic form one seen in here . How can we design dB in order to save user's data dynamically?
Can you not just allow for fields within the database to be null. That way when you save the form if any fields have not been filled it doesn't throw an error
Seems, you need to implement EAV data model wiki link Also, there are some ready solutions
some project on github
EAV behaviour on bakery, also one of project from github
expandable behaviour

Importing rules and data from different models in yii

How can i pull data from different tables(models) into view in yii. actually i done this with loadModel method. but my question is how can we import the rules into the view too. Here comes my scenario
I have a User model and Profile model. The User model contains username and password and Profile model contains userid,name,address etc. so in my profile edit view i need all these data with the rules, username-unique,password,confirm pasword-required, etc., i can implement the required rule to all these, but i dunno how to import the table related rules like unique.
So basically this is a form that takes in two models and displays that data for you to edit and submit.
Simply make the render call to your view and pass both the models. e.g. $this->render('aview', array('model1'=>$model1, 'model2'=>$model2));
Get your view to display the form elements based upon these models.
When you submit simply create new objects for respective models and populate them with the data received. e.g.
$model1 = new model1;
$model1=>id = id; //id received from the form submit.
...............
Once you have the models populated you can call validate() on each of them to figure out if the data is according to your rules. If it is you proceed otherwise you display error. I hope this helps unless I missed something in your question.

What's an elegant way to handle an invalid form in a MVC application controller?

I have a custom MVC application/framework where each action is a function inside of a controller class.
I have a signup action which renders a view with a form requesting user details. On submit it posts to a processSignup action that validates the data and either inserts the user into the database and redirects to a success page or needs to redisplay the original view/form with the errors.
The signup action runs several queries and hands off a variety of data to the view. E.g. it needs to get a list of available countries that the user can choose in a drop-down, retrieve a list of suggested usernames based on the name they entered on a previous form, and retrieve other data from a model.
I'm trying to find out how I can avoid repeating all these queries and the render of the view in my processSignup action in the event of an error. What's the standard way to handle these situations? The signup action receives POST data from a previous form so I can't have the signup form submit to itself and branch based on GET vs POST.
I could just call the signup function directly and have it receive an optional parameter to differentiate between normal requests and ones for invalid forms but that seems hackish. How do other frameworks handle this scenario? I'm using PHP but this is more of a high-level question than one specific to the language.
Avoiding duplicate code certainly isn't unique to MVC frameworks. The standard approach would be to simply create a private function that both signup and processSignup call before rendering the view. Something like:
private function setCommonVars()
{
// set country, username arrays, etc...
}
As far as rendering the signup view, the flexible thing about MVC is that you can reuse views. Your framework should have a way of explicitly choosing a view within a controller method (e.g., $this->view = 'signup'). That way, if an error occurs in processSignup, you can choose the signup view before rendering.

Categories