Thursday, February 4, 2016

as_object()

CONTROLLER
===========
<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Posts extends Controller  {

public function action_index()
{
$posts=Model::factory('post')->getPosts();
$data['title']="GP Verification";
$this->response->body(View::factory('posts')->bind('posts',$posts));
}
}

MODEL
======
<?php defined('SYSPATH') or die('No Direct Access');

class Model_Post extends Model
{
public function getPosts()
{
$result=DB::select()->from('posts')->as_object()->execute();
//if you do not use as_object() , you will have to use array format in VIEW
return $result ;
}




VIEW
====
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<table>
<?php foreach($posts as $post): ?>
<tr>
<td><?php echo $post->postTitle; ?></td>
//You can use the object format if you use as_object in query;
//$result=DB::select()->from('posts')->as_object()->execute();

<td><?php echo $post->postBody ;?></td>
</tr>
<?php endforeach;?>
</table>
</body>

</html>