What’s the difference between Yii 2 advanced application and basic?

The following table shows the similarities and differences between the basic and advanced templates: Source: https://github.com/yiisoft/yii2-app-advanced/blob/master/docs/guide/start-comparison.md As you can see, the main differences are: Advanced template supports front- and back-end apps; Advanced template is ready to use User model; Advanced template supports user signup and password restore.

how to use not null condition in YII2

You can use the not operator combined with the fields that should not be null to generate a IS NOT NULL SQL statement. Like this: $query = new Query; $query->select(‘ID, City,State,StudentName’) ->from(‘student’) ->where([‘IsActive’ => 1]) ->andWhere([‘not’, [‘City’ => null]]) ->andWhere([‘not’, [‘State’ => null]]) ->orderBy([‘rand()’ => SORT_DESC]) ->limit(10); Also check the examples in the documentation.

Disable CSRF validation for individual actions in Yii2

For the specific controller / actions you can disable CSRF validation like so: use Yii; … Yii::$app->controller->enableCsrfValidation = false; Or inside a controller: $this->enableCsrfValidation = false; Take a look at $enableCsrfValidation property of yii\web\Controller. Update: Here is some specification. If you want to disable CSRF validation for individual action(s) you need to do it in … Read more

How to get root directory in yii2

Open file D:\wamp\www\yiistore2\common\config\params-local.php Paste below code before return Yii::setAlias(‘@anyname’, realpath(dirname(__FILE__).’/../../’)); After inserting above code in params-local.php file your file should look like this. Yii::setAlias(‘@anyname’, realpath(dirname(__FILE__).’/../../’)); return [ ]; Now to get path of your root (in my case its D:\wamp\www\yiistore2) directory you can use below code in any php file. echo Yii::getAlias(‘@anyname’);

Yii2 redirect to previous page

You could use Yii::$app->request->referrer which returns the last page the user was on. Usage is straightforward: return $this->redirect(Yii::$app->request->referrer); You need also take into account that referrer can be null: return $this->redirect(Yii::$app->request->referrer ?: Yii::$app->homeUrl); See the docs.

Performing raw SQL queries in Yii2?

You can execute raw sql like this $connection = Yii::$app->getDb(); $command = $connection->createCommand(” SELECT SUM(bets.balance_return) AS total_win , bets.user_id , users.user_name , users.user_status FROM bets INNER JOIN users ON bets.user_id = users.id WHERE users.user_status=”verified” AND bets.date_time > :start_date GROUP BY bets.user_id ORDER BY total_win DESC”, [‘:start_date’ => ‘1970-01-01’]); $result = $command->queryAll(); I recommend reading: http://www.yiiframework.com/doc-2.0/yii-db-connection.html#createCommand()-detail … Read more

How to pass param from controller to layout in YII2

yii\base\View has special $params property. For example it’s used for building breadcrumbs in default generated CRUD code templates with Gii. You can set it like this before rendering: use Yii; Yii::$app->view->params[‘customParam’] = ‘customValue’; Inside a controller you can set it like this: $this->view->params[‘customParam’] = ‘customValue’; Then it will be available in views (including main layout): … Read more

Log the actual SQL query using ActiveRecord with Yii2?

Method 1 With relations that return yii\db\ActiveQuery instance it’s possible to extract the raw SQL query directly in code for example with var_dump(). For example if we have user relation: /** * @return \yii\db\ActiveQuery */ public function getUser() { return $this->hasOne(User::className(), [‘id’ => ‘user_id’]); } You can then var_dump() the raw SQL like that: var_dump($model->getUser()->prepare(Yii::$app->db->queryBuilder)->createCommand()->rawSql); … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)