Part of the EllisLab Network
   
 
pass variables into model constructor
Posted: 29 January 2007 11:18 PM   [ Ignore ]  
Summer Student
Total Posts:  16
Joined  01-10-2007

How can i pass variables into a constructor of a model

ie.

class Car extends Model
{
  public Car($params)
  {
      echo $params…..
..
. .
.   }

}

Controller{

$this->load->model(‘car’, $params);

}

I have not found a way to this yet.  I have to hack around it.

Profile
 
 
Posted: 30 January 2007 12:06 AM   [ Ignore ]   [ # 1 ]  
Administrator
Avatar
RankRankRankRankRankRank
Total Posts:  6762
Joined  03-23-2006

When you actually call the model function, that’s when.

// in the controller
$this->load->model('car');

echo
$this->car->get_year($car_id);

edit: Darn, reading Crafter’s response makes me realize I completely blew you question.  Sorry, I misunderstood, just ignore the above post wink

 Signature 

DerekAllard.com - CodeIgniter, ExpressionEngine, and the World of Web Design
BambooInvoice - Open Source, CodeIgniter powered invoicing.

Profile
MSG
 
 
Posted: 30 January 2007 01:27 AM   [ Ignore ]   [ # 2 ]  
Research Assistant
RankRankRank
Total Posts:  552
Joined  06-17-2006

You can’t.

The CI loader controls the instancing of your model , and makes no allowances for contructor parms.
See system.libraries/Loader.php

function model($model, $name = '', $db_conn = FALSE)
{
  
:
                
$CI->$name = new $model();
  :
}

So your workaround is the only way for now.

PHP does not support strict types, so the point of constructor variable is lost somewhat in any case. It’s just pure convenience that saves you the overhaed of an additional class method invocation that you gain with PHP.

 Signature 

CodeCrafter - Open Source Code Generation for CI

Profile
 
 
   
 
 
‹‹ uh oh...      caching query result ››
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 719, on June 06, 2008 10:16 AM
Total Registered Members: 66430 Total Logged-in Users: 30
Total Topics: 84793 Total Anonymous Users: 5
Total Replies: 455044 Total Guests: 199
Total Posts: 539837    
Members ( View Memberlist )
Newest Members:  Dylan1978X_franbaguasllogocsaturkeyPeter BryanttherendStudioGeorgiaJZeerfedeghe