如何在Yii2中获得结果的第一行? (queryRow等效)

时间:2015-11-30 11:45:54

标签: yii2 yii2-advanced-app

如何获得第一行结果?以下是我的代码,它给出了一个像Undefined index: module

这样的错误
if ( substr( $action, 0, 4 ) === "stl_" )
            {
                $query  = "SELECT * FROM a_actions LEFT JOIN a_modules ON ( a_modules.id=a_actions.module_id )
                        WHERE a_actions.id=(SELECT dependency FROM a_actions WHERE action='{$action}') AND a_modules.module_status = 1 ";
                $action = \Yii::$app->db->createCommand( $query )
                                        ->queryAll();
//print_r($action);die();
                $module = $action[ 'module' ];
                $action = $action[ 'action' ];
            }

$ action有价值

Array ( [0] => Array ( [id] => 7 [module_id] => 7 [action] => index [label] => Members [dependency] => [created_by] => [created_at] => [updated_by] => [updated_at] => [is_deleted] => 0 [module] => members [module_name] => [module_status] => 1 ) )
在Yii1中我会使用

$action = \Yii::$app->db->createCommand( $query )
                                        ->queryRow();

1 个答案:

答案 0 :(得分:4)

您可以使用queryOne()

\Yii::$app->db->createCommand( $query )
->queryOne();

QueryOne()