I am trying to test my codeigniter system using simpletest, however I keep getting the error. I am very new to PHP and codeigniter so please go easy.
Fatal error: Call to a member function add() on a non-object in C:\xampp\htdocs\di2\application\tests\simple_test.php on line 35
This is my test file, at the moment I am trying to simply add a new user to the database.
require_once BASEPATH."core/Model.php";
require_once APPPATH."models/Users_model.php";
class ExampleTest extends UnitTestCase
{
function ExampleTest()
{
parent::__construct('Example tests');
}
///// All functions starting with "test" will be tested /////
function testSimpleStuff()
{
$name = 'Andreas';
$online = TRUE;
$this->assertEqual($name, 'Andreas');
$this->assertTrue($online);
}
//There are already 8 members in the table
function testAdd()
{
$this->model->add("new member");
$this->assertEqual($this->model->get(9),'new member');
$this->assertNull($this->model->get(0));
$this->assertNull($this->model->get(10));
}
}
This is the model I am calling:
class Users_model extends CI_Model
{
function __construct()
{
parent::__construct();
}
//Used to retrieve all of the members from the database and list them by their id in descending order
function get($id = null)
{
$this->db->select('id, first_name, last_name, dob, email_address, phone_number, address_line_1, address_line_2, town, county, postcode, student, volunteer');
$this->db->from('Users');
if (!is_null($id)) $this->db->where('id', $id);
$this->db->order_by('id', 'desc');
return $this->db->get()->result();
}
function add($data)
{
$this->db->insert('Users', $data);
return $this->db->insert_id();
}
function update($data, $id)
{
$this->db->where('id', $id);
$this->db->update('Users', $data);
return $this->db->affected_rows();
}
function delete($id)
{
$this->db->where('id', $id);
$this->db->delete('Users');
return $this->db->affected_rows();
}
function filterUsers()
{
return $this->db->get('Users')->result();
}
}
Can anyone please tell me why I am getting this error, and how I can go about fixing it. Cheers in advance.
This is not the right way to call a model in codeigniter.
$this->model->add("new member");
It should be-
$this->load->model('Users_model');
$this->Users_model->add('new member');
This is because, in CodeIgntier, you need to include your model explicitly (we can also do it in autoload.php).
In your controller's constructor
function ExampleTest()
{
parent::__construct('Example tests');
$this->load->model('Users_model');
}
Now, call it from your controller like:
$this->Users_model->add("new member");
Related
I'm writing PHP using CI. I inserted data, but got an error like this:
Code:
class Recruit extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->database();
$this->load->model('Resume_model');
$this->load->helper(array('form','url'));
}
public function index() {
$this->data['posts'] = $this->Resume_model->get_resume();
$this->load->view('recruit', $this->data);
}
public function resume_form() {
echo 'OK';
$test = $this->input->post('type');
echo 'test:',$test;
echo 'KO';
$save = array(
'type' =>$this->input->post('type'),
'fullname' =>$this->input->post('fullname'),
'tel' =>$this->input->post('tel'),
'location' =>$this->input->post('location')
);
$this->Resume_model->saveResume($save);
redirect(base_url()."recruit");
}
Class Resume_model extends CI_Model {
function saveResume($data) {
{
$this->db->insert('resume', $data);
$resume_id = $this->db->insert_id();
}
return $resume_id;
}
function get_resume() {
$this->db->select("fullname,type");
$this->db->from('resume');
$query = $this->db->get();
return $query->result();
}
function getResume() {
$query = $this->db->query('SELECT * FROM resume');
if($query->num_rows() > 0 ) {
return $query->result();
} else {
return array();
}
}
public function deleteResume($id) {
$this -> db -> where('id', $id);
$this -> db -> delete('resume');
}
Make your fields in the DB to allow NULL input.
The reason is simple, your localhost environment and your online environment are different. Different MySQL configurations or different versions causes these errors.
Sometimes there is a need for putting in an empty record into a reference table for future use or because of the order of your coding event but to fix it you have to either put data into your columns upon inserting or setting your column (type) to 'NULL' or 'NULLABLE' from the DB.
so I believe I have it right (im new to active records).
the problem I am having I keep getting error
Message: Call to a member function getbank() on a non-object
My code is simple
Controller
class Profile extends CI_Controller
{
function index()
{
$data = array();
if($query = $this->profile_model->getbank())
{
$data['records'] = $query;
}
$this->load->view('profile_view', $data);
}
}
model
class Profile_model extends CI_Model {
function getbank()
{
$query = $this->db->get('bank');
return $query->results();
}
Its so simple, I cant see a error, thank you.
Try this
Controller
class Profile extends CI_Controller
{
function index()
{
$data = array();
$this->load->model('profile_model'); # Added
$query = $this->profile_model->getbank(); # Changed
if(!empty($query)) # Changed
{
$data['records'] = $query;
}
$this->load->view('profile_view', $data);
}
}
Model
class Profile_model extends CI_Model {
public function getbank() # Changed
{
$query = $this->db->get('bank');
return $query->result(); # Changed
}
}
Update
Simple answer, thanks to all those who tried helping. I had the model file name with a small letter not a capital.
Eg profile_model.php
should be Profile_model.php
However, this solution is elegant and simple, so it improved my code so il accept this one.
It may be because you didn't load your model?
Try changing your controller to:
class Profile extends CI_Controller
{
function index()
{
$data = array();
$this->load->model('Profile_model');
if($query = $this->Profile_model->getbank())
{
$data['records'] = $query;
}
$this->load->view('profile_view', $data);
}
}
I am a new codeigniter developer and have encountered an error. I have stored some value in database and want to display it in view page. Help me please.
Controller
function get_user_value() {
$data['query'] = $this->user_m->user_data_set($query);
$this->load->view('user_settings', $data); }
Model
public function user_details()
{
// $query = $this->db->select('*')->from('user')->get();
$query = $this->db->get('user');
return $query->$result();
}
Try this:
Note call first the model to your controller
add first this function _construct()
function __construct() {
parent::__construct();
$this->load->model("model_file_name", "model_name_alias");
//Example $this->load->model("user_model","user");
//the "user_model" is the file name in your model folder
}
function get_user_value() {
$data['query'] = $this->model_name_alias->user_details();
$this->load->view('user_settings', $data);
}
I am having an issue calling a stored procedure in my model not sure if I am doing it right the screen is coming up blank, also do i need to auto load anything to call a stored procedure?
<?php
class Employee_model extends CI_Model
{
public function insert_employee($data)
{
$this->db->insert('employee_list',$data);
return $this->db->insert_id();
}
public function get_employee()
{
$this->db->query("call {storedprocedure Select_employeelist} ");
$query =$this->db->get();
return $query->result();
}
public function delete_employee($id,$data)
{
$this->db->where('id',$id);
$this->db->update('employee_list',$data);
return print_r($data);
}
public function edit_employee($id)
{
$this->db->select('*');
$this->db->from('employee_list');
$this->db->where('id',$id);
$this->db->where('status',1);
$query =$this->db->get();
return $query->result();
}
public function update_employee($data,$id)
{
$this->db->where('id',$id);
$this->db->update('employee_list',$data);
return print_r($data);
}
}
To call a procedure from codeigniter
Consider test_proc as your procedure. Then it should be
$this->db->query("call test_proc()");
If you need to to send parameters then
$query = $this->db->query($test_proc,array('id'=>'1','name'=>'test','address'=>'abc'));
To see the result then
print_r($query);
i have model and defined 2 function
1.getMember()
2. getMemberCommunity($member_id)
Controller code
public function index($pass=null){
$this->load->model('member_model');
$data['query']=$this->member_model->getMember();
$this->load->view('backend/project',$data);
}
View
foreach($query as $row) {
// i want to call my another model function ?
//please give me any solutions ya tips
}
You could do something like this by joining the members table where the member id is equal to the member id in the member community table, I think this is what you want:
Model
public function getMembers(){
$this->db->from('members')
->join('member_community', 'members.id = member_community.member_id', 'left');
$members = $this->db->get();
return $members;
}
Controller
public function index($pass=null){
$this->load->model('member_model');
$data['query']=$this->member_model->getMembers();
$this->load->view('backend/project',$data);
}
View
foreach($query->result_array() as $row) {
// do whatever in here
}