简体   繁体   English

如何在symfony中创建自定义查询?

[英]How to create a custom query in symfony?

I need to make a custom query with inner join and display the name instead of the id of that table 我需要使用内部联接进行自定义查询并显示名称而不是该表的id

public function showAction($id)
{
    $em = $this->getDoctrine()->getManager();

    $entity = $em->getRepository('chriscrudBundle:BpCrpCard')->find($id);

    if (!$entity) {
        throw $this->createNotFoundException('Unable to find BpCrpCard entity.');
    }

    $deleteForm = $this->createDeleteForm($id);

    return $this->render('chriscrudBundle:BpCrpCard:show.html.twig', array(
        'entity'      => $entity,
        'delete_form' => $deleteForm->createView(),
    ));
}
$qb->select('bp.name')
->innerJoin('bp.phones', 'p', 'WITH', 'p.id'  =           'bp.id' );

You can create some custom queries in your repositories thanks to DQL: 您可以通过DQL在您的存储库中创建一些自定义查询:
http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/dql-doctrine-query-language.html http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/dql-doctrine-query-language.html

You can also do raw SQL this way: 您也可以这样做原始SQL:

$em = $this->getDoctrine()->getManager();
$connection = $em->getConnection();
$statement = $connection->prepare("SELECT foo WHERE bar = :id");
$statement->bindValue('id', 1);
$statement->execute();
$results = $statement->fetchAll();

Doctrine creates some magic getters on your repository too, builded that way : findByColumneName(param) or findOneByColumnName(param) Doctrine也会在您的存储库中创建一些魔法getter,以这种方式findByColumneName(param)findByColumneName(param)findOneByColumnName(param)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM