I get the error
Fatal error: Call to a member function retrieve_products() on a non-object
The controller is:
<?php
class Cart extends CI_Controller { // Our Cart class extends the Controller class
public function _construct()
{
parent::_construct(); // We define the the Controller class is the parent.
$this->load->model('Cart_model'); // Load our cart model for our entire class
}
function index()
{
$data['products'] = $this->cart_model->retrieve_products(); // Retrieve an array with all products
}
}
The model is:
<?php
class Cart_model extends CI_Model {
function retrieve_products(){
$query = $this->db->get('products'); // Select the table products
return $query->result_array(); // Return the results in a array.
}
}
I want to say that your call
$data['products'] = $this->cart_model->retrieve_products();
Should be:
$data['products'] = $this->Cart_model->retrieve_products();
Ie: uppercase "C" in cart_model
Maybe we're using different versions (I have 1.7.2), but to declare a model, CI_ does not appear. My working code has the equivalent of:
class Cart_model extends Model
Also, the class should capitalized:
$this->Cart_model->retrieve_products();
(instead of)
$this->cart_model->retrieve_products();
I think its your typo error you have spelled construct function as _construct rather than __construct thats why codeigniter considers it as a function rather than a class constructor and model loading is limited to only that function.
Related
In CodeIgniter 3.1.3 I extended CI_Model in application/core/MY_Model.php:
class MY_Model extends CI_Model {
public function __construct($id = NULL) {
parent::__construct();
$this->load($id);
}
public function load($id) {
$this->db->where('id', $id);
$this->db->limit(1);
$query = $this->db->query($this->_table);
if ($row = $query->result()) {
// #todo Process results
}
// Free the resources.
$query->free_result();
}
}
My User_model looks like this:
class User_model extends MY_Model {
public function __construct($id = NULL) {
parent::__construct($id);
}
}
I also extended the CI_Controller in application/core/MY_Controller as follows:
class MY_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('User_model');
}
}
I autoloaded the database connection in application/config/autoload.php as:
$autoload['libraries'] = array('database');
Without loading the User_model in the controller I was able to run migrations, so the database connection is configured correctly. But when I added $this->load->model('User_model') I get the error "Undefined property: User_model::$db".
If I let User_model extend CI_Model it runs without errors and with a var_dump in the homepage's controller it shows that the database is autoloaded correctly. But as soon as I put MY_Model in between, the database class is undefined in the model and also $this->load in the model returns NULL, so it appears the model is not properly constructed.
I can only imagine this to be a very small mistake, but I've been staring at it for hours with several breaks in between and I just don't see it. Can anyone else help me?
From a controller, lets say A_controller
$this->load->library('MyLib');
In MyLib, my original issue is this block
$ci = &get_instance();
$ci->load->model('my_model');
$this->active_model = $ci->my_model;
Then the miracle come when it become
$ci = &get_instance();
$ci->load->model('my_model');
$ci->my_model->db = & $ci->db;
$this->basic_model = $ci->my_model;
When CI loads a model $db property come along with CI.
So, when you call $this->db in model which means you want "my_model" have the property, right?
What I did just link the property from CI to my_model!!!
I made an array in MY_Controller class placed on core folder. In its constructor i fetched records from db so as to make navigation menu in my views. Since i have different page layouts so i cannot call the same header view every where. for this reason i made a core class as per my understanding which i am not sure is right or not. below is the code for my controller
class MY_controller extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('Category_model');
$data['parent'] = $this->Category_model->getParentCategories();
$data['child'] = $this->Category_model->getChildCategories();
}
}
my default controller is main
class Main extends MY_controller {
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->view('home/header',$data);
$this->load->view('home/footer');
}
Now in my header view i am receiving undefined variable parent and child error. I want this two variables available in all the views so that i do not have to define those two variables in every controller.
Thanks
You may try something like this:
class MY_controller extends CI_Controller
{
$commonData = array();
function __construct()
{
parent::__construct();
$this->load->model('Category_model');
$this->commonData['parent'] = $this->Category_model->getParentCategories();
$this->commonData['child'] = $this->Category_model->getChildCategories();
}
}
Then use $this->comonData in your index method instead of $data to pass to the view:
public function index()
{
$this->load->view('home/header', $this->comonData);
$this->load->view('home/footer');
}
Now it'll be available in the header view and since it's at the top of other views then you may use it further, unless you override it with other value in any class.
how to call model method in another model, example
I have code like this
/model/user.php
public function get_token_by_id($id){
//some code
}
i want call in my another model
/model/restaurant
App::bind('user','user');
class RestaurantController extends BaseController {
public function __construct(user $modelUser){
$this->modelUser = $modelUser;
}
public function getUser(){
$someVar = $this->modelUser->get_token_by_id($id);
}
}
But i get an error
Call to a member function get_token_by_id() on a non-object
how to fix it?
Well... that's because $this->modelUser is a non object !
To be more precise, $this->modelUser returns null or something like that (try a var_dump($this->modelUser)). It could be because your model doesn't have the attribute declaration (protected $modelUser) or because you don't pass the right variable into the constructor.
So, I'm getting a fatal error because the method is undefined when the controller calls the method. Though this is not true as the method is inside the classes model.
StudentsController.php
<?php
class StudentsController extends AppController{
public function index(){
$students = $this->Student->find('all');
$this->set('students', $students);
}
public function add(){
if($this->request->is('post')){
$this->formatData($this->request->data);
}
}
}
?>
And then my model:
Student.php (Model)
<?php
class Student extends AppModel{
var $studentData;
public function formatData($studentData){
if(is_null($studentData)){
return false;
}else{
echo $studentData;
}
}
}
?>
You're not invoking the method on the model, but on the controller where there is no such method available, hence the error.
While the controller may automatically load the model, it doesn't expose its API, it just makes an instance of the model available via magic property accessors.
So instead of
$this->formatData($this->request->data);
you have to invoke the method on the model like this:
$this->Student->formatData($this->request->data);
See also
http://book.cakephp.org/2.0/en/controllers.html#Controller::$uses
http://book.cakephp.org/2.0/en/controllers.html#Controller::loadModel
http://book.cakephp.org/2.0/en/models.html#understanding-models
I created a custom module. The call which is failing is:
$collection = Mage::getModel('[modulename]/[model]')->getCollection();
$collection->addFieldToFilter('is_public', 1)->getSelect();
$collection = Mage::getModel('[modulename]/[model]')->getCollection()->getFirstItem();//
Obviously, I screwed something up in my models. Here they are:
Model/[Model].php
class [namespace]_[modulename]_Model_[model] extends Mage_Core_Model_Abstract {
public function _construct() {
parent::_construct();
$this->_init('[modulename]/[model]');
}
}
Model/Mysql4/[Model].php
class [namespace]_[modulename]_Model_Mysql4_[model] extends Mage_Core_Model_Mysql4_Abstract {
public function _construct() {
$this->_init('[modulename]/[model]', 'id');
}
}
Model/Mysql4/[Model]/Collection.php
class [namespace]_[modulename]_Mysql4_[model]_Collection extends
Mage_Core_Model_Mysql4_Collection_Abstract {
protected function _construct() {
//parent::_construct();
$this->_init('[modulename]/[model]');
}
}
The collection works, sort of.
$collection = Mage::getModel('[modulename]/[model]')->getCollection()->getData();
The code above returns all rows in the table. It fails as soon as I try to apply another function to it. From what I've read addFieldToFilter should just work for a properly set up collection.
This is in magento. Thanks for your help!
Check the following:
What is the class of your Object in question and check if this class has getFirstItem() function in place.
Note:
abstract class Mage_Core_Model_Resource_Db_Collection_Abstract extends Varien_Data_Collection_Db does not have getFirstItem()
class Varien_Data_Collection implements IteratorAggregate, Countable
has this function.
So check as stated above what is the class of your Object class in question.
Paste your specific code lines where this error is coming from.