Welcome Guest, Not a member yet? Register   Sign In
model's constructor with parameters
#1

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
Reply
#2

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).
Reply
#3

(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.
Reply
#4

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
Reply
#5

(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]
Reply
#6

(This post was last modified: 04-06-2015, 01:59 AM by casa.)

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.
Reply
#7

(This post was last modified: 04-06-2015, 02:12 AM by Athov. Edit Reason: Adding items from the Database )

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()
   
}

Reply
#8

(This post was last modified: 04-06-2015, 02:46 AM by casa.)

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" 
//____ 
Reply
#9

(This post was last modified: 04-06-2015, 04:38 AM by Athov. Edit Reason: Adding link to CI Docs )

(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/ge...odels.html
Reply
#10

(This post was last modified: 04-06-2015, 05:19 AM by casa.)

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.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB