CodeIgniter Undefined property when loading a model [duplicate] - php

This question already has answers here:
Codeigniter - I am looking to use/connect to a different database for one of my controllers and one model
(3 answers)
Closed 10 years ago.
I'm receiving the following errors whilst trying to implement a very simple model controller in codeigniter. I'm new to the framework but as far as I can see this should work.
I've also tried auto loading the model. I am autoloading the database library.
Message: Undefined property: User::$user_model
Fatal error: Call to a member function get_user() on a non-object
The model
class User_model extends CI_Model
{
function __construct()
{
parent::__construct();
}
function get_user()
{
return "test";
}
}
The controller
class User extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
function index()
{
$this->load->model('User_model');
$data['value'] = $this->User_model->get_user();
$this->load->view('user_edit', $data);
}
}
Thanks

just use some thing like this, $this->load->model('user_model'); don't use $this->load->model('User_model'); and make sure you have same name, when the model name like user_mode.php and the class like class User_model extends CI_Model(){} so when you call it from controller use the lower case look like your model name user $this->load->model('user_model')

I think i've found the problem. I had some additional setter methods in my user controller which i've used all the time, didnt think they would be a problem.
public function __set($key, $value)
{
// Check to see that the requested attribute exists and then assign the value
if (property_exists($this, $key))
{
$this->$key = $value;
}
}
turns out I needed to take away one of the underscores as codeigniter doesn't like it.
public function _set($key, $value)
I should have really included the full class but I was trying to keep it as simple as possible!

Related

Call to undefined method Illuminate\Database\Query\Builder::groups() eager loading

I am creating a website using laravel. I have a small issue with eager loading. I have already made several websites with laravel, but still I can't find what is wrong here.
This is my config model:
<?php
class Config extends \Eloquent {
protected $table = "configs";
public function groups() {
return $this->hasMany('ConfigOptionGroup', 'config_id');
}
}
And this is my testcontroller class:
<?php
namespace WebsiteController;
class DemoController extends \BaseController {
public function getTest() {
$c = \Config::where('id', 1)->with(['groups' => function($q){
$q->whereNull('config_option_group_id');
}])->first();
return $c;
}
}
Whenever I surf to the url that calls the getTest method I get an error saying Call to undefined method Illuminate\Database\Query\Builder::groups(). However, the function groups() exists in the Config model.
When I remove the with() function from the query, it works just fine. But I can never load the groups via the relation.
Is there anyone who can help me with this problem?
Update: I have removed the Config facade by commenting out the line in /app/config/app.php.

using static variable in different controller

i'm making an application where i'm saving user information in user controller of my code igniter application inside a static variable, now i want to access static variable in other controller, how can i approach that? my code here
<?php
class User extends CI_Controller{
public static $user_data = array();
public __construct()
{
parent::__construct();
self::$user_data = array('value'); // values from the model
}
}
// now i can user that static variable from the view in this controller
class Friends extends CI_Controller{
public __construct()
{
parent::__construct();
if(User::$user_data->isFriends)
{
redirect('person/'.User::$user_data->id);
}
}
}
// how can i access this functionality in codeigniter? it gives error undefined class User not found
static::$user_data = array('value')
shouldn't it be self::... in your User class?
There are a number of issues with your code.
As you have defined that $user_data is an array with public static $user_data = array(); at the top of the User class, it is unnecessary to redefine it on line 7, i.e. self::$user_data = array('value');
Second issue is that the $user_data array will only contain one element, 'value'. I assume by lines 15 and 17 (User::$user_data->isFriends and User::$user_data->id), you are looking for two elements, 'isFriends' and 'id', these will not exist.
This brings me to another issue, the syntax you are using to get an element of the $user_data array will not work. The syntax '->' followed by a property name is used for variables of type Object, and cannot be used for an associative array.
Instead, you should use - for example - $user_data['isFriends'].
I have updated your code below...
<?php
class User extends CI_Controller{
public static $user_data = array();
public __construct()
{
parent::__construct();
// this does the same thing as self::$user_data = array('value');
// but without unnecessarily declaring the array again.
self::$user_data[] = 'value';
}
}
// now i can user that static variable from the view in this controller
class Friends extends CI_Controller{
public __construct()
{
parent::__construct();
if(User::$user_data['isFriends'])
{
redirect('person/'.User::$user_data['id']);
}
}
}
// how can i access this functionality in codeigniter? it gives error undefined class User not found
Try running the code and let me know what errors are being displayed and I will try and resolve them by updating my answer

Codeigniter -fetching data sitewide with codeigniter hooks

I am trying to get scroll news in any page of the website from database without having to pass the variable in every controller of the site. So I decided to use hooks instead of passing scroll variable in every controller.
I did create a class like this
class Scroll
{
function getScroller()
{
$data = array();
$CI =& get_instance();
$CI->db->where('a_status','active');
$CI->db->limit(4);
$CI->db->order_by('id','desc');
$Q = $CI->db->get('news');
if($Q->num_rows() > 0){
foreach($Q->result_array() as $row){
$data[] = $row;
}
}
$Q->free_result();
return $data;
}
}
What I get now is
Severity: Notice
Message: Trying to get property of non-object
Call to a member function get() on a non-object in E:\xampp\htdocs\
Can anyone please help me how to do this ? Thanks I want to get scrollernews in any controller's view automatically without having to pass in each controller. Thanks
If you are defining that on a view level, there is no need for that.
You can define db requests directly in a view.
Other approach would be to have separated controller which with separate view and load it in the page through iframe. It's often used for "web widgets" that can be later on loaded in to another pages.
Extending the core class of CI Controller will should cause you less troubles.
http://ellislab.com/codeigniter/user-guide/general/core_classes.html
application/core/MY_Controller.php
class MY_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
//By do this, all controllers who use this class as parent controller
//will have $news in their views
$this->load->vars(array(
'news' => array()
));
}
}
application/controller/welcome.php
class Welcome extends MY_Controller {
public function index()
{
$this->load->view('welcome_message');
}
}
application/views/welcome_message.php
var_dump($news);
use separated library or helper and call that method on controller's constructs like :
class My_Controller extends CI_Controller(){
function __construct(){
parent::__construct();
//load your library
//call your library method
}
}

error calling member function of alleged non-object in Codeigniter controller

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.

Cakephp error:Call to a member function find() on a non-object

I am having problems with my code. When I try to debug my web application I get the following error message....Call to a member function find() on a non-object.....here is my code
class TeamsController extends AppController {
var $name = 'Teams';
function index() {
$this->set('teams', $this->team->find('all'));
}
function Welcome() {
}
}
I am trying to display records from my MySQL database. Now with that said, I did this tutorial and I followed the instructions down to the tee.....but somehow my code has bugs. The only difference between my code and the code of the tutorial I did is the variable names...and the controller names....and the I dont have the hello world function... Here is a sample of the code from the tutorial I did....
class PostsController extends AppController {
var $name = 'Posts';
function index() {
$this->set('posts', $this->Post->find('all'));
}
function hello_world() {
}
}
With that said, am I suppose to declare an instance of an object to get this to work?
It's likely a case sensitivity issue:
function index() {
$this->set('teams', $this->Team->find('all'));
}
If not, ensure your controller has access to the Teams model (e.g. $uses).

Categories