Keutelvocht
Keutelvocht

Reputation: 688

Symfony : Get key value from object doctrine

I have this array of objects with doctrine, how do i scope out one of the values?

This doesnt seem to work for me : todolist[0]->todo_body

todolist
(
    [0] => AppBundle\Entity\Todo Object
    (
        [todo_id:AppBundle\Entity\Todo:private] => 1
        [todo_body:AppBundle\Entity\Todo:private] => Install Symfony
    )

    [1] => AppBundle\Entity\Todo Object
    (
        [todo_id:AppBundle\Entity\Todo:private] => 2
        [todo_body:AppBundle\Entity\Todo:private] => Learn Symfony
    )

    [2] => AppBundle\Entity\Todo Object
    (
        [todo_id:AppBundle\Entity\Todo:private] => 3
        [todo_body:AppBundle\Entity\Todo:private] => Create Controller
    )

    [3] => AppBundle\Entity\Todo Object
    (
        [todo_id:AppBundle\Entity\Todo:private] => 4
        [todo_body:AppBundle\Entity\Todo:private] => Create first page
    )

)

Upvotes: 2

Views: 3036

Answers (2)

Alessandro Minoccheri
Alessandro Minoccheri

Reputation: 35963

Try this, but first be sure to have in your entity the getter getTodoBody:

todolist[0]->getTodoBody()

If you don't have the getter inside TodoEntity add this:

public function getTodoBody() {
    return $this->todo_body;
}

If you need todo_body inside your html template try this:

{{ todo.TodoId }} 

or

{{ todolist[0].todo_body }}

Upvotes: 1

Jakub Matczak
Jakub Matczak

Reputation: 15656

AppBundle\Entity\Todo properties like 'todo_body' are clearly private (as stated in dump that you've provided) and therefore you cannot access them directly from outside with $todo->todo_body.

You need a getter method to access such properties.

You can try with something like $todolist[0]->getTodoBody() if you already have a getter in your AppBundle\Entity\Todo class.

If not, you will need to create it in first place. It would look something like:

public function getTodoBody() {
    return $this->todo_body;
}

And similar for todo_id

Upvotes: 1

Related Questions