Undefined property: stdClass in my codeigniter controller - php

This is my controller function
public function check_orgn_mail()
{
$this->load->model('registrationmodel', '', TRUE);
$omail = $this->input->post('mailid');
$mailresult = $this->registrationmodel->check_org_mail();
$smil = explode("#", $omail);
$registerCompanyID = null;
if ($mailresult)
{
foreach ($mailresult as $row)
{
$registerCompanyID = $row->id;
$cmail = explode("#", $row->email);
$acmail[] = $cmail[1];
}
if (in_array($smil[1], $acmail))
{
return TRUE;
}
}
$this->form_validation->set_message('check_orgn_mail', 'Invalid Organization Mail id.');
return false;
}
This is the function in my model
function check_org_mail()
{
$this->db->select('email','id');
$this->db->from('customers');
$this->db->where('status', 1);
$query_emai = $this->db->get();
return $query_emai->result();
}
I am getting this error
Severity: Notice
Message: Undefined property: stdClass::$id Filename:
controllers/registration.php
I tried some trial and error but not working. Please help me with this. Previously it was working fine but when I checked with the different mail id, it started giving this error.

change your select to this:
$this->db->select('email, id');

You need to use
$this->db->select('email, id');
All columns are supposed be a single string argument separated by commas.
You are providing multiple arguments.

Related

Trying to get property 'staff 'of non-object when test REST API from Postman

I'm trying to understand the reason of this error message from postman when test API.
When I am testing my REST API from postman, it gives me error
ErrorException (E_NOTICE)
Trying to get property 'staff' of non-object
I try to find the problem but i can't find it. I kept searching for this but couldn't find an answer that will make this clear.
Anyone can help me on this?
Thanks!
This my code snippet
public function updatestatus($request, $leave, $is_api=0)
{
$status = $request->get('status');
$user = $is_api? JWTAuth::parseToken()->authenticate():Auth::user();
// $user = Auth::user();
$staff= $user->ref_user;
$applying_staff = $leave->staff;
$applying_user = $applying_staff->main_user;
//Approved
if($status==2 && $leave->status==1)
{
$leave->status =2;
$leave->approved_by_staff_id = $staff->staff_id;
$leave->approved_date = new Carbon('today');
$leave->save();
if($user->centre_id)
Helper::ClearObjective(10,$user->centre_id);
dispatch(new EmailJob($applying_user->email,new LeaveNotification(route('staff.leave.show',$leave->id), $leave->statusStr)))
->onConnection('database')
->onQueue('emails');
return response()->json(['Success'=>'Success']);
//send email
}
// Rejected
else if($status==3 && $leave->status==1)
{
$leave->status =3;
$leave->status_rejected_reason = $request->get('reason',null);
$leave->save();
if($user->centre_id)
Helper::ClearObjective(10,$user->centre_id);
$leave_cat = LeaveCategory::find($leave->leave_type);
if($leave_cat->leave_status!=0)
{
$leave_ent = $applying_staff->leaves()->where('leave_type',$leave->leave_type)->first();
if($leave_ent)
{
$leave_ent->leave_remaining += $leave->leave_days;
$leave_ent->save();
}
}
dispatch(new EmailJob($applying_user->email,new LeaveNotification(route('staff.leave.show',$leave->id), $leave->statusStr, $leave->status_rejected_reason)))
->onConnection('database')
->onQueue('emails');
//send email
}
//Cancelled
else if(($status==4 && $leave->status==2) || ($status==4 && $leave->staff_id == $staff->staff_id))
{
$leave->status =4;
$leave->status_rejected_reason = $request->get('reason',null);
$leave->save();
$leave_cat = LeaveCategory::find($leave->leave_type);
if($leave_cat->leave_status!=0)
{
$leave_ent = $applying_staff->leaves()->where('leave_type',$leave->leave_type)->first();
if($leave_ent)
{
$leave_ent->leave_remaining += $leave->leave_days;
$leave_ent->save();
}
}
dispatch(new EmailJob($applying_user->email,new LeaveNotification(route('staff.leave.show',$leave->id), $leave->statusStr,$leave->status_rejected_reason)))
->onConnection('database')
->onQueue('emails');
//send email
}
// return reponse()->toJson(compact('leave'));
return $leave;
}
Calling API
public function update(Request $request)
{
return $this->leaveApplicationRepository->updatestatus($request,1);
}
your updatestatus() need 3 parameter and your update() function pass only 2 paramenter;
public function update(Request $request)
{
// please provide you leave data
$leave = "your data is here";
return $this->leaveApplicationRepository->updatestatus($request,$leave, 1);
}
From the context that I can see, it is trying to reference the staff variable in:
$applying_staff = $leave->staff;
The non-object message will mean that $leave itself is the problem. Since you are passing in 1 to the call:
return $this->leaveApplicationRepository->updatestatus($request,1);
the 1 becomes the $leave parameter, and that is not an object, hence the error.
Maybe you think that by passing in 1, it will find the correct model automatically? This is not the case. You need to load that model explicitly.

Error Trying to get property of non-object and Undefined variable: Codeigniter

I am facing the following error:
Trying to get property of non-object and Undefined variable php
errors in my code
Controller:
function showDoctorInformation(){
$this->load->model('PatientModel');
$data['doctorinfo'] = $this->PatientModel->getDoctorInformation();
$this->parser->parse('patient_msgview', $data);
}
Model:
function getDoctorId() {
$this->db->from('person');
$this->db->select('doctorId');
$doctorId = $this->db->get()->result();
return $doctorId;
}
function getDoctorInformation() {
$doctorId = $this->getDoctorId();
$this->db->from('DoctorInfo');
$this->db->where('doctorId', $doctorId);
$this->db->select('name', 'surname', 'Bio', 'Address', 'img');
$doctorinfo = $this->db->get()->result();
return $doctorinfo;
}
View:
<?= $doctorinfo->name ?>
I have displayed information from the database before with this method and I can't see the error now.
result() return
This method returns the query result as an array of objects, or an
empty array on failure
So you need to fetch single data form your database using ->row()
function getDoctorId() {
$this->db->select('doctorId');
$this->db->from('person');
$this->db->select('doctorId');
$query = $this->db->get();
if ($query->num_rows == 1) {
$row=$query->row();// fetch single row
return $row->doctorId;// get doctor id
} else {
return FALSE;
}
}
And in viwe you have to get your data using foreach loop
For exm
foreach ($doctorinfo as $row)
{
echo $row['title'];
echo $row['name'];
echo $row['body'];
}

Trying to get property of non-object/ Php codeigniter

I can't seem to figure about what is wrong with this code. thank yu for your help.
I am getting error messages:
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: views/members.php
Line Number: 24
My model:
public function show_user()
{
$this->db->select('');
$this->db->from('users');
$this->db->where('email',$this->input->post('email'));
$q=$this->db->get('');
if($q->num_rows() > 0 )
{
$data = array();
foreach($q->result() as $row)
{
$data=$row;
}
return $data;
}
}
My controller:
public function members()
{
if ($this->session->userdata('is_logged_in'))
{
$this->load->model('model_users');
$data['member'] = $this->model_users->show_user();
$this->load->view('members', $data);
}
else
{
redirect('main/restricted');
}
}
My view:
echo "<p> Congratulations you are logged in!!</p>";
foreach($member as $row)
{
echo $row->Name. "<br/>";
echo $row->email. "<br/>";
}
So this my code. I want to display user information from database but I am getting errors. please help me..
You only need to return $q-result();
if($q->num_rows() > 0 )
{
return $q-result();
}
I'm not sure what your database looks like but I suspect this is because either the Name or email properties don't exist on the $row object. Are you sure your field in your users table is called Name or is it name (lowercase)? If its lowercase your code should be.
foreach($member as $row)
{
echo $row->name. "<br/>"; //Changed this line
echo $row->email. "<br/>";
}
Also your foreach loop on your model is a bit pointless. You might as well remove it and just do
return $data->row;

PHP CodeIgniter: Trying to get property of non-object

Everything seems to work just fine but I keep getting the following error:
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: models/ordermodel.php
Line Number: 24
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: models/ordermodel.php
Line Number: 31
================================================================
In the view i just echo $company_name
Controller:
$city = $this->ordermodel->get_city($order_reference);
$customerCompanyName = $this->ordermodel->get_company_name($order_reference);
$data['company_name'] = $customerCompanyName;
Model:
function get_city($ordernumber) {
$this->db->where('order_number', $ordernumber);
$city = $this->db->get('order');
return $city->row()->city;
}
function get_company_name($ordernumber) {
$this->db->where('order_number', $ordernumber);
$companyname = $this->db->get('order');
return $companyname->row()->company_name;
}
Use therresult() method, it should elimiate all of your errors
$row = $city->result(); return $row->city;
Seem that your query has no results, try:
function get_company_name($ordernumber) {
$this->db->where('order_number', $ordernumber);
$companyname = $this->db->get('order');
if ($companyname->num_rows() > 0) {
return $companyname->row()->company_name;
}
else {
return '';
}
}

php returning undefined property

In other of not be repetitive, I wrote that getName function to do the duty job for me: the classes querying the database would only send the SQL query:
public function getVolumeServer($id)
{
$str = "SELECT *FROM site_volume_server WHERE site_id='$id'";
return $this->getName($str);
}
public function getName($str)
{
$data = array();
$query = $this->getQuery($str);
if($query){
foreach($query->result() as $row){
array_push($data, $row->name);
}
}
return $data;
}
public function getQuery($str){
$data = array();
$query = $this->db->query($str);
if(!$query || $query->num_rows() == 0){
return false;
}
return $query;
}
However, I'm getting the following error:
<p>Severity: Notice</p>
<p>Message: Undefined property: stdClass::$name</p>
<p>Filename: models/sitematrix_model.php</p>
<p>Line Number: 129</p>
Line 129 is $this->getName($str).
I did not have that before, when that portion of code was embedded in the function, but there are similar functions that only differ by the table being accessed.
Any Thoughts?
Thanks
I am having the same problem except I am trying to access the $agenda->time_from property from __get() which is a time column in agenda table which is retrieved from db properly (I checked the var_dump, ran the query manually in phpMyAdmin).
I do that because the default call parent::_get() won't return the value; it returns NULL instead.
I found a workaround for my problem. All you need to do is to access the value like :
$agenda->_object['time_from']
I guess this is not quite the right solution, but working one.

Categories