Welcome Guest, Not a member yet? Register   Sign In
autoload model works, load->model not work
#1

[eluser]josluimg[/eluser]
Hi, if I load a model with autoload it works, but If I load the model with load->model in the controller it doesn't work.

My code:
autoload.php
Code:
$autoload['model'] = array('User');

user.php
Code:
class User extends CI_Model  
                         ......
  function __construct(){
   parent::__construct();
  }

userfull.php
Code:
class Userfull extends User
                         ......
  function __construct(){
   parent::__construct();
  }

               public function getFullMedicalProfile($id) ....

Then in a controller
Code:
class Mobile extends REST_Controller
{

private function profile_view($id)
{
  //Obtener el usuario
  $this->load->model('Userfull');
  $user = $this->Userfull->getFullMedicalProfile($id);
....

I get the next error: Fatal error: Call to undefined method stdClass::getFullMedicalProfile() in ...

The model doesn't load well, look at "stdClass", but if I put in autoload.php
Code:
$autoload['model'] = array('User','Userfull');

It works... I don't understand, what's wrong? :S

Thanks community


#2

[eluser]CroNiX[/eluser]
You can't have models extending models (except base CI_Model) without altering CI, adding a SPL autoloader or manually loading all models that extend other models. The reason it works in your autoload example is because you are explicitly loading both models individually. If you just load the "Userfull" model, it doesn't know it needs to load the "User" model first unless you manually tell it.

This would probably work in your controller:
Code:
$this->load->model(array('User', 'Userfull'));
#3

[eluser]hot_sauce[/eluser]
Code:
// File name must be:
userfull_model.php
// Model:
class Userfull extends CI_Controller {
     function getFullMedicalProfile($id)
     {
         return $id++;
     }
}
// load model
$this->load->model('userfull');
// load function
$user = $this->userfull->getFullMedicalProfile($id);
#4

[eluser]josluimg[/eluser]
Thanks for your responses, but I think the problem is more trivial but I don't know which:

Code:
class Vip_relation extends CI_Model {

public $id_user;  //Usuario
public $id_user_vip; //Usuario VIP al que id_user permite ver su perfil

function add()
{
  $this->db->insert('user_vip', $this);
}
}

class Vip extends REST_Controller{

private function addvip($id)
{

  $this->load->model("Vip_relation");
  $this->Vip_relation->id_user_vip = $id;
  $this->Vip_relation->add();  //FAIL ---
  
  $respuesta = new RestResponse();
  $this->response($respuesta->responseOK(), 200);
}
}

Fatal error: Call to undefined method stdClass::add()

What's wrong? I'm so newbie...
#5

[eluser]hot_sauce[/eluser]
Code:
class Vip_relation extends CI_Model {

function add($id_user, $id_user_vip = 0)
{
$params = array(
  'id_user'=>$id_user,
  'id_user_vip'=>$id_user_vip,
);
  $this->db->insert('user_vip', $params);
}
}

class Vip extends REST_Controller{

private function addvip($id)
{

  $this->load->model("vip_relation");
  $this->vip_relation->add($id);
  
  $respuesta = new RestResponse();
  $this->response($respuesta->responseOK(), 200);
}
}
#6

[eluser]josluimg[/eluser]
Thanks hot_sauce, but I get:

Fatal error: : Call to a member function add() on a non-object in

the same error.

Really, my code is:

Code:
$this->load->model("Vip_relation",);
  $this->Vip_relation->id_user = $this->autentication_username;   //Here, PHP create Vip_relation like a standar class
  $this->Vip_relation->id_user_vip = $id;
  $this->Vip_relation->add();
#7

[eluser]josluimg[/eluser]
I found a solution but I don't understand it:

By stackoverflow

Quote:When loading a model within a model, you need to get an instance of code igniter (instead of $this):

$CI =& get_instance();
$CI->load->model('Question_model');
$CI->Question_model->initialize($active_question);
$CI->Question_model->get_answers_list();

Can anyone explain me? Because I'm loading a model in a Controller. Thanks
#8

[eluser]hot_sauce[/eluser]
Code:
/*  Model Filename: vip_relation_model.php  */
class Vip_relation extends CI_Model{
   function add(){
   ....
   }
}

// Controller:
$this->load->model('vip_relation_model');
$this->vip_relation->add($id);
#9

[eluser]InsiteFX[/eluser]
Look at your class name then look at the model name your loading, there both the same.
#10

[eluser]josluimg[/eluser]
[quote author="InsiteFX" date="1403683188"]Look at your class name then look at the model name your loading, there both the same.
[/quote] Are the same with right sintax (first letter capitalized, etc...). The calls name, model name or file name aren't the problem, besides I've try tried all combinations, bcz I went crazy. The solution I found is http://ellislab.com/forums/viewthread/245665/#1066626




Theme © iAndrew 2016 - Forum software by © MyBB