Jump to content

Mapping data to Models


Drongo_III
 Share

Go to solution Solved by requinix,

Recommended Posts

Hello...(again)

 

I'm hoping for some advice from the good folks here.  Sorry I keep asking questions but I work alone so it's kind of hard to code review your own ideas...

 

Anyway I have a User Model and I want to be able to pass data submitted via $_POST ( which has been validated) to the model and map each piece of form data to its intended property.

 

I was thinking of creating a mapping class (basic outline below).  Essentially I'd have an array with form field names as a key and then a sub array mapping to the name of it's counterpart  setter/getter method on the user model.  

 

In one respect I think this is good in that the model remains ignorant of the form.  On the other hand the array could become quite cumbersome for forms with lots of questions.  

 

Does anyone have a view on whether this is a good/bad approach? And whether there's actually a better way to shuttle $_POST data to a model and getting form data back from a model?

/**
* Array which has each form field name as it's primary key that maps to it's 
* equivalent getter/setter on the desired model
*/
$formFieldToModelMapping = array(
	
	'name'=>array(			
			'modelSetter'=>'setName',
			'modelGetter'=>'getName'
		),
		
	'email'=>array(			
			'modelSetter'=>'setEmail',
			'modelGetter'=>'getEmail'
		),
		
		/* Repeated for each form field which has an associated proptery on the user model*/
);



/**
* @param $model - some User Model
* @param $data - array of validated $_POST data e.g. $_POST['name'], $_POST['email']
*/
public method transferFormDataToModel($data, $model){ // $_POST 
	foreach($formFieldToModelMapping as $formField=>$accessor){
		
		if( isset($data[$formField]) ){
			$methodName = $formFieldToModelMapping[$formField]['modelSetter'];
                      $model->$methodName( $data[$formField] );
		}
	}
}

//NOTE THIS IS JUST TYPED IN AND NOT TESTED CODE
Edited by Drongo_III
Link to comment
Share on other sites

Having to maintain a mapping is cumbersome. Do you have any attachment to being able to separate the model from the form? Would it be so bad to consider the model (this model) as a "form model" and have form fields map directly to object properties?

Link to comment
Share on other sites

Having to maintain a mapping is cumbersome. Do you have any attachment to being able to separate the model from the form? Would it be so bad to consider the model (this model) as a "form model" and have form fields map directly to object properties?

 

Thanks for picking this up Requinix

 

Well the thinking was I could use mapping classes to:

 

1) Shuttle data from forms to the model (and model to forms)

2) Shuttle DB model data to an instance of a model

 

And thereby have it all sort of neatly managed in one spot.

 

I guess the thing that drove me towards considering a dedicated mapper class is that the model would need to retain references to form field names which won't always have a 1:1 relationship with model property names. And the question in my mind was 'should the model need to know anything about the form?' because there will be a few properties on the model which don't all come from the form.  This sort of left the model feeling like a mixed bag.

 

 Is it not considered good practice to utilise mapping classes? And if you do use a mapping class is it ok to list out the getters and setters as I have done?

Link to comment
Share on other sites

  • Solution

Mapping tends to be a thing when you're working with a framework that already has its own model system - and mapping practices.

 

If I were implementing this idea on my own then I would keep it simple: form model's properties match the form fields and it has load/save methods to import/export a database model, or models as the case may be (and obviously another method, likely inherited, for populating from request data).

Link to comment
Share on other sites

'name'=>array(            
            'modelSetter'=>'setName',
            'modelGetter'=>'getName'
        ),
        
    'email'=>array(            
            'modelSetter'=>'setEmail',
            'modelGetter'=>'getEmail'

I would assume all form fields already match the method property and all getters and setters follow the same naming convention and then only need to define something in a mapping array for the few that do not. Set up throws or user errors in the model to notify you a.s.a.p. of a field your form is sending to the model that does not exist on the model and has not been mapped.

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.