C

Simplified tutorials to learn the popular basic language C

VB.Net

Learn the basic desktop development application language

CodeIgniter

One of the popular PHP three tier (MVC) framework

How to Send Controller Variables into Model

Passing data from Controller to Model CodeIgniter
As we shown in previous lesson, CodeIgniter Model manage all database operations. If we want to perform a database operation, then that should be via a Model class.




Creating a Model:

  • Open your CodeIgniter Model folder. 
  • Create a PHP file. Give a name, make it without any space. (Here I give ‘projectName’)
  • Now open the file.
  • Now you have to create a class like creating class in Controller. The class name should be same as the name of the Model name. Make the first letter as capital.
Creating Model Class in CodeIgniter

Here I create a Model class with name ProjectName because file name is same.

Now you need to extent a super class CI_Model like we extend CI_Controller in Controller section.

Now the total Model code:

<?php
class ProjectModel extends CI_Model
{ 

}

How to Send data from Controller into Model?

If want to insert a data into database, then that should be transfer into the Model from the controller.
There are many ways to send a data or a value to Model.

Method 1: Using Model Variables:

This is the best way to send a data into the Model class. For this, you have to create enough variables on Model class.

Here I am sending variable username and userage values into the Model.

So we must create corresponding variables on the Model

var $USERNAME = '';
var $USERAGE = 0;

We should use a keyword ‘var’ before the variable name and variablename should contain a doller ($) symbol. CodeIgniter recommends all model variable as capital letters but small letter also works.

You need to assign a default value into the variable. Here the value on variable USERNAME will be a string, so we assign two single quotes (' '). In case of USERAGE, the value on the variable will be numbers, so lets us assign a default value zero (0).

Now the Model code:

<?php
class ProjectModel extends CI_Model
{
  var $USERNAME = '';
  var $USERAGE = 0;
}

Now let us pass our two controller variables ‘username’ and ‘userage’  that we created early.
When we load this controller function ‘userregister’, here we have to pass these two variables' values into the model.

Let us write like this:

$this->load->Model('ProjectModel');
$this->ProjectModel->USERNAME = $name;
$this->ProjectModel->USERAGE = $age;

Explanation:

$this->load->Model('ProjectModel');

  -This line loads the model ‘ProjectModel’ into our project.

$this->ProjectModel->USERNAME = $name;

  - In Model 'ProjectModel', there is a variable named ‘USERNAME’ that we just created, now it get a value of variable ‘name’ on controller.

Note:
  • Do not user $ symbol with model variable while calling ($this->ProjectModel->USERNAME).
  • The value of $name is originated from the user input that was passed from view section. See previous lesson.
Like this,
 $this->ProjectModel->USERAGE = $age;  means the value on the model ‘USERAGE’ get a value of variable $age from controller.

By writing these two codes, model variables get all these values from controller.

Method 2:Using Model Function

If we want to perform a database operation, there should be a function on model.

Creating a Model function:

Creating a function on model is similar to creating function on controller.
Here I create a function named ‘addvalue’ on model:

public function addvalue()
{ 

}

Total Model Code:

<?php
class ProjectModel extends CI_Model
{
  var $USERNAME = '';
  var $USERAGE = 0;

public function addvalue()
 { 

 }
}

Calling a Model Function from Controller:

It is very simple to call a model function from controller, which is similar to assigning a model variable from controller. We have to load the model at first, that we have done in our first method.

$this->load->Model('ProjectModel');
$this->ProjectModel->addvalue();

Passing variables with function as parameters:

We can pass values while calling a model function as function parameters.
While calling function ‘addvalue’, we can add parameters ‘name’ and ‘age’ with this.

   $this->ProjectModel->addvalue($name,$age);

Here we added two parameters with the function. So we should add another two variables on model function to accept these values.

<?php
class ProjectModel extends CI_Model
{
 public function addvalue($name,$age)
 {

 }
}

Note:
  • We can use any variable name on Model, but number of parameters should be equal in both sending and accepting variables.
Now you successfully sent your variables into the model.

Other Methods:

We can send values into model using Cookies, Sessions, via URL, etc. But above two methods are common and best. We will learn other methods later.

Getting and Using Variable Values on Model:

We should a function to perform model activities.
So let’s check our values using the model function ‘addvalue’.

In case of method 1:

Just write

$this->variablename.

Example:

$this->USERNAME

Note:
  • We normally do not print variable on Model section. This is just a test to check whether the variable has a life in model section.
Total Model Code:


<?php
class ProjectModel extends CI_Model
{
  var $USERNAME = '';
  var $USERAGE = 0; 

 public function addvalue()
 { 
  echo $this->USERNAME;
 } 
}

Total data work flow:

CodeIgniter data flow in Controller and Model

Sample Output:


Sending values from Controller into Model in CodeIgniter

In case of Method 2:

Here we send data using parameters. So just using the parameter variable on model is enough to get its value.

<?php
class ProjectModel extends CI_Model
{
 public function addvalue($name,$age)
 {
   echo $name;
 }
}

Data flow in second method:
CodeIgniter Calling a Model function from controller

We will learn more examples later

No comments:

Post a Comment