【发布时间】:2019-03-29 10:12:50
【问题描述】:
我使用 GoogleChartsBundle 并且我想恢复我的数据库中的数据,例如,我想查看每个类别的候选人数量 Like:
$pieChart = new PieChart();
$pieChart->getData()->setArrayToDataTable(
[['Categorie', 'Number'],
['Categorie Web', 11],
['Categorie Mobile', 2]
]
);
这是工作 但我想在动态中添加这个(更多类别)我已经在 controller 中尝试过这个但没有工作: 更新
$data = null;
foreach($categories as $categorie)
{
$data[] = array(
$categorie->getTitre(), count($categorie->getCandidats()),
);
}
return $this->json($data);
$pieChart = new PieChart();
$pieChart->getData()->setArrayToDataTable(
[$data]
);
PS : $data 包含 [["Web",1],["Mobile",1]],但在图表中我看不到数据
这是实体类别和实体候选的示例
Entity Categorie
class Categorie
{
//...
/**
* @ORM\Column(type="string", length=255)
*/
private $titre;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Candidat", mappedBy="categorie")
*/
private $candidats;
public function __toString()
{
return $this->getTitre();
}
/**
* @return Collection|Candidat[]
*/
public function getCandidats(): Collection
{
return $this->candidats;
}
//... getter 和 setter }
实体候选
班级候选人 {
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Categorie", inversedBy="candidats")
*/
private $categorie;
}
【问题讨论】:
-
你试过你的数据库里有没有数据吗?
$categories和$categories->getCandidats()究竟包含什么? -
categories 返回所有类别和categories->getCandidats() 返回所有candidats 类别,我添加了count(categories->getCandidats())
-
那究竟包含什么?数据格式是否与以前相同?
-
我已经更新了我的问题查看部分控制器
标签: php mysql symfony google-visualization symfony4