Skip to content

Instantly share code, notes, and snippets.

@DeaconDesperado
Created September 8, 2011 19:37
Show Gist options
  • Save DeaconDesperado/1204451 to your computer and use it in GitHub Desktop.
Save DeaconDesperado/1204451 to your computer and use it in GitHub Desktop.
ORM validation
<?
class Model_Blog_Blog extends ORM{
protected $_table_name = 'blogs';
protected $_has_many = array('images'=>array('model'=>'blog_image','foreign_key'=>'blog_id'));
public function rules()
{
return array(
'title' => array(
// Uses Valid::not_empty($value);
array('not_empty'),
// Calls Some_Class::some_method('param1', 'param2');
array('max_length', array(':value', 120)),
),
'body' => array(
array('not_empty')
)
);
}
}
class Model_Blog_Image extends ORM{
protected $_table_name = 'blog_images';
protected $_belongs_to = array(
'blog'=>array(
'model'=>'blog_blog','foreign_key'=>'blog_id'
)
);
}
<?
public function action_write(){
if (HTTP_Request::POST == $this->request->method())
{
try
{
$blog = ORM::factory('blog_blog');
$blog->values($this->request->post());
$blog->save();
if(!empty($_FILES)){
//apply images
foreach($_FILES as $file){
$img = Image::factory($file['tmp_name']);
$blob = $img->__toString();
$bimage = ORM::factory('blog_image');
$bimage->image = $blob;
$bimage->blog_id = $blog->id;
$bimage->save();
}
}
}catch(ORM_Validation_Exception $e){
print_r($e->errors('models'));
}
}else{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment