Call to undefined method codeigniter - codeigniter

where i'm doing mistake? I tried to solve it, but nothing. I did all right, is the first time I have this error.
Fatal error: Call to undefined method Team_model::exist_team() in C:\wamp\www\example\application\libraries\teams.php on line 28
This is my library:
class Teams { // My class
function __construct()
{
$this->ci =& get_instance();
$this->ci->load->model('team_model');
}
function exist_team($team_id) { // this function give me that error
if ($query_get_info = $this->ci->team_model->exist_team($id_team)) { // line with error
return $query_get_info[0];
} else {
return false;
}
}
}
This is my model
class Team_model extends CI_Model {
function exist_team($id_team) { // function exixst team in library
$this->db->select('*');
$this->db->from('teams');
$this->db->where('url',$id_team);
$query = $this->db->get();
if ($query->num_rows() == 0) {
return false;
} else {
return $query->result();
}
}
}

I was able to run this on a clean CI install without errors. Here's what I would check:
Make sure you're file names for both the model and library are capitalized. (Team_model.php & Teams.php).
*Note -- Codeigniter Docs state that only the Library names must be capitalized, but using the current version on GitHub, the load fails unless both files were capitalized.
Declare the CI variable outside the constructor scope. This shouldn't be an issue, but it's worth exploring.
class Teams {
private $_ci;
function __construct()
{
$this->_ci =& get_instance();
$this->_ci->load->model('team_model');
}
}
Unrelated, but an error: You use $team_id then $id_team.
function exist_team($team_id) {
if ($query_get_info = $this->ci->team_model->exist_team($id_team)) {

Check for spelling mistakes of Models name, if there is any spelling mistakes or distinct name from original model show error of Undefined Method.

Related

How to fix this error: Call to a member function get() on a non-object?

Fatal error: Call to a member function get() on a non-object in
C:\Program Files (x86)\EasyPHP-DevServer-14.1VC9\data\localweb\Paging\application\models\name_model.php
Severity: Error
Message: Call to a member function get() on a non-object
Filename: models/name_model.php
Line Number: 13
Backtrace:
models/name_model.php:
class Name_model extends CI_Model {
public function __construct()
{
parent::__construct();
}
public function get_name()
{
$query = $this->db->get('name');
return $query->result();
}
}
Line 13: $query = $this->db->get('name');
How to fix the error message?
You didn't load database libraries that why you getting this error.
Open your autoload.php file and add this string in libraries
$autoload['libraries'] = array('database');
And another way is you need to load libraries in the model
class Name_model extends CI_Model {
public function __construct()
{
parent::__construct();
$this->load->database();
}
public function get_name()
{
$query = $this->db->get('name');
return $query->result();
}
}
Kindly write it as follow when your going to fetch only name(s) from your table,
public function get_name()
{
$query = $this->db->select('name')->get('your_table_name');
return $query->result();
}
It will work fine.

Check for Sessions in Controller _constructor

I am new to Codeigniter and still trying to understand some basics.
I am building a registration/login system. everything good for now.
I am creating a controller to show the user info. Before that I want to check if the session existis and if user is logged in, if not I want to redirect to the site root.
class Myprofile extends CI_Controller {
function __construct()
{
$user_data = $this->session->userdata('user_data');
$user_data['logged_in'] = isset($user_data['logged_in']) ? $user_data['logged_in'] : null;
if($user_data['logged_in'] != 1){
redirect();
}
}
public function index(){
redirect("myprofile/info");
}
public function info(){
echo"here I'll ave my info";
}
}
The problem is the error I am getting. It looks like I it can't see the sessions in the construct part of the script.
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Myprofile::$session
Filename: controllers/myprofile.php
Line Number: 6
This would be the very best solution, otherwise I need to put the same code on all the functions.
Hope to hear some feedback help. Thanks
Perhaps the session library is not loaded. Add session class on the autoload array() located in application/config/autoload.php
eg.
$autoload['libraries'] = array('database', 'session', 'output');
class Myprofile extends CI_Controller {
function __construct()
{
$this->load->library('session');
$user_data = $this->session->userdata('user_data');
$user_data['logged_in'] = isset($user_data['logged_in']) ? $user_data['logged_in'] : null;
if($user_data['logged_in'] != 1){
redirect();
}
}
public function index(){
redirect("myprofile/info");
}
public function info(){
echo"here I'll ave my info";
}
}

CodeIgniter - Call to a member function ... on a non-object from Model [duplicate]

This question already has answers here:
Call to a member function on a non-object [duplicate]
(8 answers)
Closed 9 years ago.
I'm using CI 2.1.3 and I'm having issues with loading models and referring to the CodeIgniter "super object".
For example:
I'm trying to log in using my Login controller:
class Login extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('Users_model');
}
public function validate_credentials() {
$this->load->library('form_validation');
// Some form validation here...
$query = $this->Users_model->validate();
if($query) { // if the user's credentials validated...
// something
} else {
// something
}
}
And when it gets to my Users_model:
class Users_model extends CI_Model {
public function __construct()
{
parent::__construct();
}
public function validate()
{
$this->db->where('active', 1);
$this->db->where('email', $this->input->post('email'));
$this->db->where('password', md5($this->input->post('password')));
$query = $this->db->get('user');
if($query->num_rows == 1)
{
return $query;
}
}
}
I get an error "Fatal error: Call to a member function where() on a non-object in users_model.php on line XX" referring to the first line of validate() function.
I can get it working by using double colon (::) operator in Login controller like Users_model::validate() but I think I shouldn't need that.
I can also get it working by writing something like:
$ci = get_instance();
$ci->db->where...
at the start of the Users_model->validate() function, but I think I shouldn't need to do that either.
The database class is being loaded in autoload.php.
So the problem is that $this in my models refers to the model class itself rather than the "super object" it's supposed to. I have no moves left and I'm guessing it's about something very simple but I just can't see it. Please help me.
Please try by loading CI library like this
$this->load->library('database');
in __construct() function before loading model:
or load this library from autoload file like:
$autoload['libraries'] = array('database','form_validation', 'session');

How do I make a function available to several CodeIgniter methods?

CodeIgniter 2.1.2
I have a class that contains two methods, for the purpose of this questions showone and view. The latter returns all items of a small database and can also perform a search. The other one is for permalinks like domain.com/showone/firstname-lastname
<?php
class Pages extends CI_Controller {
public function view($page)
{
//this includes a mysql search
}
public function showone($slug)
{
//abbreviated version:
$query = "SELECT * FROM mytable WHERE slug = '" . $slug . "'";
$result = $this->db->query($query);
if ($result->num_rows() == 0)
{
//here is where I'd like to use the same search that I used in showall
}
else
{
//show the one item
}
}
} //class
?>
So if a user decides to directly enter a URL that doesn't return anything from the database, I would like to direct him to search results instead of showing a 404.
So how do I set up a function searchdatabase($query) to be used by both showone and view?
You can use your controller functions inside your controller:
public function view($page)
{
$this->showone($slug);
}
Define that function in your model, load your model and then call the model->method.
<?php
//Your Model would look something like this.
class Search_Model extends CI_Model {
public function __construct(){
parent::__construct();
}
public function showone($slug)
{
//abbreviated version:
//Its best to use active record for building your queries
$this->db->where->('slug', $slug);
$result = $this->db->get('mytable');
if ($result->num_rows() == 0)
{
//here is where I'd like to use the same search that I used in showall
}
else
{
//show the one item
}
}
} //class
And then in your controller you would do this:
<?php
//Your Controllerwould look something like this.
class Index extends CI_Controller {
public function __construct(){
parent::__construct();
//model will be loaded for each method.
//if you're going to use this model across several controllers
//its best to autoload it, set that in autoload.php under /app/config
$this->load->model('search_model');
}
public function index(){
$searchResults = $this->search_model->showone('slugone');
}
Update
I just realized you are wanting to show all results if no results were returned. In which case you would perform that logic in your model as well..
In your conditional statement you would do the following:
if ($result->num_rows() == 0)
{
return $this->showall();
}
else
{
return $this->view($slug);
}
}
your showall() and view() methods would return $result->result();

Loading custom config file into a Codeigniter library

I know this is probably simple, but I'm not getting. I've created a library, and I want to load the parameters from a config file. So here's an example of what I have:
// libraries/Mylib.php
class Mylib {
var $ci;
var $key;
public function _construct {
$this->ci =& get_instance();
$this->ci->config->load('mylib');
$this->key = $this->ci->config->item('key');
}
public function myKey() {
return "Key=" . $this->key;
}
}
// config/mylib.php
$config['key'] = 'randomcharacters';
I load the library, and try to print out the myKey function, but it just returns "Key=", without the actual key. What am I missing?
It seems like you missed an underscore for your constructor:
instead of
public function _construct () {
you should use
public function __construct () {

Resources