1

I am using CodeIgniter, I am returning more than one value in the model so I did like

return array('true' =>1,'customer_id' => $result->customer_id);

and In the controller, I am displaying like

$true=$result['true'];
$custId=$result['customer_id'];

So there is no issue in this.

Now let's talk about in details

In the model, I have this logic

//sql query
if ($result) {
    if ($result->password_change == 0) {
         return array('true' =>1,'customer_id' => $result->customer_id);//multiple returns
    }
    else{
       return $result;
    }
}
else{return false;}

In the controller

 $result=$this->Member_model->check_data(parameters);
 $true=$result['true'];
 $custId=$result['customer_id'];

 if ($result) {
     if($true == 1){
         //some code here
     }
     else{
         //some code here
     }
 }
 else{
 }

This is my second code.

if ($result) {
  if ($result - > password_change == 0) {
    if ($result - > id == $id) {
      return array('true' => 1, 'customer_id' => $result - > customer_id); //return more then 2
    } else {
      return false;
    }
  } else {
    if ()) // some condition
  {
    return (array) $result;
  } else {
    return false;
  }
}
} else {
  return false;
}

I am getting the error that

"Message: Cannot use object of type stdClass as array"

Because when returning if condition(from the model) then it's working but when it returns else (I mean $result from the model) then I am getting the error because it is not getting the $result['true'].

Hope you can understand my issue. Would you help me out on this issue?

3
  • Well you need to accommodate the different return permutations in your controller. You are currently just assuming the return value is the array you're creating. Commented Dec 20, 2018 at 14:27
  • @Utkanos, Can you explain me with the help of an example? Please Commented Dec 20, 2018 at 14:34
  • It's like this. Your model is returning different types of data, but your controller is not checking for this - it's assuming only one of those types. So either harmonise your model to always return a given type of data, or tell your controller to first CHECK the type of returned data before proceeding. See @Richard Parnaby-King's answer for more. Commented Dec 20, 2018 at 15:03

2 Answers 2

1

You have two options. Either: ALWAYS return an array, or return an ArrayObject:

Alway return an Array

In your else, cast $result as an array:

//sql query
if ($result) {
    if ($result->password_change == 0) {
         return array('true' =>1,'customer_id' => $result->customer_id);//multiple returns
    }
    return (array) $result;
}
return false;

Always return an ArrayObject

Not knowing if this model is returning the result anywhere else in you application, I have no idea if other code expects $result to be an object. Casting as an array might break your code elsewhere. Let's convert $result from stdClass to an ArrayObject:

//sql query
if ($result) {
    if ($result->password_change == 0) {
         return array('true' =>1,'customer_id' => $result->customer_id);//multiple returns
    }
    return new \ArrayObject($result, \ArrayObject::ARRAY_AS_PROPS);
}
return false;

This approach will allow you to call $result['test'] AND $result->test.

Sign up to request clarification or add additional context in comments.

6 Comments

One little issue in the case where it returns false which is boolean so the controller needs to test $result before trying to extract either arrays or objects...
@Richard, Give me some time to check.
I just want to know why you removed the else condition? because I have 2 more conditions in the else. I am still getting the error Message: Undefined index: true
Sorry, that's my personal ocd. There's nothing wrong leaving them in :)
@RichardParnaby-King, Wait a min, I am updating my code in the question for more clarity.
|
1
Yeah, ok this is solution for multiple conditions : 

    $this->db->select('*'); // Here your column name
    $this->db->from('members'); // Table name
    $this->db->where($login_access); // Here your condition
    $query = $this->db->get();

    $result = $query->result_array();
    $query = $this->db->get('customer'); // Your code above if condition
    $result = $query->result_array(); // Get data in array Format using **result_array()**
        if ($result) {
          if ($query->num_rows() > 0) { // You have to change condition from **$result['password_change ']** To **$query->num_rows() > 0**
            if ($result - > id == $id) { // Also change here condition from **$result - > id** To $result['id']
              return array('true' => 1, 'customer_id' => $result['customer_id']); //Also change here from **$result - > customer_id** To $result['customer_id']
            } else {
              return false;
            }
          } else {
            if () // some condition
          {
            return $result; // Also Change here from **return (array) $result** To $result
          } else {
            return false;
          }
        }
        } else {
          return false;
        }

5 Comments

I don't want to remove the else condition because I have more condition inside it.
@user9437856 by mistake I posted another answer. You can find it in this updated answer.
I am getting the error in this like if ($result['password_change'] == 0) { //Message: Undefined index: password_change
ook let me check i will give you solution in as fast as possible.
Thanks for answer again. You used if ($query->num_rows() > 0) { May I know why you used that? I am fetching all the records and there is one column which is "password_change" and I am checking that column only.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.