2

I'm sending an object via my Action in Symfony, but I cannot retrieve it in my view.

This is the Action code:

public function testphpAction()
    {
/*SOME DOCTRINE CODE*/
$productos = $consulta->getResult();
return $this->render('TPMainBundle:Default:test.html.php', array(
                'productos' => $productos,
    ));
}

I tried the solutions from this thread but with no effort: PHP Error: Cannot use object of type stdClass as array (array and object issues)

This is the code of my view 'test.html.php':

foreach($productos as $producto) {
    echo $producto['descripcion'];
}
// This displays the error: "Cannot use object of type TP\MainBundle\Entity\Works as array

So I tried the solution from the other thread:

foreach ($productos as $producto) {
        $id         = $producto->id;
        echo $id;
//But it throws the error: Cannot access private property TP\MainBundle\Entity\Works::$id

Neither this worked:

$id = $productos->id;
// Throw: "Trying to get property of non-object"

How can I access it? I don't have this problem if I render to a twig template, but I need to use php.

The var_dump of $productos is this: (I ommited the other objects in this chain)

array(3) { [0]=> object(TP\MainBundle\Entity\Works)#290 (4) { ["id":"TP\MainBundle\Entity\Works":private]=> int(1) ["descripcion":"TP\MainBundle\Entity\Works":private]=> string(30) "Landing page for a toys store." ["img":"TP\MainBundle\Entity\Works":private]=> string(12) "images/1.jpg" ["preview":"TP\MainBundle\Entity\Works":private]=> string(13) "images/1m.jpg" }
Community
  • 1
  • 1
lio89
  • 115
  • 1
  • 12

2 Answers2

1

Define your getters, and then use them.

for example in this

foreach ($productos as $producto) 
{ $id = $producto->id; echo $id; }

Try

$producto->getId(); 

instead of

 $producto->id;

assuming you defined your getter.

Abdelaziz Dabebi
  • 1,624
  • 1
  • 16
  • 21
  • Ahhh ok, I tried something like public function{ etc thinking about the getter....... but producto->getId() worked inside the foreach !! Thank you – lio89 Aug 14 '15 at 23:03
  • depending on what you want, that's easy and depends on your logic; for example you can use <\br> html tag in the foreach loop. – Abdelaziz Dabebi Aug 14 '15 at 23:13
  • The modification doesn't work when the doctrine query is different.... I changed it in order to select with a WHERE clause, (SELECT p FROM TPMainBundle:Works p WHERE p.id = :id) and now I'm getting "Invalid argument supplied for foreach()" do you know why? – lio89 Aug 14 '15 at 23:35
  • actually, this have nothing to do with my answer, foreach expects an iterable list, but your query returns a single result, so not iterable. Other thing, don't accept and then deny it depending on your modification, that's not how the site works. – Abdelaziz Dabebi Aug 14 '15 at 23:44
  • Not about accept or not, you are asking for answers to two unrelated question. Anyway I hope I helped you and your where query works. – Abdelaziz Dabebi Aug 14 '15 at 23:51
  • Yes! you helped me a lot, but i'm still having problems with the other unrelated issue, so I'll open another thread. – lio89 Aug 15 '15 at 00:15
  • just get rid of the foreach loop. you have only one single result to display – Abdelaziz Dabebi Aug 15 '15 at 00:17
  • I tried that, but look at this: Now my view have NO for each loop!! My view can be anything, but it will keep saying "Invalid argument supplied for foreach()" – lio89 Aug 15 '15 at 00:26
0
$id         = $producto->id;

This won't work because whether the property is not publicly accessible or the Object does not have that property.

The Work object has description, but it's private, so you'd have to create a getter function to echo it.

e.g

echo $producto->getDescription();

Diego Fu
  • 410
  • 2
  • 10