Trying to get property of non-object error on object - php

I'm getting the following error reports:
Severity: Notice
Message:
Trying to get property of non-object
Using gettype() I can see that it's a proper object.
A print_r() returns:
stdClass Object ( [campaigngroupid] => 3 [name] => And another one [dt_created] => 2014-02-04 17:11:21 [created_userid] => 1 [deleted] => 0 )
Echoing out $object->name for example works fine, but still, I'm getting this notice...
The object is set using CodeIgniter's ->row() from a database query result.
All I can ask is, sup?

Try this in your model, Suppose $query holds the query object:
function some_model_function(){
..........
..........
if($query->num_rows() > 0 ){
return $query->row();
}
return FALSE;
}
Also check whether the notice is from any where else.

Related

CodeIgniter : for each loop Attempt to assign property 'details' of non-object

i am fetching data from two tables as below :
$form['form']=json_decode($this->db->get_where('forms',array(
'id' => $id
))->result()[0]->form_sections);
This gives me following result on printing :
Array
(
[form] => Array
(
[0] => Personal_Information
[1] => Education_
[2] => Professional_Life
)
)
Now i have details against each of these array indexes that i am trying to assign them as below
foreach ($form['form'] as $value){
$value->details=$this->db->get_where('formdata',array(
'form_section' => $value
))->result();
}
Which gives me following error
Message: Attempt to assign property 'details' of non-object
can someone please help me to sort out the issue , i have to assign the index and loop in it in my view
This should help understand and clear up the message:
// Custom data
$Person_Info = 404; // Could be any non-object
$array = array("form" => array( 0 => $Person_Info) );
foreach( $array['form'] as $value ){
$results = new stdClass();
$results->name = "User A";
$value->details = $results; // $results are you fetch request
}
// Output: Attempt to assign property 'details' of non-object in php<8.0
// Output: Uncaught Error: Attempt to assign property "details" on int php>=8.0
Try to print_r or var_dump your $value and see what you are getting. It's not an object for some of your array item.
Edit: If ever in doubt about the data. Use if (is_obejct($var)) php.net/is_object or similar checks based on what you need.

Object inside an array, passing an object to a PHP method

I have a PHP application with a function that was built to expect information from an API call. However, I'm trying to use this function by passing in information that mimics the API data.
I struggle a bit with arrays and this seems to be an object within an array.
I can access the array that the api provides, so when I use the following code ($triggers is the array the api call returns):
print("<pre>".print_r($triggers,true)."</pre>");
I get the following output:
Array
(
[0] => stdClass Object
(
[triggerid] => 18186
[status] => 0
[value] => 0
)
This is the beginning of the function:
function iterate_triggers($triggers){
$trigger_id_values = array();
foreach($triggers as $trigger) {
//Necessary to show human readable status messages.
$check_status = array(0=>"Up", 1=>"Down", 2=>"Degraded", 3=>"Maintenance");
array_push ($trigger_id_values, [$trigger->triggerid, $trigger->value]);
So if I wanted to pass this function a [triggerid] => 18186 and [value] => 1 how would i do that?
Currently I'm trying:
iterate_triggers(array(0 => array("triggerid" => 18186,"status" => 0,"value" => 1,)));
but this gives me a "Trying to get property of non-object" error. Please be kind to me, I've done my best to research and structure this on my own to no avail.
The easiest way is to cast the assoc array just to an object
In your case this would be
iterate_triggers(array(0 => (object)array("triggerid" => 18186,"status" => 0,"value" => 1,)));
You are currently creating and passing an array that contains an array, while your function expects an array of objects.
You should create your object beforehand, then construct your parameter array, and pass it to your function.
$obj = new \stdClass();
$obj->triggerid = 18186;
$obj->status = 0;
$obj->value = 1;
$arr = array($obj);
iterate_triggers($arr);
This comment on php.net, and the rest of that object documentation, may be useful to you.
By far the easiest and correct way to instantiate an empty generic php object that you can then modify for whatever purpose you choose: <?php $genericObject = new stdClass(); ?>
The error message you are seeing with your own code is caused by $trigger->triggerid inside the function, when $trigger is an array instead of an object as the function expects. Object properties are accessed using $someObject->propertyName notation, while array elements are accessed using $someArray['keyName']

Echoing the data from data array passed to view in code igniter

So I'm trying to echo specific values from the array, but I get message:
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: views/drilldown.php
Line Number: 92
that many times.
Heres the result of print_r of the array:
Array ( [0] => Array ( [productCode] => S10_1949 [productName] => 1952 Alpine Renault 1300 [productLine] => Classic Cars [productScale] => 1:10 [productVendor] => Classic Metal Creations [productDescription] => Turnable front wheels; steering function; detailed interior; detailed engine; opening hood; opening trunk; opening doors; and detailed chassis. [quantityInStock] => 7305 [buyPrice] => 98.58 [MSRP] => 214.3 [image] => S10_1949.jpg ) )
View Code:
$follow = $p['0'];
foreach($follow as $item) {
echo $item->productCode;
}
Model Code:
public function getProduct($p)
{
$this->db->where('productCode', $p);
$resultset = $this->db->get('products');
return $resultset->result_array();
}
Controller Code:
function drillDown()
{
$productID=$this->uri->segment(3);
$data['p']=$this->littlemodel->getProduct($productID);
$this->load->view('drillDown',$data);
}
Can anybody please help?
I believe you just want one row from the database.
You should be using
return $resultset->row()
So in the model, try this:
public function getProduct($p)
{
$this->db->where('productCode', $p);
return $this->db->get('products')->row();
}
Keep the controller code the same.
In the view, use this:
echo $p->productCode;
This is just an optimized way of fetching your requirements. In case of any trouble, do let me know. Will be happy to help.

PHP echo value of a object from array

I have a array which I am printing using print_r.
<?php
print_r ($this)
?>
I get following result in my browser.
PackingListForm Object
(
[objShipment:protected] => Shipment Object
(
[objCustomFieldArray] =>
[intShipmentId:protected] => 38
[strShipmentNumber:protected] => 1035
[intTransactionId:protected] => 97
[intFromCompanyId:protected] => 1
[intFromContactId:protected] => 1
[intFromAddressId:protected] => 1
[intToCompanyId:protected] => 2
[intToContactId:protected] => 3
[intToAddressId:protected] => 2
[intCourierId:protected] => 1
[strTrackingNumber:protected] =>
[dttShipDate:protected] => QDateTime Object
)
)
Now I want to print / echo intTransactionId.
I have used following variable to echo the result, but I am getting undefined variable.
<?php
$noted = $this->objShipment->intTransactionId;
print_r ($noted);
?>
I am getting following php exception error in my browser.
Undefined GET property or variable in 'Shipment' class: intTransactionId
Line 33: $noted = $this->objShipment->intTransactionId;
My question is how can I echo / print value of intTransactionId?
intTransactionId is a protected property which means that you can't access it outside of the class itself (or parennt class or child class).
The exception, I think, is thrown in a __get magic method defined in Shipment (or one of its parent classes). This method is called when trying to access an unset property (or non-accesible property).
Please check this behaviour.
First try to convert it into array and then it will be much easier :),
$newArray = (array)$this;
print_r($newArray);//to see what can you get from there
// get_object_vars
$newArray = get_object_vars($object);
The object is from class PackingListForm, have look on that class if you have access and see if there is any get() function.

CodeIgniter: view is not generated in a certain case

Starting from a variable called $data, which is an associative array which includes an object, and whose printed value is this:
Array
(
[item] => stdClass Object
(
[id] => 1
[tipo] => 0
[idioma] => es
[nombre] => Artí­culo de prueba
[titulo] => Esto es un artí­culo de prueba
[alias] => articulo-de-prueba
[texto] => Lorem ipsum etc etc
[url] =>
[video] =>
[fecha_c] => 2012-11-27 10:50:37
[fecha_m] => 2012-11-27 17:00:00
[fecha_p] => 2012-11-28 00:00:00
[destacado] => 0
[status] => 1
)
[imagenes] => Array
(
)
)
I need to filter its value and assign it to another array, this way:
protected function load_form($data = '') {
$this->load->helper('form');
// If item data have been sent, pass it to the form view to edit it.
// Else display empty form for new item.
if (! empty($data)) {
// Data can be an associative array with an object and another array or just an object
if (array_key_exists('item', $data)) {
$this->_vars['item'] =& $data['item'];
}
else {
$this->_vars['item'] =& $data;
}
if (array_key_exists('imagenes', $data)) {
$this->_vars['imagenes'] = $data['imagenes'];
}
}
$view = $this->load->view(ADMIN_FORMS_PATH . $this->_controller . '_form', $this->_vars, true);
/*DEBUG*/ echo $view; // just for debugging purposes
}
The first assignment generates these errors:
A PHP Error was encountered Severity: Notice Message: Undefined index:
item Filename: core/Admin_Controller.php Line Number: 205
A PHP Error was encountered Severity: Notice Message: Object of class
stdClass could not be converted to int Filename:
core/Admin_Controller.php Line Number: 205
It behaves like the item index doesn't exist, and it does. Also, it tries to convert the object to an integer.
Why does it happen and what should I do to fix it?
EDIT:
I was doing &= instead of =&. That's the reason of the errors.
Anyway, the problem persists and the code seems to stop.
EDIT2:
Trying to redefine the problem. It might be something related to CodeIgniter, so I've added the whole function, including CodeIgniter functions.
The load_form() method can be invoked from a request to create a new item, in which case $data is empty, or from a request to edit a given item (in $data). In the first case (creation), the debug line is executed, but not in the second case (edition).
This may be your issue. Near the end you have"$datos['imagenes'];" where it should be "$data['imagenes'];"
if (! empty($data)) {
// Data can be an object, or an array with object + array of images
if (array_key_exists('item', $data)) {
$this->_vars['item'] &= $data['item'];
}
else {
$this->_vars['item'] = $data;
}
if (array_key_exists('imagenes', $data)) {
$this->_vars['imagenes'] = $data['imagenes'];
}
}
Problem solved. It was too simple.
It was inside the view. I was trying to echo:
$item['id']
instead of
$item->id
The item data is being retrieved as an object, not an array. It was causing the problem silently, without any warning, notice or error.
Anyway I appreciate your quick help.

Categories