Skip to content

Instantly share code, notes, and snippets.

@selvinortiz
Last active December 24, 2015 15:29
Show Gist options
  • Save selvinortiz/6821005 to your computer and use it in GitHub Desktop.
Save selvinortiz/6821005 to your computer and use it in GitHub Desktop.
Craft:Record:getInstance
<?php
namespace Craft;
class Plugin_BaseRecord extends BaseRecord
{
public static function getInstance(BaseModel $model=null)
{
$record = get_called_class();
$record = new $record;
if ($model) {
$props = $model->getAttributes();
if ($props && count($props)) {
foreach ($props as $prop => $value) {
$record->$prop = $value;
}
}
}
return $record;
}
}
// Elsewhere in your service layer
$record = Plugin_Record::getInstance($model);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment