donglu8344812 2017-01-10 10:56
浏览 47

关于关系的WHERE语句

I have two tables : Place and Description

A place can contains 0, 1 or more descriptions.

So in Place entity, I have a descriptions field:

/**
* @ORM\OneToMany(targetEntity="Description", mappedBy="place")
 */
private $descriptions;

And in Description entity I have a place field:

/**
 * @ORM\ManyToOne(targetEntity="Place", inversedBy="descriptions")
 */
private $place;

I would like to use the QueryBuilder to get descriptions based on a Place's field. Something like

SELECT * FROM Description WHERE Place.id = 439483

I guess I should use join, but it also returns Places column. How could I just get descriptions based on a Place condition?

Thanks

EDIT: Here is what I tried:

$em = $this->getEntityManager();
        $placeRepository = $em->getRepository("AppBundle:Place");
        $q = $placeRepository->createQueryBuilder('p')
            ->select("d")
            ->innerJoin("p.descriptions", "d")
            ->where("p.id = 439483");

        $q = $q->getQuery();
        $res = $q->getResult();

        return $res;

But it returns a place and descriptions, I just want descriptions.

  • 写回答

2条回答 默认 最新

  • dongxu7121 2017-01-10 12:39
    关注

    next time do post what you actually tried. But as this is a simple query, here:

    $em = $this->getEntityManager();
                $placeRepository = $em->getRepository("AppBundle:Place");
                $q = $placeRepository->createQueryBuilder('p')
                    ->select("d")
                    ->innerJoin("p.descriptions", "d")
                    ->where("p.id = 439483");
    
                $q = $q->getQuery();
                $res = $q->getResult();
    
                return $res;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据