Viewed   707 times

I get this warning in my error logs and wanted to know how to correct this issues in my code.

Warning: PHP Notice: Undefined property: stdClass::$records in script.php on line 440

Some Code:

// Parse object to get account id's
// The response doesn't have the records attribute sometimes.
$role_arr = getRole($response->records);  // Line 440 

Response if records exists

stdClass Object
(
    [done] => 1
    [queryLocator] =>
    [records] => Array
        (
            [0] => stdClass Object
                (
                    [type] => User
                    [Id] =>
                    [any] => stdClass Object
                        (
                            [type] => My Role
                            [Id] =>
                            [any] => <sf:Name>My Name</sf:Name>
                        )

                )

        )

    [size] => 1
)

Response if records does not exist

stdClass Object
(
    [done] => 1
    [queryLocator] =>
    [size] => 0
)

I was thinking something like array_key_exists() functionality but for objects, anything? or am I going about this the wrong way?

 Answers

4
if(isset($response->records))
    print "we've got records!";
Tuesday, August 30, 2022
3

I found a solution to the problem: this is a bug in Symfony 2.6, actually 2.7 seem to have fixed it.

If someone still creates a project with composer create-project symfony/framework-standard-edition ..., where the Symfony2 version defaults to 2.6 and experiences the same problem I have faced, here is a workaround:

Either edit YourProject/app/SymfonyRequirements.php line 406 and

YourProject/vendor/sensio/distribution-bundle/Sensio/Bundle/DistributionBundle/Resources/skeleton/app/SymfonyRequirements.php line 406 and replace:

 is_dir($this->getComposerVendorDir()),

With this:

is_dir(__DIR__.'/../vendor/composer'),

This will remove the Major Problem complaining about the vendors. Then this will instead remove the file_get_contents() Warning:

Inside YourProject/app/SymfonyRequirements.php and make line 546 look like this:

file_get_contents(__FILE__) === file_get_contents(__DIR__.'/../vendor/sensio/distribution-bundle/Sensio/Bundle/DistributionBundle/Resources/skeleton/app/SymfonyRequirements.php'),

Then open localhost/YourProject/web/config.php and you will see the nice Symfony Welcome page again.

Thursday, December 22, 2022
 
gmaiolo
 
2

Every time you do $array[] = it inserts a new element in the end of an array. What you need to do is:

class MaterialType {

public $id;
public $name;


function getAllMaterialType() {

    $query = "SELECT * FROM material_types";
    $result = mysql_query($query);

    $arr = array();
    while($row = mysql_fetch_array($result)) {
        $mat = new MaterialType();
        $mat->id = $row['m_type_id'];
        $mat->name = $row['m_type_name'];
        $arr[] = $mat;
    }

    return $arr;
}

}
Tuesday, November 1, 2022
 
cubbi
 
5

If you want to avoid the warning you'll need to pre-create each level:

$data = new stdClass();
$data->result = new stdClass();
$data->result->complex = new stdClass();
$data->result->complex->first_key = $old_data->result->complex;
Tuesday, August 30, 2022
 
naim
 
5

edit: it's currently 2016-09-24, and PHP 5.4 has been released 2012-03-01, and support has ended 2015-09-01. Still, this answer seems to gain upvotes. If you're still using PHP < 5.4, your are creating a security risk and endagering your project. If you have no compelling reasons to stay at <5.4, or even already use version >= 5.4, do not use this answer, and just use PHP>= 5.4 (or, you know, a recent one) and implement the JsonSerializable interface


You would define a function, for instance named getJsonData();, which would return either an array, stdClass object, or some other object with visible parameters rather then private/protected ones, and do a json_encode($data->getJsonData());. In essence, implement the function from 5.4, but call it by hand.

Something like this would work, as get_object_vars() is called from inside the class, having access to private/protected variables:

function getJsonData(){
    $var = get_object_vars($this);
    foreach ($var as &$value) {
        if (is_object($value) && method_exists($value,'getJsonData')) {
            $value = $value->getJsonData();
        }
    }
    return $var;
}
Thursday, September 1, 2022
 
guelfey
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :