How to get the first row of table in yii2 with where condition?

Active3 hr before
Viewed126 times

6 Answers


How does the Bladesinging wizard's Extra Attack feature interact with the additional Attack action from the Haste spell? , Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers , Reconnecting with a previous professor then asking right away for a reference letter , 2 strange .. 3 upvote for thsi question and 3 upvote for the answer of the same user ... and nothing for the right answer for another user ..very strange ... – ScaisEdge Feb 2 '18 at 14:01

Yes i got my answer.Use Limit there

(new Query()) -
> select('*') -
   > from('stock_in') -
   > where("user_id = '$request_user_id'") -
   > andWhere("product_id = '$product_id'") -
   > andWhere("remaining_quantity > '0'") -
   > limit('1') -
   > one();
load more v

More Learn Books Resources Develop Download Yii Report an Issue Report a Security Issue Contribute to Yii About What is Yii? Release Cycle News License Team Official logo,how can i get the first row of result? Below is my code which is giving me an error like this

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

   WHERE = (SELECT dependency FROM a_actions WHERE action = '{$action}') AND a_modules.module_status = 1 ";

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

      > queryAll();


   $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();
load more v

Working with Relational Data,Accessing Relational Data,Joining with Relations,Note: Be very careful when calling deleteAll() because it may totally erase all data from your table if you make a mistake in specifying the condition.

Warning: If you need to pass user input to these methods, make sure the input value is scalar or in case of array condition, make sure the array structure can not be changed from the outside:

// yii\web\Controller ensures that $id is scalar
function actionView($id) {
   $model = Post::findOne($id);
   // ...

// explicitly specifying the column to search, passing a scalar or array here will always result in finding a single record
$model = Post::findOne(['id' => Yii::$app - > request - > get('id')]);

// do NOT use the following code! it is possible to inject an array condition to filter by arbitrary column values!
$model = Post::findOne(Yii::$app - > request - > get('id'));
load more v

Retrieve the single record from the database based on condition.,Select single column based on id.,Select single columns record from the database based on condition.,Find single record based on id.

Find single record based on id.

$model = User::findOne($id);
load more v

Yes, i have no rows in table matching that condition and i dont get null value as result,Table has couple of hundred rows and all post_code fields are in format "LV-\d{4}",Do you mean you have no record in post_office table with post_code field equals to 0 but getting a record instead of null?,If you do ActiveRecord::findOne() passing array with 1 value pair where key isn't primary key and value is (bool)false or (int)0 result will be 1st row in table instead of null

$postOffice = PostOffice::findOne([
   'post_code' => false

$postOffice = PostOffice::findOne([
   'post_code' => 0
load more v

$user = User::whereEmail($email) - > first();

Other "first-table" queries related to "How to get the first row of table in yii2 with where condition?"