In a follow up to my earlier question, I'm focusing on cleaning up my code to keep it incredibly simple. I've moved on from the Controller and Model to the Model and Service. Typically you would have something like the following in a service method:
$record = new MyRecord();
$record->name = $model->name;
$record->anotherField = $model->anotherField;
// etc..
Which works and you end up defining the attributes in _attributes
as an array like below:
[*:_attributes] => array
(
'uid' => '0'
'name' => 'My Name'
'anotherField' => 'Another Field'
)
The logic that I am looking for, which I'm sure someone will tell me I am just using the wrong method, can be seen in the controller.
public function actionSave()
{
// require a post request
$this->requirePostRequest();
// assign the fields from post request
$post = craft()->request->getPost('fields');
// create a model, populate with the assigned fields
$model = MyModel::populateModel($post);
// validate attributes
if ($model->validate()) {
// save the record
craft()->myPlugin->saveNew($model);
// redirect to posted url
$this->redirectToPostedUrl();
}
// grab errors from the model
$errors = $model->getErrors();
// return errors to the template as 'errors'
craft()->urlManager->setRouteVariables(array(
'errors' => $errors
));
}
In the controller code, I'm populating the model
with the post
with this line:
`$model = MyModel::populateModel($post);`
Is there a way to create the same behavior for the method in the service to "automagically" assign the model attributes to the $record
?