2

I am getting such a warning:

A PHP ERROR WAS ENCOUNTERED
Severity: Warning
Message: Missing argument 1 for User_model::__construct(), called in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\CI_PP\system\core\Loader.php on line 303 and defined
Filename: models/user_model.php
Line Number: 20

while running app in CodeIgniter, NetBeans, PHP 5.4.

This is the code of models/user_model.php(playing around, ignore avoiding OOP principles, please):

<?php

  class User_model extends MY_Model {

    public $_table = 'pp_user';
    public $primary_key = 'u_id';
    public $firstname;
    public $lastname;
    public $emailAddress;
    public $password;
    public $gender;
    public $deliveryAddress;
    public $address;
    public $city;
    public $zip;
    public $country;
    public $isAdmin;

//this is line nr.20:
    public function __construct($firstname, $lastname, $emailAddress, $password, $gender, $address, $deliveryAddress, $city, $zip, $country, $isAdmin) {
        parent::__construct();

        $this->firstname = $firstname;
        $this->lastname = $lastname;
        $this->emailAddress = $emailAddress;
        $this->password = $password; //TODO!
        if ($gender == 'male') {
            $this->gender = 0;
        } else if ($gender == 'female') {
            $this->gender = 1;
        } else {
            $this->gender = -1;
        }
        $this->deliveryAddress = $deliveryAddress;
        $this->address = $address;
        $this->city = $city;
        $this->zip = $zip;
        $this->country = $country;
        $this->isAdmin = $isAdmin;
    }
}

I am calling constructor in registration.php(controller):

            $firstname = $this->input->post('tf_first_name');
        $lastname = $this->input->post('tf_last_name');
        $emailAddress = $this->input->post('tf_email_address');
        $password = $this->input->post('tf_password_base');
        $gender = $this->input->post('tf_gender');
        $address = $this->input->post('tf_address');
        $deliveryAddress = $this->input->post('tf_delivery_addres');
        $city = $this->input->post('tf_city');
        $zip = $this->input->post('tf_zip');
        $country = $this->input->post('tf_country');
        $isAdmin = FALSE;


        $user_instance = new User_model(
                        $firstname,
                        $lastname,
                        $emailAddress,
                        $password,
                        $gender,
                        $address,
                        $deliveryAddress,
                        $city,
                        $zip,
                        $country,
                        $isAdmin);

If I change contor arguments from:

public function __construct($firstname, $lastname, $emailAddress, $password, $gender, $address, $deliveryAddress, $city, $zip, $country, $isAdmin) 

to:

public function __construct($firstname="", $lastname="", ...) {

then it works, but I do not like such a solution. I have been searching for a hints all over the web but according to PHP OOP tutorials and PHP manual it looks ok.

Loader.php on line 303 is doing this:

$CI->$name = new $model();

I tried to changed parameters to direct ones when instantiating object, I tried to remove relation to parent class but the problem remains the same.

I am really curious what might be the problem, any idea?

1
  • Loading model is not the same that loading libraries. I don't think CodeIgniter expects arguments when loading a model. Forget this ! I didn't see you call "new User_model" sry ! Commented Mar 27, 2014 at 12:58

2 Answers 2

1

This is happening because CodeIgniter is calling User_model::__construct() when the application is loaded. CodeIgniter has to load all models before you can use them which means you can't, or rather shouldn't, pass parameters to them. You need to move that code from the __construct to another function like add_user within the User_model class that you can pass the data into.

<?php

class User_model extends MY_Model {

    public $_table = 'pp_user';
    public $primary_key = 'u_id';
    public $firstname;
    public $lastname;
    public $emailAddress;
    public $password;
    public $gender;
    public $deliveryAddress;
    public $address;
    public $city;
    public $zip;
    public $country;
    public $isAdmin;

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

    public function add($firstname, $lastname, $emailAddress, $password, $gender, $address, $deliveryAddress, $city, $zip, $country, $isAdmin) {
        $this->firstname = $firstname;
        $this->lastname = $lastname;
        $this->emailAddress = $emailAddress;
        $this->password = $password; //TODO!
        if ($gender == 'male') {
            $this->gender = 0;
        } else if ($gender == 'female') {
            $this->gender = 1;
        } else {
            $this->gender = -1;
        }
        $this->deliveryAddress = $deliveryAddress;
        $this->address = $address;
        $this->city = $city;
        $this->zip = $zip;
        $this->country = $country;
        $this->isAdmin = $isAdmin;
    }
}

Then call the model like this.

$firstname = $this->input->post('tf_first_name');
$lastname = $this->input->post('tf_last_name');
$emailAddress = $this->input->post('tf_email_address');
$password = $this->input->post('tf_password_base');
$gender = $this->input->post('tf_gender');
$address = $this->input->post('tf_address');
$deliveryAddress = $this->input->post('tf_delivery_addres');
$city = $this->input->post('tf_city');
$zip = $this->input->post('tf_zip');
$country = $this->input->post('tf_country');
$isAdmin = FALSE;

$this->load->model('User_model');
$this->User_model->add($firstname,
                $lastname,
                $emailAddress,
                $password,
                $gender,
                $address,
                $deliveryAddress,
                $city,
                $zip,
                $country,
                $isAdmin);
Sign up to request clarification or add additional context in comments.

4 Comments

You should also look at the CodeIgniter Docs for how they load models. ellislab.com/codeigniter/user-guide/general/models.html
Thank you for an explanation. I tried to adjust the code and it works now.
Yes, I see in documentation that they use: $this->load->model('Model_name'); $this->Model_name->function(); But used to other OOP languages I expected (and also read in PHP manual) that it should work like shown earlier. After explanations I understood I have to stick to another way. I am also using CRUD operations wrapper for CI, so I would just make it more confused if I added this info here.
You can do anything you like in the model, but CodeIgniter prefers to do the class loading for you. I had the same mindset as you when I first used CI, but it's a great framework.
1

you have to do this

public function __construct($firstname="", $lastname="", ...) {

else you have pass the arguments when you create the object of the class because when you create the object of class the construct method execute that time so you have pass all argument value during the creating object of class or you have to use above method

1 Comment

I mentioned it worked but I did not like it. Now I understand why it works as it works, so both solutions fix the issue out. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.