Welcome Guest, Not a member yet? Register   Sign In
Passing Arguments problems
#1

[eluser]Richard Testani[/eluser]
I'm getting a range of errors trying to pass arguments to models and libraries.

Code:
//controller/index.php
class Index extends Controller {
public function __construct() {
  parent::Controller();
  $data['arg'] = 'yes';
  $this->load->model('Bsn', $data);
}
}
//models/bsn.php
class Bsn extends Model {
public function __construct($data) {
   parent::Model();
}
}

The above return 4 errors:
Code:
A PHP Error was encountered

Severity: Notice

Message: Array to string conversion

Filename: libraries/Loader.php

Line Number: 155

------------------------------

A PHP Error was encountered

Severity: Warning

Message: Missing argument 1 for Bsn::__construct(), called in /home/bobbysue/www/dev/system/libraries/Loader.php on line 184 and defined

Filename: models/bsn.php

Line Number: 7

------------------------------

A PHP Error was encountered

Severity: Notice

Message: Array to string conversion

Filename: libraries/Loader.php

Line Number: 184

------------------------------

A PHP Error was encountered

Severity: Notice

Message: Array to string conversion

Filename: libraries/Loader.php

Line Number: 185

What is happening?
I am my argument as an array.
I've also written it out as:
Code:
$data = array('yes');
with same error.

Any ideas what I am doing wrong or what is actually happening?
Thanks
Rich
#2

[eluser]jedd[/eluser]
[quote author="Richard Testani" date="1249435817"]
Code:
//controller/index.php
...
  $this->load->model('Bsn', $data);
[/quote]

It is meaningless to pass data to a model class per se - you send data to a method within a model.

The second parameter of a model load denotes an alternative name for the model (see the [url="http://ellislab.com/codeigniter/user-guide/general/models.html"]CI User Guide section on Models[/url] for more information) which explains the array to string conversion error.
#3

[eluser]ecigraeme[/eluser]
You need to define a function in your model, call that function and pass a parameter to it.

Code:
//controller/index.php
class Index extends Controller
{
    public function __construct()
    {
        parent::Controller();
        $data['arg'] = 'yes';
        $this->load->model('Bsn', $data);
        $this->Bsn->some_function($data);
    }
}

//models/bsn.php
class Bsn extends Model
{
    public function __construct($data)
    {
       parent::Model();
    }

    public function some_function($data)
    {
       // do something
    }
}
#4

[eluser]Johan André[/eluser]
The second argument of the load->model()-function is for creating a new name for the model:

Code:
$this->load->model('categories_model', 'cats');

$all_results = $this->cats->get_all();

You can't (afaik) pass arguments to a model constructor...
#5

[eluser]jcavard[/eluser]
[quote author="Richard Testani" date="1249435817"]I'm getting a range of errors trying to pass arguments to models and libraries.

Code:
A PHP Error was encountered
Severity: Notice
Message: Array to string conversion
Filename: libraries/Loader.php
Line Number: 155
This is because the argument CI is expecting is a string (see here) and you pass an array ($data)

Code:
A PHP Error was encountered
Severity: Warning
Message: Missing argument 1 for Bsn::__construct(), called in /home/bobbysue/www/dev/system/libraries/Loader.php on line 184 and defined
Filename: models/bsn.php
Line Number: 7
This is because the model constructor expect a parameter which is missing. You should not have a parameter in the model constructor.

Code:
A PHP Error was encountered
Severity: Notice
Message: Array to string conversion
Filename: libraries/Loader.php
Line Number: 184
Because it's trying to convert the $data array to a string...

Code:
A PHP Error was encountered
Severity: Notice
Message: Array to string conversion
Filename: libraries/Loader.php
Line Number: 185
same thing


The second argument passed when you load a model is the internal name of that model, example taken from the user guide
you can do this way
Code:
$this->load->model('Model_name');
$this->Model_name->function();
or this way
Code:
$this->load->model('Model_name', 'MyFunkyModelNameCool');
$this->MyFunkyModelNameCool->function();
You cannot pass argument to a model that way.


You have a few things wrong in your code. Try this first
Code:
//controller/index.php
class Index extends Controller {
public function Index()
{
  parent::Controller();

  $this->load->model('Bsn');
}
}
//models/bsn.php
class Bsn extends Model {
public function Bsn()
{
   parent::Model();
}
}
#6

[eluser]Adam Griffiths[/eluser]
Index is also a reserved name in CodeIgniter for controllers. So you'll have to rename your controller, that's probably causing you some problems.
#7

[eluser]Richard Testani[/eluser]
Wow - great help on this one.
I had not realized, the special case of the model.

I've renamed my Controller from Index which I found out in another case that it's a reserved name.
Thanks everyone.
Rich




Theme © iAndrew 2016 - Forum software by © MyBB