dongtuan8547 2016-12-19 14:23
浏览 101
已采纳

ActiveQuery to array(asArray)

What's wrong with that query?

$numery = Number::find()
        ->select('number.phone_number')
        ->from('number')
        ->leftJoin('ordered_number', ['ordered_number.number_id' => 'number.phone_number'])
        ->where(['ordered_number.order_id' => 123])
        ->asArray();

I'm not sure if asArray is used in good way and also if I have to end my query with one() or all() because all the examples I've seen have it.

I changed it to:

        $numery = Number::find()
                ->select('number.phone_number')
                ->from('number, ordered_number')
                ->where(['ordered_number.number_id' => 'number.id'])
                ->andWhere(['ordered_number.order_id' => 123])
                ->asArray()
                ->all();

But still I get NULL instead of 4-element array. When I delete the "where" part I get all the numbers I have, so I suppose there is a problem somewhere there. The query that works in raw MySQL and that I want to use looks like this:

SELECT number.phone_number 
FROM number, ordered_number 
WHERE ordered_number.number_id=number.id 
AND ordered_number.order_id=123
  • 写回答

2条回答 默认 最新

  • dtmsaqtly798322992 2016-12-20 09:07
    关注

    About the first one:

    $numery = Number::find()
        ->select('number.phone_number')
        ->from('number')
        ->leftJoin('ordered_number', ['ordered_number.number_id' => 'number.phone_number'])
        ->where(['ordered_number.order_id' => 123])
        ->asArray();
    

    You need to end it with one() or all() depending on the number of results you are expecting to get.
    one() gives you single DB row, all() gives you array of DB rows.
    Without asArray() it gives you object (or array of objects in case of all()) - with asArray() it gives you array (or array of arrays) obviously.

    One more thing - when you use Model::find() static method you don't have to set from() as it will automatically use the underlying DB table for the model.

    Update: leftJoin is wrong - it should be

    ->leftJoin('ordered_number', 'ordered_number.number_id = number.phone_number')
    

    About second one:

    $numery = Number::find()
                ->select('number.phone_number')
                ->from('number, ordered_number')
                ->where(['ordered_number.number_id' => 'number.id'])
                ->andWhere(['ordered_number.order_id' => 123])
                ->asArray()
                ->all();
    

    You are adding two tables in from(). This method states that in that case yu should use array instead of string:

    ->from(['number', 'ordered_number'])
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器