0

I have a doctrine array which holds an object, and that object contains properties, I'm trying to access each one but something doesn't work right.

Here is the print_r() of $users:

Array ( 
    [0] => Entities\Months Object ( 
        [id:Entities\Months:private] => 12
        [month:Entities\Months:private] => December 
        [units:Entities\Months:private] => 1 
    ) 
)

Here is the code:

$q = $this->doctrine->em->createQuery("select m from Entities\Months m where m.month = 'December'");
$users = $q->getResult();
print_r($users);
foreach($users as $key => $value){
    echo $value->id:Entities\Months:private;
}

This throws an error probably as the characters are messing up the property name. I tried this as well:

echo $value->{'id:Entities\Months:private'};

But says:

A PHP Error was encountered

Severity: Notice

Message: Undefined property: Entities\Months::$id:Entities\Months:private

Filename: controllers/data.php

Line Number: 264

So if anyone knows how to read these objects to manipulate them it would be appreciated.

1 Answer 1

1

Usually with Doctrine, all properties of entities are private or protected. You can access them with the getter $value->getId();

So your code should be

$q = $this->doctrine->em->createQuery("select m from Entities\Months m where m.month = 'December'");
    $users = $q->getResult();
    print_r($users);
    foreach($users as $key => $value){
        echo $value->getId();
    }
Sign up to request clarification or add additional context in comments.

1 Comment

perfect! works great thx for that info, i have getters and setters, was just not thinking about using them. Though a simple reading of the key would work

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.