Bloodhound
Bloodhound

Reputation: 2966

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

how can i get the first row of result? Below is my code which is giving me an error like this 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 has value

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 ) )

in Yii1 i would have used

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

Upvotes: 0

Views: 3528

Answers (1)

Insane Skull
Insane Skull

Reputation: 9358

You can use queryOne()

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

QueryOne()

Upvotes: 5

Related Questions