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

How to get the first row of a result in Yii2? (queryRow Equivalent)

如何获得第一行结果?下面是我的代码,它给我这样的错误 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();

您可以使用queryOne()

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

QueryOne()