Fuelphp orm if id not exsitst
I ran into a small problem what i cant really solve.
I created a delete function for videos in fuel php with orm, and maybe its a but stupid but i cant really figure out how to redirext the user if and id doest not exsits.
here is my code
public function action_delete($id)
{
$deletevideo = Model_Video::find($id);
if($deletevideo->user_id != Session::get('sentry_user')):
Session::set_flash('fail', 'The video you want to remove is not yours, we logged this activity');
Response::redirect(Uri::base() . "myvideos");
else:
unlink(realpath("users/video/" . $deletevideo->video_preview));
unlink(realpath("users/video/" . $deletevideo->video_file));
$deletevideo->delete();
Response::redirect(Uri::base() . "myvideos");
endif;
}
html
<a href="<?php echo Uri::base(); ?>video/delete/<?php echo $myvid->id; ?>" onclick = "if (! confirm('Are your sure you want to delete this video? <?php echo $myvid->video_title; ?>')) return false;" class="btn btn-danger">Delete</a>
I saw in a forum people talked about DB::expr
to achive this but can find any example about it.
Could please someone give me a hint?
Here is some code that should get you going:
$video = Model_Video::find($id);
if ( ! $video instanceof Model_Video)
{
Session::set_flash('fail', 'The video ID is not valid!');
Response::redirect('go/somewhere/else');
}
You should always check if the result is in fact a valid Model_Video before using it.
链接地址: http://www.djcxy.com/p/64808.html