Sonata-project Admin/Controller等文件查询Entity数据和自定义函数配置

(1)Admin文件中查询Entity

$_provinces = $this->container->get('doctrine')->getRepository('JyCommonStoreBundle:Region')->findByRegionType(1);

(2)Controller中查询Entity

$em = $this->getDoctrine()->getManager();
$articles = $em->getRepository('JyCommonStoreBundle:Article')->findByPlatform(2);
$articles = $em->getRepository('JyCommonStoreBundle:Article')->getList();

(3)Repository中查询Entity

$this->getEntityManager()
    ->createQuery("select a.title,a.addtime  from Article a where date=:date and category_id=:category_id")
    ->setParameter('date', $date)
    ->setParameter('category_id', 1)
    ->getResult();

注:以上文件中调用的findByRegionType和getList函数为自定义函数,需单独建立Repository文件并在相应Entity中将其与对应的文件关联起来,如下配置:


/**
 * @ORM\Table(name="xuejieblog.article")
 * @ORM\Entity(repositoryClass="ArticleRepository")
 */



by 雪洁 2014-11-25 05:10:42 2206 views
我来说几句

相关文章