将关联数组从 Symfony 控制器传递到 Twig 和 JavaScript
Pass Associative Array from Symfony Controller to Twig and JavaScript
所以,我有 2 个数组,在确保它们的长度相同后,我将它们合并为一个数组。
//Fetching Ratings for All Coaches & Making a Key, Value Associative Array with Coach id as Key and Rating as Value
$coaches = $coachRepository->findAll();
$rating = $coachRepository->findRatingByCoach();
//Array of coach IDs
$id_array = array();
foreach ($coaches as $c){
$id_array[] = $c->getId();
}
$combined = array_combine($id_array, $rating);
现在我想将这个键值数组传递给 Twig 模板和 JavaScript。我目前正在这样做
return $this->render('...', [
..., 'combined'=>$combined
]);
我正在访问 Twig 中的数组
{% for key, value in combined %}
{{ key }} - {{ value }}
{% endfor %}
我遇到了这个错误。
Object of class App\Entity\Coach could not be converted to string
查看 Stack Trace,我可以看到组合的数组以这种形式传递。
'combined' => array(object(Coach))
经过一些调试后发现,我认为问题出在 $id_array
上,而实际上是 $rating
变量出现了问题。我不确定为什么,但我得到了一个浮点值。
我通过更改获取 $rating 的方式解决了这个问题,现在我不再使用 QueryBuilder 从数据库中获取它,而是从 Coaches 列表中获取它
//Array of coach IDs and array of Ratings
$id_array = array();
$rating_array = array();
foreach ($coaches as $c){
$id_array[] = $c->getId();
$rating_array [] = $c->getRating();
}
即使我设置数组
经过一些调试后发现,我认为问题出在 $id_array 上,而实际上是 $rating 变量出现了问题。我不确定为什么,但我得到了一个浮点值。我通过更改获取 $rating 的方式解决了这个问题,现在我不是使用 QueryBuilder 从数据库中获取它,而是从 Coaches 列表中获取它
//Fetching Ratings for All Coaches & Making a Key, Value Associative Array with Coach as Key and Rating as Value
$coaches = $coachRepository->findAll();
//Array of coach IDs and array of Ratings
$id_array = array();
$rating_array = array();
foreach ($coaches as $c){
$id_array[] = $c->getId();
$rating_array []= $c->getRating();
}
$this->debug_to_console($id_array[0]);
$combined = array_combine($id_array, $rating_array);
我仍然在 twig 中以相同的方式访问数组
所以,我有 2 个数组,在确保它们的长度相同后,我将它们合并为一个数组。
//Fetching Ratings for All Coaches & Making a Key, Value Associative Array with Coach id as Key and Rating as Value
$coaches = $coachRepository->findAll();
$rating = $coachRepository->findRatingByCoach();
//Array of coach IDs
$id_array = array();
foreach ($coaches as $c){
$id_array[] = $c->getId();
}
$combined = array_combine($id_array, $rating);
现在我想将这个键值数组传递给 Twig 模板和 JavaScript。我目前正在这样做
return $this->render('...', [
..., 'combined'=>$combined
]);
我正在访问 Twig 中的数组
{% for key, value in combined %}
{{ key }} - {{ value }}
{% endfor %}
我遇到了这个错误。
Object of class App\Entity\Coach could not be converted to string
查看 Stack Trace,我可以看到组合的数组以这种形式传递。
'combined' => array(object(Coach))
经过一些调试后发现,我认为问题出在 $id_array
上,而实际上是 $rating
变量出现了问题。我不确定为什么,但我得到了一个浮点值。
我通过更改获取 $rating 的方式解决了这个问题,现在我不再使用 QueryBuilder 从数据库中获取它,而是从 Coaches 列表中获取它
//Array of coach IDs and array of Ratings
$id_array = array();
$rating_array = array();
foreach ($coaches as $c){
$id_array[] = $c->getId();
$rating_array [] = $c->getRating();
}
即使我设置数组
经过一些调试后发现,我认为问题出在 $id_array 上,而实际上是 $rating 变量出现了问题。我不确定为什么,但我得到了一个浮点值。我通过更改获取 $rating 的方式解决了这个问题,现在我不是使用 QueryBuilder 从数据库中获取它,而是从 Coaches 列表中获取它
//Fetching Ratings for All Coaches & Making a Key, Value Associative Array with Coach as Key and Rating as Value
$coaches = $coachRepository->findAll();
//Array of coach IDs and array of Ratings
$id_array = array();
$rating_array = array();
foreach ($coaches as $c){
$id_array[] = $c->getId();
$rating_array []= $c->getRating();
}
$this->debug_to_console($id_array[0]);
$combined = array_combine($id_array, $rating_array);
我仍然在 twig 中以相同的方式访问数组