简体   繁体   中英

Processing a Symfony/Doctrine result which is an array of Object

I've used the following in my Controller Action

$data = $this->getDoctrine()->getRepository('MyBundle:links')->findAll();

data now holds and array of objects of class "Links" which are as follows

Array
(
    [0] => MyBundle\Entity\links Object
        (
            [id:MyBundle\Entity\links:private] => 2
            [urls:MyCheckerBundle\Entity\links:private] => http://localhost/1.php
        )    
    [1] => MyBundle\Entity\links Object
        (
            [id:MyBundle\Entity\links:private] => 1
            [urls:MyCheckerBundle\Entity\links:private] => http://localhost/2.php
        ))

How do I process this array of objects if I want to access id and urls so that I can display on my page ?

The array is just an array containing the entities of yours.

So what you can do is this:

foreach ($data as $object) {
        // ID variable
        var id = $object->getId()
        var urls = $object->getUrls() // Not sure if the method is called.

What it comes down to; you can just use the methods you have defined in your entities to access the properties of those objects.

probably in twig :

{% for object in data %}

{{ object.id }}
{{ object.url }}

{% endfor %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM