如何使用多个联接进行查询?

时间:2019-01-28 15:15:04

标签: php orm doctrine-orm doctrine

我正在尝试对三个实体进行查询。这是数据库模式:https://imgur.com/a/yUYde0C

箭头所示,它是单向关系。

var message = threads[x].getMessages()[0];
var saveFolder = getFolder(driveFolderID,message);//this is valid folder
var att = message.getAttachments();
Logger.log(att[0].getContentType());  //this returns 'pdf'
file = saveFolder.createFile(att[0]);

我只想让所有部分都连接到当前需求(在会话变量'req_id'中给出。)

没有任何结果。

感谢您的帮助。 :)

编辑:这是错误消息(很抱歉,我不知道如何格式化这些错误消息):

$dql = 'SELECT r,m,s 
        FROM Requirement r 
            JOIN r.Manufacturer m 
            JOIN r.Section s 
        WHERE r.id=' . $_SESSION['req_id'];

echo $dql;

$query = $entityManager->createQuery($dql);
$req = $query->getResult();

foreach ($req->getSections() as $section) {

    echo $section->getName() . '<br>';
}

1 个答案:

答案 0 :(得分:0)

正如@ArthurShveida所说,联接必须类似于属性中的var名称,而不是类名称。

因此此DQL查询有效:

$dql = 'SELECT r,m,s 
        FROM Requirement r 
             JOIN r.manufacturer m 
             JOIN r.sections s 
        WHERE r.id=' . $_SESSION['req_id'];

因为这是类Requirement

class Requirement
{
    /**
     * @Id @Column(type="integer") @GeneratedValue
     **/
    protected $id;

    /* some other attributes */

        /**
     * @ManyToOne(targetEntity="Manufacturer")
     * @JoinColumn(name="manufacturer_id", referencedColumnName="id")
     */
    private $manufacturer;

    /**
     * @ManyToMany(targetEntity="Section")
     * @JoinTable(name="Requirement_Section",
     *      joinColumns={@JoinColumn(name="requirement_id", referencedColumnName="id")},
     *      inverseJoinColumns={@JoinColumn(name="section_id", referencedColumnName="id")}
     *      )
     */
    private $sections;

    /* getter/setter functions */

}

感谢您的帮助!