How do I do $this->model->getList() in php - php

I am working on i MVC and i've gone through some project to see how works but, when i do something like $this->model->getList() to get a list of items from my DB in my controller i get this error message
Notice: Undefined property: Managemenu::$model in C:\wamp\www\mymvc\Admin\controllers\managemenu.php on line 10
Fatal error: Call to a member function getList() on a non-object in C:\wamp\www\mymvc\Admin\controllers\managemenu.php on line 10
Actually this is my code on my managemenu model
<?php class Managemenu_Model extends Model{
function __construct(){
parent::__construct();
}
public function getList(){
$menus = Menu::find_all();
return $menus;
}
public function edit(){
}
public function update(){
}
public function save(){
}
public function delete(){
}
}?>
and this is what my controller is like
<?php class Managemenu extends Controller{
public $mymenus = null;
function __construct(){
parent::__construct();
$this->index();
}
public function index(){
#$this->loadModel("managemenu");
$this->mymenus = $this->model->getList();
$this->view->render("managemenu/index");
}
public function doList(){
}}?>
Also, I want to get the list of my menus on the front end by looping through the $mymenus into a table if i do foreach($this->mymenus as....) i keep on having the same error in my view template too
can anybody give me a hand. i don't know why that error comes up

Related

Notice: Undefined property: In creating new model using namespace

I am a newbie php coder. I have this main controller:
namespace App\Core;
class Controller
{
/** #var View View The view object */
public $View;
public $templates;
/**
* Construct the (base) controller. This happens when a real controller is constructed, like in
*/
public function __construct()
{
}
public function loadModel() {
$this->model = new \App\Front\Model\IndexModel(); //error line
}
}
In IndexController I have:
namespace App\Front\Controller;
use App\Front\Model\IndexModel;
class IndexController extends \App\Core\Controller {
public function index(){
$this->loadModel->test();
}
}
In IndexModel I have:
namespace App\Front\Model;
class IndexModel
{
public function test(){
echo 'test print';
}
}
In Action I get this error:
Notice: Undefined property: App\Front\Controller\IndexController::$loadModel in /Applications/xampp/htdocs/cmstest/application/Front/Controller/IndexController.php on line 13
I load all classes using composer and PDR-4 method.
What is the problem and how do I fix it? Thanks
Notice that in your loadModel method you just assign the new model to this but you not returning anything -> so you cannot can function test() on null.
In order to fix it use:
class IndexController extends \App\Core\Controller {
public function index(){
$this->loadModel();
$this->model->test();
}
}
If you insist on doing in index only one function you can modify your loadModel function to:
public function loadModel() {
if (!$this->model) // that way you load the Model only once. If you want to reload every time just remove the if
$this->model = new \App\Front\Model\IndexModel();
return $this->model;
}
And then you can do:
public function index(){
$this->loadModel()->test();
}

Unable to locate the specified class: Session.php, When call a function from another controller in Codeigniter

contrller:News.php
This is my controller News
<?php class News extends CI_Controller {
public function __construct()
{
}
public function getShowIN_News()
{
return $result;
} } ?>
contrller:Category.php
This is my controller Category
<?php class Category extends CI_Controller {
public function __construct()
{
}
public function category()
{
require('news.php');
$test = new News();
$data["headlines"] = $test->getShowIN_News();
} }?>
By using an empty constructor, you're making it so that CI_Controller::__construct() isn't called, and that's where everything in the framework is initialized.
I know you've put it there to hack it so you can call one controller from another, but it is very intentionally made that way, exactly so you don't do this.

Getting error in loading model in controller in codeigniter Message: Undefined property: Cart::$load

I am a beginner in codeigniter and currently I am working on a shopping cart, taking help from http://net.tutsplus.com/tutorials/php/how-to-build-a-shopping-cart-using-codeigniter-and-jquery/ tutorial. I am using codeigniter 2.1.3.
I am getting an error:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Cart::$load
Filename: controllers/cart.php
Line Number: 7
Fatal error: Call to a member function model() on a non-object in D:\xampp\htdocs\ci\application\controllers\cart.php on line 7
Can someone please tell me why it is not working?
The name of my controller is cart.php
<?php
class Cart extends CI_Controller {
public function Cart()
{
//parent::CI_Controller(); // We define the the Controller class is the parent.
$this->load->model("cart_model"); // Load our cart model for our entire class
}
public function index()
{
$data['products'] = $this->cart_model->retrieve_products(); // Retrieve an array with all products
print_r($data['products']);
//$data['content'] = 'cart/products'; // Select view to display
//$this->load->view('index', $data); // Display the page
}
}
?>
and my model is cart_model.php
<?php
class Cart_model extends CI_Model{
//public function _construct(){
//parent::_construct();
//}
public function retive_products(){
$query = $this->db->get("products");
return $query->result_array();
}
}
/* End of file cart_model.php */
/* Location: ./application/models/cart_model.php */
?>
Codeigniter 2.1.3 is intended to support PHP 5.2.4 and newer.
Change the class constructor:
<?php
class Cart extends CI_Controller {
public function __construct()
{
parent::__construct();
}
instead of
public function cart()
{
parent::CI_Controller();
Try to review your code. You calling the retrieve_products function on model but in model you have retive_products function .
Controller
public function index() {
$data['products'] = $this->cart_model->retrieve_products(); // Retrieve an array with all products
print_r($data['products']);
//$data['content'] = 'cart/products'; // Select view to display
//$this->load->view('index', $data); // Display the page
}
Model
public function retive_products() {
$query = $this->db->get("products");
return $query->result_array();
}

why I'm getting function get() on a non-object in error when using codeigniter?

index_model.php is below:
<?php
class index_model extends CI_Model {
function __construct() {
parent::__construct();
}
function getVideo()
{
$query = $this->db->get('videolar');
return $query->result_array();
}
}
?>
And index controller is below, too
<?php
class index extends CI_Controller {
function __construct() {
parent::__construct();
}
function index()
{
$this->load->model('index_model');
$data['video'] = $this->index_model->getVideo();
$this->load->view('index',$data);
}
}
?>
When I call index controller it returns this error
Fatal error: Call to a member function get() on a non-object in
/var/www/atlet/application/models/index_model.php on line 10
I set database in autoload.php.
$autoload['packages'] = array('database');
You need to load the database in the libraries array, not the packages array:
$autoload['libraries'] = array('database');

Controller doesn't load, no errors from Apache or CodeIgniter

I'm sure this is a simple mistake on my part. I'm trying to demo ci to a friend and we created a simple controller that looks like this:
<?php
class Customerlookup extends CI_Controller {
//constructor for this class
public function __construct()
{
parent::__construct();
$this->load->model('customerlookup_model');
}
public function index()
{
echo 'test';
}
public function loadcustomers()
{
$data['cust'] = this->customerlookup_model->get_customers();
$this->load->view('customerlookup',$data);
}
}
And here's what the model looks like:
<?php
class Customerlookup_model extends CI_Model
{
public function __construct()
{
parent::Model();
$this->load->database();
}
public function get_customers()
{
$query = $this->db->get('customer');
return $query->result_array();
}
}
If I try to test this out by doing either:
localhost/myapp/index.php/customerlookup/loadcustomers
or
localhost/myapp/index.php/customerlookup/
nothing happens and no errors appear and no data or messages either. We are using the latest CodeIgniter (2.1.3).
Any suggestion?
i found the problem. php errors were not turned on. there were some syntax problems. checked the apache error log and figured out that the errors were just not being displayed
thanks for the help guys
you are missing with your url helper
$this->load->helper('url');
into your
class Customerlookup extends CI_Controller {
//constructor for this class
public function __construct()
{
parent::__construct();
$this->load->model('customerlookup_model');
$this->load->helper('url');
}
public function index()
{
echo 'test';
}
public function loadcustomers()
{
$data['cust'] = this->customerlookup_model->get_customers();
$this->load->view('customerlookup',$data);
}
}

Categories