Welcome Guest, Not a member yet? Register   Sign In
Extending CI_Model in applications/core
#1

[eluser]glevine[/eluser]
I posted a topic last night that is similar to this one, but I promise that it is a different scenario. I'm no longer trying to extend the same CI core class multiple times. I'm using a Library to store my non-peer models...

I've written some code that is very similar to the following. Obviously it is very simple, but I think it illustrates the problem I'm having.

Code:
// applications/libraries/MY_Object.php
abstract class MY_Object
{
  protected $id;

  public function getId()
  {
    return $this->id;
  }

  public function setId($id = null)
  {
    $this->id = $id;
  }

  abstract public function toArray();
}

Code:
// applications/libraries/carobject.php
class CarObject extends MY_Object
{
  private $make, $model;

  public function construct($data = array())
  {
    $this->id = $this->setId($data['id']);
    $this->make = $data['make'];
    $this->model = $data['model'];
  }
}

Code:
// applications/core/MY_Peer.php
class MY_Peer extends CI_Model
{
  public function __construct()
  {
    parent::__construct();
    $this->load->database();
  }
}

Code:
// applications/models/carpeer.php
class CarPeer extends MY_Peer
{
  public function getById($id)
  {
    $query = $this->db->get_where('car', array('id' => $id));
    $row = $query->row_array();

$this->load->library('CarObject', $row, 'car1');
    return $this->car1;
  }
}

Code:
// applications/core/MY_Controller.php
class MY_Controller extends CI_Controller
{
  // does some stuff the CI_Controller doesn't do
}

Code:
// applications/controllers/car.php
class Car extends MY_Controller
{
  public function __construct()
  {
    parent::__construct();
    $this->load->model('CarPeer');
  }

  public function view($id)
  {
    $render = array();
    $car = $this->CarPeer->getById($id);
    $data = $car->toArray();
    $this->load->view(car/view, $render['car']);
  }
}

MY_Controller class gets loaded from the applications/core without any problems. However, I have to explicitly include MY_Peer and MY_Object. For example, without...

Code:
require_once APPPATH . 'core/MY_Peer.php';

... in applications/models/carpeer.php and...

Code:
require_once APPPATH . 'libraries/MY_Object.php';

... in applications/libraries/carobject.php, the classes aren't loaded.

I understand that I can't extend the Library class without loading it first and that I can't use $this->load->library('MY_Object') until I'm inside of a class. Another option for dealing with loading the library base class would be to always load that class.

However, what I don't understand is why MY_Controller is loaded without providing an include path, while MY_Peer is not. They are in the same directory and are used in the same manner -- as a base class that extends a CI class (one is a controller and one is a model).

The error I get is...

Quote:Fatal error: Class 'MY_Peer' not found in /var/www/application/models/carpeer.php on line 5

... when I comment out require_once statement in my CarPeer class.

Why don't I see that same error when MY_Controller is extended by Car in the above code? What is different between CI_Controller and CI_Model?

Thanks!
#2

[eluser]CroNiX[/eluser]
You only use MY_ when extending a native CI core class. There is no CI core class named Peer or Object, so it won't work. I'd recommend thoroughly reading the user guide.
http://ellislab.com/codeigniter/user-gui...asses.html

Reading through the code to see how CI works under the hood is very helpful as well. "Understand the ground you're standing on before trying to build that house" as they say Smile
#3

[eluser]glevine[/eluser]
Well thank you for pointing me in the right direction. I finally found that the load_class() function in core/Common.php implements this constraint.

IMO, the documentation isn't exactly clear that the name of the class has to be identical to the one you are extending. The example suggests that *may be* the case, but nowhere does it say that's a requirement. I feel the author(s) left room for doubt.




Theme © iAndrew 2016 - Forum software by © MyBB