1

I wrote an api call in my Symfony project that returns all fields from my entity with the query defined below..

Now, I need to define just three fields like 'id', 'name', 'value' and to pull values from that fields that are currently stored in a database.

public function getChartData() {
    $myResults = $this->getMyRepository()
                       ->createQueryBuilder('s')
                       ->groupBy('s.totalCollected')
                       ->orderBy('s.id', 'ASC')
                       ->getQuery()
                       ->getArrayResult();

    $result = array("data" => array());

    foreach ($myResults as $myResult => $label) {
            $result['data'][$schoolResult] =  $label["id"];
            $result['data'][$schoolResult] =  $label["name"];
            $result['data'][$schoolResult] =  $label["totalCollected"];
    }
}

The problem is it return just totalCollected field.

One of errors are Call to a member function getId() on array and so on, and I can't figure out a way to pull data from db...

4
  • 1
    You are returning an array of data from the DB. So in order to access it you will need to use some loop for foreach or while or use 0 as an access to a first item in that array. If you are using 0 than something like this might help $myResults[0]->getId(), Commented Dec 25, 2018 at 14:54
  • 1
    so, you cannot use array as an object, I assume you get an array back with single/multiple rows, I think you should iterate it and then, get your fields from each row and a single row should match your entity Commented Dec 25, 2018 at 15:00
  • 1
    Can you share the content of $myResults and $schoolResults? Commented Dec 25, 2018 at 16:26
  • 1
    I edited the post it actually the same. I get response just for last array of values. Just totalCollected is returned, not Id and Name.. @DavidWinder Commented Dec 26, 2018 at 8:29

1 Answer 1

1

I cannot see in your code where $schoolResult come from but lets guess it string key of some sort.

Notice you trying to set 3 value on the same key so only the last one remains.

Look at:

$a = array();
$a["key"] = 4;
$a["key"] = 6;  

It is simple to see that $a["key"] will contains 6 and not 4 or both.

When you do:

foreach ($myResults as $myResult => $label) {
    $result['data'][$schoolResult] =  $label["id"];
    $result['data'][$schoolResult] =  $label["name"];
    $result['data'][$schoolResult] =  $label["totalCollected"];
}

You override the data in $result['data'][$schoolResult] therefor only try totalCollected is there as the last one to set.

In order to fix that you can use:

foreach ($myResults as $myResult => $label) {
    $result['data'][$schoolResult]["id] =  $label["id"];
    $result['data'][$schoolResult]["name"] =  $label["name"];
    $result['data'][$schoolResult]["totalCollected"] =  $label["totalCollected"];
}

Hope that helps!

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

1 Comment

That's it! Thanks a lot! @DavidWinder

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.