MySQL查询与Doctrine 2和不同的结果

时间:2018-04-16 11:45:55

标签: mysql doctrine-orm doctrine

所以这就是事情。

我使用Doctrine 2.5查询数据库,我想使用Doctrine的createQuery()方法来完成它。原因是Doctrine 2以camelCase方式保存结果,我的列名都带有下划线。由于大多数简单查询在createQuery(甚至是查询构建器)中运行良好,我希望将所有本机MySQL查询移到Doctrine的DQL中(以获取camelCase中的所有结果,以便'标准化结果)。或者你们可以教我如何让Doctrine 2以与列在数据库中相同的方式返回结果... xD

我有以下查询:

$sql = '
     SELECT sp.*, so.user_id
     FROM shop_order so
     INNER JOIN shop_payment sp ON(so.payment_id=sp.id)
     WHERE so.id = '.$orderID;
$stmt = $em->getConnection()->prepare($sql);
$stmt->execute();
$result = $stmt->fetch();

这导致以下结果:

Array
(
    [id] => 14
    [payment_type_id] => 2
    [flag] => boleto_bb
    [tid] => 
    [billet] => 1
    [parcel_quantity] => 
    [cart_name_holder] => 
    [cart_date_holder] => 1999-12-01
    [cart_number_holder] => 0
    [datecreated] => 2017-08-30 14:51:25
    [dateupdate] => 2017-08-30 16:36:18
    [status] => 2
    [user_id] => 16
)

但如果我这样做:

$query = $em->createQuery('
    SELECT sp, so.userId
    FROM ShopOrder so
    INNER JOIN ShopPayment sp WITH (so.paymentId = sp.id)
    WHERE so.id = ' . $orderID);
$result = $query->getArrayResult();

我得到以下结果:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [id] => 14
                    [paymentTypeId] => 2
                    [flag] => boleto_bb
                    [tid] => 
                    [billet] => 1
                    [parcelQuantity] => 
                    [cartNameHolder] => 
                    [cartDateHolder] => DateTime Object
                        (
                            [date] => 1999-12-01 00:00:00.000000
                        )
                    [cartNumberHolder] => 0
                    [datecreated] => DateTime Object
                        (
                            [date] => 2017-08-30 14:51:25.000000
                        )
                    [dateupdate] => DateTime Object
                        (
                            [date] => 2017-08-30 16:36:18.000000
                        )
                    [status] => 2
                )
            [userId] => 16
        )
)

如何从createQuery方法获得第一个结果?

0 个答案:

没有答案