Tutorial by Examples

Post::find()->all(); // SELECT * FROM post or the shorthand (Returns an active record model instance by a primary key or an array of column values.) Post::findAll(condition); returns an array of ActiveRecord instances. Find All with where Condition $model = User::find() ->w...
OPERATORS $postsGreaterThan = Post::find()->where(['>', 'created_at', '2016-01-25'])->all(); // SELECT * FROM post WHERE created_at > '2016-01-25' $postsLessThan = Post::find()->where(['<', 'created_at', '2016-01-25'])->all(); // SELECT * FROM post WHERE created_at < '2...
<?php namespace models; use yii\db\ActiveRecord; use yii\behaviors\TimestampBehavior; class Post extends ActiveRecord { public static function tableName() { return 'post'; } public function rules() { ...
$customer = Customer::findOne(10); or $customer = Customer::find()->where(['id' => 10])->one(); or $customer = Customer::find()->select('name,age')->where(['id' => 10])->one(); or $customer = Customer::findOne(['age' => 30, 'status' => 1]); or $customer = C...
Find single record based on id. $model = User::findOne($id); Select single column based on id. $model = User::findOne($id)->name; Retrieve the single record from the database based on condition. $model = User::find()->one(); // give first record $model = User::find()->where(['i...
Example: Customers who can create a post. Each customer can create multiple posts. As soon as customer creates the post, post will be under administrators review. Now we have to fetch the customers list who have all active posts by using sub-query. Note: If a customer has 5 post, among 5 posts if h...

Page 1 of 1