Php 从MySQL查询结果创建关联数组-Doctrine2
我正在做一个Symfony2项目,我需要为日历使用一个插件,该插件需要传递一些值,如下所示: JS 可以通过ajax按如下方式传递这些事件数据: JS 到目前为止还不错。但现在我需要从数据库的下表中获取数据:Php 从MySQL查询结果创建关联数组-Doctrine2,php,mysql,symfony,doctrine-orm,Php,Mysql,Symfony,Doctrine Orm,我正在做一个Symfony2项目,我需要为日历使用一个插件,该插件需要传递一些值,如下所示: JS 可以通过ajax按如下方式传递这些事件数据: JS 到目前为止还不错。但现在我需要从数据库的下表中获取数据: id title description datetime category hour 1 Example Title 1 Description 1. 2016-12-21 00:00
id title description datetime category hour
1 Example Title 1 Description 1. 2016-12-21 00:00:00 general all day
2 Example Title 2 Description 2. 2016-12-21 00:00:00 general 09:00
在我的Repository类中,我使用以下代码进行查询:
public function findEvents($category) //$category has the searched value passed from the controller.
{
return $this->getEntityManager()->createQuery(
'SELECT e FROM BackendBundle:Events e WHERE e.category=:category ORDER BY e.datetime ASC, e.hour ASC')
->setParameters(array(
'category' => $category))
->getResult();
}
但现在我不知道如何才能获得仅与某些列的值关联的数组,如以下示例:
$arr = array(array("title" => 'Example Title 1',"description" => "Description 1.","datetime"=>new \DateTime("now")),
array("title" => 'Example Title 2',"description" => "Description 1.","datetime"=>new \DateTime("now"))
);
最后感谢@Garry,我能够用以下代码解决我的问题:
public function findEvents($category) //$category has the searched value passed from the controller.
{
return $this->getEntityManager()->createQuery(
'SELECT CONCAT(CONCAT(e.title, ' '), e.date) AS title, e.description, e.datetime FROM BackendBundle:Events e WHERE e.category=:category ORDER BY e.datetime ASC, e.hour ASC')
->setParameters(array(
'category' => $category))
->getResult();
}
除了只获取我需要的三列的值之外,我还能够将日期添加到标题中(我在问题的前面没有指出)。最后感谢@Garry,我能够用以下代码解决我的问题:
public function findEvents($category) //$category has the searched value passed from the controller.
{
return $this->getEntityManager()->createQuery(
'SELECT CONCAT(CONCAT(e.title, ' '), e.date) AS title, e.description, e.datetime FROM BackendBundle:Events e WHERE e.category=:category ORDER BY e.datetime ASC, e.hour ASC')
->setParameters(array(
'category' => $category))
->getResult();
}
除了只获取我需要的三列的值之外,我还能够将日期添加到标题中(我在前面的问题中没有指出)。使用partial
选择partial e.{title,description,etc}FROM…
您可以尝试return$this->getEntityManager()->createQuery('SELECT e.title,e.description,e.datefrom BackendBundle:Events e WHERE e.category=:category ORDER BY e.date ASC,e.hour ASC')->setParameters(array('category'=>$category))->getResult(条令\ORM\Query::水合物数组)
谢谢@Garry,我是通过添加你告诉我的内容得到这个结果的,除了原则\ORM\Query::hydrome\u数组
出现错误(我不知道是什么原因)。我之前没有提到的问题是,我需要将小时列中的文本添加到标题列值的末尾(例如:Title1.| 09:00),所以我想做的是获取所有列,然后对其进行修改并创建修改后的关联数组。我如何添加这些信息?要获取标题和时间,您需要在选择中使用CONCAT,如下所示select e.title,e.description,e.date,CONCAT(CONCAT(e.title',),e.date)作为titledate
。尝试使用\doctor\ORM\Query::HYDRATE\u数组。注意开头的\项。使用部分从…
中选择部分e.{title,description,etc}。您可以尝试返回$this->getEntityManager()->createQuery('SELECT e.title,e.description,e.datefrom BackendBundle:Events e WHERE e.category=:category ORDER BY e.date ASC,e.hour ASC')->setParameters(array('category'=>$category))->getResult(条令\ORM\Query::水合物数组)
谢谢@Garry,我是通过添加你告诉我的内容得到这个结果的,除了原则\ORM\Query::hydrome\u数组
出现错误(我不知道是什么原因)。我之前没有提到的问题是,我需要将小时列中的文本添加到标题列值的末尾(例如:Title1.| 09:00),所以我想做的是获取所有列,然后对其进行修改并创建修改后的关联数组。我如何添加这些信息?要获取标题和时间,您需要在选择中使用CONCAT,如下所示select e.title,e.description,e.date,CONCAT(CONCAT(e.title',),e.date)作为titledate
。尝试使用\doctor\ORM\Query::hydrome\u数组。请注意开头的\。
$arr = array(array("title" => 'Example Title 1',"description" => "Description 1.","datetime"=>new \DateTime("now")),
array("title" => 'Example Title 2',"description" => "Description 1.","datetime"=>new \DateTime("now"))
);
public function findEvents($category) //$category has the searched value passed from the controller.
{
return $this->getEntityManager()->createQuery(
'SELECT CONCAT(CONCAT(e.title, ' '), e.date) AS title, e.description, e.datetime FROM BackendBundle:Events e WHERE e.category=:category ORDER BY e.datetime ASC, e.hour ASC')
->setParameters(array(
'category' => $category))
->getResult();
}