CodeIgniter Forums
model's constructor with parameters - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Model-View-Controller (https://forum.codeigniter.com/forumdisplay.php?fid=10)
+--- Thread: model's constructor with parameters (/showthread.php?tid=61261)

Pages: 1 2


model's constructor with parameters - casa - 04-05-2015

Hello,
I want to create a model which extends CI_MODEL and in my constructor, iwant to pass some parameters.
Exemple :
class Test extends CI_Model
{
private $items ;

public function __construct( $params)
{
parent::__construct();
$this->items = $params ;
}
public function getItems() { return $this->items ; }
}

When i load my model in my controler or everywhere else i do this :
$this->load->model('Test');
$r = new Test('toto') ;

i have this message : "Missing argument 1 for Test::__construct()".
i had try this :
class Test extends CI_Model
{
private $items ;

public function __construct( $params = '')
{
parent::__construct();
$this->items = $params ;
}
public function getItems() { return $this->items ; }
}
and then when i do it :
$this->load->model('Test');
$r = new Test('toto') ;
echo $r->getItems () ; // 'toto'
No problem Smile
Is my method is the right way to practice with CodeIgniter ?
Thanks for your answers Smile


RE: model's constructor with parameters - gadelat - 04-05-2015

You are not supposed to use it this way. It reports error because $this->load->model automatically instantiates the model for you into $this->test, you are not supposed to do it yourself. Leave the constructor alone and put that logic into separate method (or access property directly).


RE: model's constructor with parameters - casa - 04-05-2015

(04-05-2015, 03:23 PM)gadelat Wrote: You are not supposed to use it this way. It reports error because $this->load->model automatically instantiates the model for you into $this->test, you are not supposed to do it yourself. Leave the constructor alone and put that logic into separate method (or access property directly).

OK. but explain why exactly ? and why is not good ? What is the problem with this method ? I inherit a class and I can normally redefine all its methods if necessary.
How i do to make constructors with parameters ? Give an example. I teach in engineering school and say no and not explain why, is not educational.
In POO, it's important to have the possibility to make constructors with parameters.


RE: model's constructor with parameters - gadelat - 04-05-2015

I did explain why. $this->load->model included AND instantiated your model, that's why it gives you error. If you don't want it to do that, don't use $this->load->model


RE: model's constructor with parameters - s4if - 04-06-2015

(04-05-2015, 09:01 PM)casa Wrote:
(04-05-2015, 03:23 PM)gadelat Wrote: You are not supposed to use it this way. It reports error because $this->load->model automatically instantiates the model for you into $this->test, you are not supposed to do it yourself. Leave the constructor alone and put that logic into separate method (or access property directly).

OK. but explain why exactly ? and why is not good ? What is the problem with this method ? I inherit a class and I can normally redefine all its methods if necessary.
How i do to make constructors with parameters ? Give an example. I teach in engineering school and say no and not explain why, is not educational.
In POO, it's important to have the possibility to make constructors with parameters.

it is because if you use $this->load->model, codeigniter will instantiate your model automatically "Without" giving any "Parameter" to it. so, it gives you errors..
if you use PDO in Codeigniter, you can call it with $this->db->[your function], because codeigniter wrap PDO in it's database class..
[CMIIW]


RE: model's constructor with parameters - casa - 04-06-2015

thanks.
i suppose an example like this :
PHP Code:
// ______________________
//   class
class Test extends CI_Model
{
  
// example of attributes
  
private $items ;
  private 
$key ;

  
// base constructor
  
public function __construct()
  {
    
parent::__construct();
    
$this->key $this->session->userdata('key_value') ;
  }
  
  
// my personnal "constructor"
  
public function make($params)
  {
     
$this->items $params ;
  }
   
// getter and setter
  
public function getItems() { return $this->items ; }

// end of class

//_________________________
// in controller or view
  
$this->load->model('Test') ; // if model is not loaded
  
$params 'titi' ;
  
$r = new Test() ; // or $r = new Test ;
  
$r->make($params) ; 
Have a good day.


RE: model's constructor with parameters - Athov - 04-06-2015

why not do
PHP Code:
class Test extends CI_Model
{
 
  private $items ;

 
  public function setItems($params '')
 
  {
 
     $this->items $params;
 
  }
 
  public function getItems()
   {
      return 
$this->items 
   }


and then
PHP Code:
$this->load->model('test');
$this->test->setItems('test'); 

as for db items
PHP Code:
class Test extends CI_Model
{
 
  private $items ;

 
  public function __construct()
 
  {
 
     parent::__construct();
 
     $this->items $this->db->get('items');
 
  }
 
  public function getItems()
   {
      return 
$this->items // you can return the items directly from here
                            // by "return $this->db->get('items');" and remove $this->items and __construct()
   
}




RE: model's constructor with parameters - casa - 04-06-2015

Note that your method launch an error when i test it :"Undefined property: CI_Loader:Test" and " Call to a member function setItems() on a non-object"
PHP Code:
$this->load->model('test');
$this->test->setItems('test');  // launch error 

Then, if i want to complete an array of object Test and manipulate datas of many ways, i need to do that (for example):
PHP Code:
$this->load->model('Test');

$tab_exit = array() ;
// loop to complete a table for any treatments
for($i=$i<10 $i++)
{
    
$params 'titi_'.$i ;
    
$r = new Test ;
    
$r->setItems($params) ; // or $r->make($params) switch model before
    
$tab_exit[] = $r ;
}

var_dump($tab_exit) ; // ok

//____
// otherwise if you do that, indeed $this->test is like a singleton, an error appears
for($i=$i<10 $i++)
{
    
$params 'titi_'.$id 
    
$tab_exit[] = $this->test->setItems($params) ; // not possible 
}
// this loop launch an error "Undefined property: CI_Loader:Test" and "Call to a member function setItems() on a non-object" 
//____ 



RE: model's constructor with parameters - Athov - 04-06-2015

(04-06-2015, 02:23 AM)casa Wrote: Note that your method launch an error when i test it :"Undefined property: CI_Loader:Test" and " Call to a member function setItems() on a non-object"

PHP Code:
$this->load->model('test');
$this->test->setItems('test');  // launch error 

Then, if i want to complete an array of object Test and manipulate datas of many ways, i need to do that (for example):

PHP Code:
$this->load->model('Test');

$tab_exit = array() ;
// loop to complete a table for any treatments
for($i=$i<10 $i++)
{
 
   $params 'titi_'.$i ;
 
   $r = new Test ;
 
   $r->setItems($params) ; // or $r->make($params) switch model before
 
   $tab_exit[] = $r ;
}

var_dump($tab_exit) ; // ok

//____
// otherwise if you do that, indeed $this->test is like a singleton, an error appears
for($i=$i<10 $i++)
{
 
   $params 'titi_'.$id 
 
   $tab_exit[] = $this->test->setItems($params) ; // not possible 
}
// this loop launch an error "Undefined property: CI_Loader:Test" and "Call to a member function setItems() on a non-object" 
//____ 

of course it will lunch an error I used your model name but a CI model needs "_model" in his name
like
class Test_model extends CI_Model{}
file name: Test_model.php
and when you loaded $this->load->model('test');
you will access it by $this->test_model

and if you want to name it something else use $this->load->model('test', 'new_model_name');
and you will access it by $this->new_model_name

and you dont need to use $r=new Test();
$this->test_model has it by default

best example: http://www.codeigniter.com/userguide3/general/models.html


RE: model's constructor with parameters - casa - 04-06-2015

No need "_model" in the name.
When you create a model, you name the php file like the name model. The loader.php of the core search the file as the model name.
Example : test.php and inside, you create class Test extends CI_MODEL
PHP Code:
class Test extends CI_MODEL
{
    private 
$items ;

    public function 
__construct() { parent::__construct() ;}

    public function 
setItems($param) { $this->items $param ; }
    
    public function 
find($id)
    {   
        
$tab =  array() ;
        
$query $this->db->get_where(..);
        foreach(
$query->result() as $row)
        {
           
$r = new Test ;
           
$r->setItems($row->id) ; // for example 
           
$tab[] = $r ;
        }
        return 
$tab ;
    }
   
    public function 
getBy()
    {
       
$tab =  array() ;
       for(
$i=0;$i<10;$i++)
       {  
          
$r = new Test ;
          
$r->setItems($i) ;
          
$tab[] = $r ;  // will work but

          // $tab = $this->setItems($i) ; // doesn't work, because error. It's like in JAVA.
       
}
       return 
$tab ;
    }


You can't set parameters like you do that is to say :
PHP Code:
$this->load->model('Test') ;
$this->Test->setItems('toto'); // launch error 

// with
public function setItems($params) { $this->items $params ; } 
With this way, you can only use method who work with information in your database
PHP Code:
$this->load->model('Test') ;
 
$array $this->Test->find(5) ; // will work  or $r = new Test ; $array = $r->find(5) ; //works too.
 
$new_array $this->Test->getBy() ; // works too but inside you need to instace $r = new Test at every iteration in the loop like i do. 
You can't manipulate attributes of your model like i say before, that is to say :
PHP Code:
// example
for($i=$i<10 $i++)
{
    
$params 'titi_'.$i ;
    
$this->test->setItems($params) ; // launch error at the first iteration
    
$tab_exit[] = $r ;

i think you mix everything or we are not on the same way.
With your method you can't do what i describe with function setItems. That never works.