3

I have in my controller a function to make a search filter.

The search parameter is obtained from a select field of my twig template.

The selected option is passed to the controller to see where results are with that value.

The query result is returned in JSON format.

public function categoryAction(Request $request){

    $em = $this->getDoctrine()->getManager();

    $category = $request->request->get('category');

    $contentCategory = $em->getRepository('PublicartelAppBundle:Content')->findByCategory($category);

    $filterContent = new JsonResponse();

    $filterContent->setData([
        'category_result' => $contentCategory
    ]);

    return $filterContent;
}

$('#selectCategory').on('change', function () {
    var optionSelect = $(this).val();
    $.post("{{path('playlist_category') }}", {category: optionSelect}, 
    function (filterContent) {
        var result = filterContent.category_result;
        console.log(result);
    }, 'json');
});

This return in console:

[Object, Object, Object]

O: Object

1: Object

2: Object

lenght: 3

The amount of items returned depends on the number of items found with that category.

If I do the following could iterate each of the elements.

for (var content in filterContent.category_result){
    console.log(content, filterContent.category_result[content]);
}

And return in console this:

0 Object {}

1 Object {}

2 Object {}

But I want to access the 'name' property of each object.

How could I say to want to access the object name property , or at least iterate one by one one each?

1
  • you was almost good. try filterContent.category_result[content].name Commented Jul 14, 2015 at 21:23

3 Answers 3

4

You can log filterContent.category_result[content].name if it's the appropriate property.

Sign up to request clarification or add additional context in comments.

4 Comments

category_result is an entity object of Content, and 'name' is a property of Content. But when i make this that you tell me, the console return is: 'undefined'..
Please show me the exact contents of filterContent.category_result
You can see content in the image the reply on the post of 'mmm'.
I don't see such an image. Please paste the contents into a Github gist and share it in the comments here if you would like me to troubleshoot further.
1

The problem is that the findBy repository method return a Doctrine ArrayCollection populated with the entity value. For translate it as a valid JSON you can iterate it and translate in a simple array as follow in the Controller class:

    $contentCategory = ....

    $contents = array();

    foreach($contentCategory  as $elem)
        $contents[] = array(
            'name' => $elem->getName();
            ....
        );


$filterContent = new JsonResponse();

        $filterContent->setData([
        'category_result' => $result
    ]);    

    return $filterContent;

You can also take into account to use a Serializer as http://jmsyst.com/libs/serializer

Hope this help

Comments

1

try this to see where is "name" :

for (var content in filterContent.category_result) {
    console.log(content);
    for (var i in filterContent.category_result[content]) {
        console.log(filterContent.category_result[content][i]);
    }
}

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.