Blog Details

Models


                                
                                    <h2>* What is a Model ?:-</h2>
<ul>
	<li>Models are <strong>optionally</strong> available for those who want to use a more traditional MVC approach.</li>
	<li>Models are PHP classes that are designed to work with information in your database.</li>
</ul>
&nbsp;
<h2>Structure of a Model :-</h2>
<h2></h2>
class <var>Model_name</var> extends CI_Model {

function <var>__construct</var>()
{
parent::__construct();
}
}
<h3><strong>Note:</strong></h3>
<ul>
	<li>Class names <strong>must start</strong> with an <strong>uppercase letter</strong>. In other words, this is valid:</li>
</ul>
&nbsp;
<h2>Loading a Model:</h2>
<ul>
	<li>Your models will typically be loaded and called from within your Controller functions. To load a model you will use the following function:</li>
</ul>
<ul>
	<li><strong>$this-&gt;load-&gt;model('<var>Model_name</var>');</strong></li>
	<li><strong>$this-&gt;<var>Model_name</var>-&gt;function();</strong></li>
</ul>
<ul>
	<li>If you would like your model assigned to a <strong>different object name</strong> you can specify it via the <strong>second parameter</strong> of the loading function:</li>
</ul>
<ul>
	<li><strong>$this-&gt;load-&gt;model('<var>Model_name</var>', '<kbd>fubar</kbd>');</strong></li>
	<li><strong>$this-&gt;<kbd>fubar</kbd>-&gt;function();</strong>
<strong></strong></li>
</ul>
&nbsp;
<h2><strong> Example:</strong></h2>
<h2>Step 1 :</h2>
<ul>
	<li>Let's create a simple Model so you can see it in action. Using your text editor, create a file called <strong>blogmodel .php</strong> , and put the following code in it:</li>
</ul>
class Blogmodel extends CI_Model {

var $title   = '';
var $content = '';
var $date    = '';

function __construct()
{
// Call the Model constructor
parent::__construct();
}

function get_last_ten_entries()
{
$query = $this-&gt;db-&gt;get('entries', 10);
return $query-&gt;result();
}

function insert_entry()
{
$this-&gt;title   = $_POST['title']; // please read the below note
$this-&gt;content = $_POST['content'];
$this-&gt;date    = time();

$this-&gt;db-&gt;insert('entries', $this);
}

function update_entry()
{
$this-&gt;title   = $_POST['title'];
$this-&gt;content = $_POST['content'];
$this-&gt;date    = time();

$this-&gt;db-&gt;update('entries', $this, array('id' =&gt; $_POST['id']));
}

}
<ul>
	<li>Then save the file <strong>blogmodel.php</strong>  to your <strong><dfn>application/models/</dfn></strong> folder.</li>
</ul>
&nbsp;
<h2>Step 2 :</h2>
<ul>
	<li>Let's create a simple controller so you can see it in action. Using your text editor, create a file called <strong>blog_controller.php</strong> , and put the following code in it:</li>
</ul>
class Blog_controller extends CI_Controller {

function blog()
{
$this-&gt;load-&gt;model('blogmodel');

$data['query'] = $this-&gt;blogmodel-&gt;get_last_ten_entries();

$this-&gt;load-&gt;view('blog', $data);
}
}
<ul>
	<li>Then save the file <strong>blog_controller.php</strong> to your <strong><dfn>application/controller/</dfn></strong> folder.</li>
</ul>
&nbsp;
<h4></h4>
                                    


Categories

CodeIgnitor

Latest posts

13.05.14
Implementing CRUD Operations ¶

<ul> <li>Now comes the fun part. We would like t

13.05.14
View

<ol> <li><a href="http://www.yiiframework.com/do

13.05.14
Creating Model

<ol> <li><a href="http://www.yiiframework.com/do

Get Free Quote