Skip to content

Instantly share code, notes, and snippets.

@kamaulynder
Created July 15, 2014 06:02
Show Gist options
  • Save kamaulynder/e052d53b7be2dd5b930d to your computer and use it in GitHub Desktop.
Save kamaulynder/e052d53b7be2dd5b930d to your computer and use it in GitHub Desktop.
protected function create_or_update_tag($tag, $post)
{
// Check
if (isset($post['parent']))
{
// If we have a parent array with url/id
if (is_array($post['parent']) AND isset($post['parent']['id']))
{
$post['parent_id'] = $post['parent']['id'];
}
// If parent is numeric, assume its an id
elseif (Valid::numeric($post['parent']))
{
$post['parent_id'] = $post['parent'];
}
else
{
// Try to find parent by slug
$parent = ORM::factory('Tag', array('slug' => $post['parent']));
if ($parent->loaded())
{
$post['parent_id'] = $parent->id;
}
}
}
$tag->values($post, array(
'tag', 'slug', 'type', 'parent_id', 'priority', 'color', 'icon', 'description', 'role'
));
// Validation - cycle through nested models
// and perform in-model validation before
// saving
try
{
// Validate base form data
$tag->check();
$tag->save();
// Response is the complete form
$this->_response_payload = $tag->for_api();
$this->_response_payload['allowed_methods'] = $this->_allowed_methods();
}
catch (ORM_Validation_Exception $e)
{
throw new HTTP_Exception_400('Validation Error: \':errors\'', array(
':errors' => implode(', ', Arr::flatten($e->errors('models'))),
));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment