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

时间:2017-03-19 08:53:56

标签: php symfony

我需要使用内部联接创建自定义查询并显示名称而不是该表的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(),
    ));
}

2 个答案:

答案 0 :(得分:0)

$qb->select('bp.name')
->innerJoin('bp.phones', 'p', 'WITH', 'p.id'  =           'bp.id' );

答案 1 :(得分:0)

由于DQL,您可以在您的存储库中创建一些自定义查询:
http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/dql-doctrine-query-language.html

您也可以这样做原始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也会在您的存储库中创建一些魔法助手,以这样的方式构建:findByColumneName(param)findOneByColumnName(param)