yii2-advanced-app

Yii2 isGuest giving exception in console application

ぃ、小莉子 提交于 2019-12-23 10:56:08
问题 In console application when I used Yii::$app->user->isGuest it is giving the below exception: Exception 'yii\base\UnknownPropertyException' with message 'Getting unknown prop erty: yii\console\Application::user' I even tried adding the user in components array in config file. But it didn't worked. Any idea what am I doing wrong? 回答1: In Console application Yii->$app->user does not exist. So, you need to configure user component in config\console.php . like as, config\console.php 'components'

Connect yii2 with mongodb

随声附和 提交于 2019-12-23 10:54:25
问题 I am new to Yii2. Can anyone tell me how to configure YII2 with mongodb and how to establish connection between YII2 and mongodb? I have tried to download the mongodb package from git hub and tried to run the following command php composer.phar require --prefer-dist yiisoft/yii2-mongodb "*" In the command prompt inside the root folder where I have installed Yii2 but I get the following error Your requirements could not be resolved to an installable set of packages. Problem 1 - yiisoft/yii2 2

Yii2: ActiveForm field numeric, length => 8

自闭症网瘾萝莉.ら 提交于 2019-12-23 09:19:47
问题 I'm trying to make yii2 to validate my ActiveForm field that should be numeric and of 8 characters long. Following is what I tried in the default LoginForm model of yii2/advanced/backend , but unfortunately the isNumeric validator simply doesn't kick in: public function rules() { return [ // username and password are both required [['username', 'password'], 'required'], // username should be numeric ['username', 'isNumeric'], // username should be numeric ['username', 'string', 'length'=>8],

Declaration of bedezign\yii2\audit\components\panels\DataStoragePanelTrait::getUrl() using yii2-audit

孤者浪人 提交于 2019-12-23 04:54:19
问题 I am using yii2-audit extension for audit logs, I followed all steps of installation and also migrate all tables. But when I update table value then it will return error like Audit module cannot be loaded . I have place behaviors() in my model . My Model <?php namespace frontend\models; use yii\base\Model; use yii\web\UploadedFile; use bedezign\yii2\models\AuditTrail; use bedezign\yii2\audit\models\AuditEntry; use bedezign\yii2\Audit; class StudentForm extends \yii\db\ActiveRecord { public

In Yii2 framework, better place to define common function which is accessible everywhere like controller, model, view

做~自己de王妃 提交于 2019-12-23 03:26:42
问题 Like i want to create function with name "dt()" function dt(){ return date('Y-m-d H:i:s'); } and want to access it like this:- echo dt(); //retrun current date and time format Which is better place in Yii2 framework to do that? 回答1: You can use it this way: http://www.yiiframework.com/extension/yii2-helpers/ Create a folder /common/helpers/ , then create a helper class there and add your static methods. namespace common\helpers; class DateHelper { public static function dt(){ return date('Y-m

How to set multi select value from array object in yii2 while updating

可紊 提交于 2019-12-22 14:58:27
问题 I have table which have multiple reference to ohter tables like user id name email categories id title user_categories user_id category_id Here a user will have multiple category associated with him/her I am able to save these successfully with new records like following View File: echo $form->field($package_categories, 'category_id')->dropDownList( ArrayHelper::map( StudyMaterialCategories::find()->all(), 'id', 'title'), ['multiple' => true] ); Save New record: $model = new Packages();

Yii2 - How to set dynamic authTimeout in User Identity?

半世苍凉 提交于 2019-12-22 09:46:26
问题 Here, I have extended User Identity of Yii2. This is my configuration. 'user' => [ 'identityClass' => app\models\UserMaster::class, 'enableAutoLogin' => false, 'loginUrl' => ['/auth/login'], 'authTimeout' => 86400 ], Here, I have defined authTimout statically. But, What I want to do is that I want to fetch timeout value from database and set it in authTimeout . Thanks. 回答1: You can use event to set authTimeout before request will be handled: 'as beforeRequest' => [ 'class' => function (Event

Submitting Button Value to Controller but fail to post the value

杀马特。学长 韩版系。学妹 提交于 2019-12-22 05:38:41
问题 I know i have made mistakes here but i have been searching for this for the whole day. I used yii2 framework and i am still new here, I want to access partone/two page, the partone/two page has two submit buttons, one is for adding row, the second one is for validating the input inside PartoneController.php <? public function actionTwo() { if(\Yii::$app->user->isGuest) { $this->goHome(); } $models = []; $val = "" ; //Create array of items Yii::trace("Lets start: "); if(Yii::$app->request-

How to display action button as dropdown in Yii2 gridview?

笑着哭i 提交于 2019-12-21 20:33:44
问题 I would like to display action button as dropdown in Yii 2 gridview. How can I achieve that without using any extension? I have added the source code bellow- <?= GridView::widget([ 'dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'columns' => [ ['class' => 'yii\grid\SerialColumn'], 'id', 'name', ['class' => 'yii\grid\ActionColumn', 'template'=>'{view}{update}{delete}', 'buttons' => [ 'view' => function ($url, $model) { return Html::a('<span class="glyphicon glyphicon-eye-open">

Yii2 Override find() to add default condition globally

眉间皱痕 提交于 2019-12-21 19:44:37
问题 I have to override the method using namespace common\models; use Yii; use yii\db\ActiveQuery; class Addfindcondition extends ActiveQuery { public function init() { $this->andOnCondition([$this->modelClass::tableName() . '.branch_id' => Yii::$app->user->identity->branch_id ]); parent::init(); } } And call the method in each model separately like this public static function find() { return new Addfindcondition(get_called_class()); } Now I want to override the find method globally. How it is