0

I know the problem is simple, but I can't find how to resolve it. I need to retrieve a value from a variable, but in vain:

I have the following controller:

  public function followersAction()
        {
            $em = $this->getDoctrine()->getManager();

            $usr= $this->get('security.context')->getToken()->getUser();

            $query = $em->createQuery(
                    'SELECT p
                    FROM TESTBlogBundle:Follow p               
                    WHERE p.followed = :me'
                    )->setParameter('me', $usr->getId());
                    $followers = $query->getResult();

            return array(
                'entities' => $followers
            );
        }

Inside another controller, I am using:

$myfollowers=$this->followersAction();

when I var_dump, I have:

array(3) {
  ["entities"]=>
  array(1) {
    [0]=>
    object(TEST\Bundle\BlogBundle\Entity\Follow)#364 (3) {
      ["id":"TEST\Bundle\BlogBundle\Entity\Follow":private]=>
      int(1)
      ["follower"]=>
      int(2)
      ["followed"]=>
      int(1)
    }
  }
  ["current_user"]=>
  int(1)
  ["followers_count"]=>
  int(1)
}

I need to get the value of property :follower.

foreach($myfollowers as $myfollower)
    {
        echo '<pre>';

        var_dump($myfollower[0]->follower);
        var_dump($myfollower[0]->getFollower());
        var_dump($myfollower->getFollower());

        echo '</pre>';


    }

I am getting errors like: Trying to get property of non-object, Call to a member function getFollower() on a non-object.

Your help is appreciated.

1 Answer 1

1

You are trying to loop through the wrong scope. $myfollowers['entities'] contains the right ones, so you could use this as a starting point for your loop.

The following should do the trick.

foreach($myfollowers['entities'] as $myfollower)
{
    echo '<pre>';
    var_dump($myfollower->getFollower());
    echo '</pre>';    
}
Sign up to request clarification or add additional context in comments.

Comments

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.