Quantcast
Channel: Active questions tagged symfony4 - Stack Overflow
Viewing all articles
Browse latest Browse all 3924

How to pass an array to a function with class dependency? [closed]

$
0
0

Before I was using $allMovies = $this->findAll(); to retrieve object data.

array:2908 [
  0 => App\Entity\Movie {#450
    -id: 39
    -title: "ozcMpVxIWJ"
    -count: "0"
  }
  1 => App\Entity\Movie {#453
    -id: 115
    -title: "sfFcUlnEiV"
    -count: "0"
  }

Now I am trying createQueryBuilder() to get array data something like this after my query.

array:10 [
    0 => array:3 [
      "id" => 39
      "title" => "ozcMpVxIWJ""count" => "10"
    ]
    1 => array:3 [
      "id" => 115
      "title" => "sfFcUlnEiV""count" => "10"
    ]
    2 => array:3 [
      "id" => 215
      "title" => "dVrinJNPpC""count" => "10"
    ]

From this data, I need to add some more logic.

With my previous code, everything was running fine. But once I change to createQueryBuilder I am getting problem. Inside the loop, I have called a function with dependency injection of Entity Movie itself.

$moviesArray = array();

foreach ($movies as $movie) {
    $movie = $this->transform($movie);
    .....

}

public function transform(Movie $movie)
{
    return array(
        'id'    => (int) $movie->getId(),
        'title' => (string) $movie->getTitle(),
        'count' => (int) $movie->getCount()
    );
}

Since my data is array, how can I pass to this function and get the same output?


Viewing all articles
Browse latest Browse all 3924

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>