yii2

Sort and search column when I'm querying with findbysql in yii2

坚强是说给别人听的谎言 提交于 2019-12-10 04:24:54
问题 I'm searching four tables and joined them and got the output I want. But unable to sort or filter the output. Please tell me how I can search it by district or a sell range or collection range. PartiesSearch model is - <?php namespace frontend\modules\districtreport\models; use Yii; use yii\base\Model; use yii\data\ActiveDataProvider; use frontend\modules\districtreport\models\Parties; use frontend\modules\districtreport\models\Bills; use frontend\modules\districtreport\models\Payment; use

How to detect if I am in 'console' mode

一个人想着一个人 提交于 2019-12-10 02:56:20
问题 I am writing an app that runs from the browser. However, some model functions are also called from the Yii2 console. Therefore, I am getting errors when trying to access variables that are set in the GUI. Is it possible to tell which mode I am in? Is there some environment variable automatically set, or should I just set some session variable in the console app to indicate the state? 回答1: You can use if (Yii::$app instanceof \yii\console\Application) for console, and if (Yii::$app instanceof

Yii2 - Include JS Script Only For Specific View

落花浮王杯 提交于 2019-12-10 02:31:01
问题 I want to include my js script specific.js for a specific view having action id specific-action-id . I don't want the specific.js to be included in every page. For including a js script for the whole site I normally have to edit AppAsset.php . For this purpose, what should I do? 回答1: You should simply register this js file in your view, e.g. : $this->registerJsFile('@web/js/specific.js'); Read more : http://www.yiiframework.com/doc-2.0/guide-output-client-scripts.html#registering-scripts 回答2:

Yii2: updateAll with multi conditions

北战南征 提交于 2019-12-10 02:24:34
问题 How to update all records by my condition? (my code is not work) $condition[] = ['>', 'position', $old_position]; $condition[] = ['<=', 'position', $new_position]; $condition[] = ['in', 'id', $ids]; Video::updateAll([ 'position' => new \yii\db\Expression('@a := @a + 1'), ], $condition); 回答1: You forgot the operator, you should simply try : $condition = ['and', ['>', 'position', $old_position], ['<=', 'position', $new_position], ['in', 'id', $ids], ]; Read more : http://www.yiiframework.com

How to get users of a specefic role in Yii2 and DbManager?

ぃ、小莉子 提交于 2019-12-10 02:16:12
问题 How to get users of a specefic role in Yii2 and DbManager in RBAC? Please introduce some API for user management and role management. I searched and read Yii2 guide but I didn't find any solution. 回答1: I wrote this function which can be added to an User class. /** * Finds all users by assignment role * * @param \yii\rbac\Role $role * @return static|null */ public static function findByRole($role) { return static::find() ->join('LEFT JOIN','auth_assignment','auth_assignment.user_id = id') -

criteria Active data provider in Yii 2

六眼飞鱼酱① 提交于 2019-12-10 02:00:50
问题 I'm trying to display database for each user in descending date order using data provider this works for Yii 1 at controller : $DataProvider = new CActiveDataProvider('ModelName', array( 'criteria' => array( 'condition' => 'user_id=' . Yii::app()->user->id, 'order' => 'submitted_dt DESC', ), 'pagination' => array( 'pageSize' => 20, ), )); i try this in Yii 2 : $DataProvider = new ActiveDataProvider([ 'query' => ModelName::find(), 'criteria' => [ 'condition' => 'user_id=' . Yii::$app->user-

Difference between main.php and main-local.php in the config folder?

可紊 提交于 2019-12-10 01:52:55
问题 Can anyone please explain the difference between main.php and main-local.php in the config folder? Why one has the -local , and the other doesn't? 回答1: You can read Configuration and environments section of Advanced Application Template README to understand this: Typically environment contains application bootstrap files such as index.php and config files suffixed with -local.php. These are added to .gitignore and never added to source code repository. In order to avoid duplication

Yii2 GridView hide column conditionally

馋奶兔 提交于 2019-12-10 01:52:41
问题 I am displaying some columns in Yii2 GridView widget, 'Executive Name' is one of those but it should be displayed only when a Supervisor is logged in not when Executive logged in. When I am hard coding visible to zero it is not displaying as follows: [ 'label' => 'Executive Name', 'attribute' => 'cs.first_name', 'visible' => '0', ], But I want to display it conditionally something like this: [ 'label' => 'Executive Name', 'attribute' => 'cs.first_name', 'visible' => function ($data) { if (

yii2 ActiveRecord Find OrderBy with calculation

自作多情 提交于 2019-12-10 01:21:43
问题 Trying to fetch a description from my database. The query returns the result but I would like to order the result to only show the one with the highest vote. The vote should be calculated by the upvoted column subtracted by the downvoted column $description = UnitDescription::find() ->where(['id_unit' => $model->id]) ->orderBy([ 'upvoted - downvoted' => SORT_DESC //Need this line to be fixed ]) ->one(); I was hoping someone might have a way to write this part of the query - Thanks 回答1: You

How to make field enum migration yii2

独自空忆成欢 提交于 2019-12-10 01:00:46
问题 I make field ENUM and the result is error when I use yii migrate/up on CMD windows. public function up() { $tableOptions = null; if ($this->db->driverName === 'mysql') { $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB'; } $this->createTable('{{%user_social_media}}', [ 'social_media' => $this->ENUM('facebook', 'google', 'twitter', 'github'), 'id' => $this->primaryKey(), 'username' => $this->string(), 'user_id' => $this->integer(11), 'created_at' => $this->integer(11),