Hard Fitness
Hard Fitness

Reputation: 452

PHP iterate thru doctrine object inside array

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.

Upvotes: 0

Views: 242

Answers (1)

Raphaël Malié
Raphaël Malié

Reputation: 4012

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();
    }

Upvotes: 1

Related Questions