James B
James B

Reputation: 35

Doctrine2 namespace object

I've just started to work with Doctrine2 in Zend Framework (also just starting to work with namespaces) and am getting data out of a database. I'm getting the rows from the database and assigning them for the view with no problem, but when looping through the rows and trying to echo out a value, I'm getting an empty string.

<?php
  foreach ( $this->rows as $row )
  {
    echo 'id = ' . $row->id . ' <br>';
  }
?>

Result is "id = "

A var_dump() on $row results in:

object(My\Entity\Events)#227 (11) {
  ["id":"My\Entity\Events":private]=>
  int(1)
  ["_userId":"My\Entity\Events":private]=>
  int(1)
  ["_startDateTime":"My\Entity\Events":private]=>
  object(DateTime)#224 (3) {
    ["date"]=>
    string(19) "2012-09-08 19:00:00"
    ["timezone_type"]=>
    int(3)
    ["timezone"]=>
    string(16) "America/New_York"
  }
  ["_endDateTime":"My\Entity\Events":private]=>
  object(DateTime)#220 (3) {
    ["date"]=>
    string(19) "2012-09-08 20:00:00"
    ["timezone_type"]=>
    int(3)
    ["timezone"]=>
    string(16) "America/New_York"
  }
  ["_eventTitle":"My\Entity\Events":private]=>
  string(11) "Dummy Event"
  ["_data":"My\Entity\AbstractEntity":private]=>
  NULL
}

I'm guessing this is something simple I'm missing/unaware of and would appreciate any insight.

Upvotes: 1

Views: 87

Answers (1)

Matteo Tassinari
Matteo Tassinari

Reputation: 18584

From your own var_dump(), the id property is private, so it cannot be accessed directly as you do in the foreach loop.

You must create an accessor method, for example getId() which will then return the value of id property.

See also http://php.net/manual/en/language.oop5.visibility.php

Upvotes: 1

Related Questions