yii2

how to build query like `select … where id % 2 = 1` using Yii 2 ?

流过昼夜 提交于 2020-01-01 10:10:06
问题 Surely, I can Yii::$app->db->createCommand($sql)->query() , but what if I wanna use a ActiveRecord::find()->where($conditions) to do this job ? 回答1: Here is one of the options with using yii\db\Expression : use yii\db\Expression; ... $models = Customer::find() ->select(['id', 'name', ...]) ->where(new Expression('id % 2 = 1')]) ->all(); It's definetely better than raw sql and ['%2=', 'id', 1] because it follows the order and more readable in my opinion. ['%2=', 'id', 1] also is not suitable

Yii2: Get selected rows data from gridView checkbox columns into controller

痴心易碎 提交于 2020-01-01 09:42:18
问题 I've view page( index.php ) in my Yii2 project, and I'm using Kartik gridView for showing the data This the view from index.php: On the right side of view, I've a checkbox column. And I've an Export button. I want to export the selected name (selected by checkbox) into name.txt file. I've finally make the export function, but I don't know how to get the selected data from view into controller. I've try suggestions that I got from many forums, for example: I put this javascript code in my view

Yii2 Gridview row by row css expression

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-31 21:27:17
问题 What is the correct way to do a row by row css expression. In Yii 1 is was rowCssClass. I couldn't figure out how to achieve this with Yii2. I had tried this, but wasn't sure I was on the right lines: 'rowOptions' => function($model, $key, $index, $grid){ if($data->option->correct_answer == 1){ return ['class' => 'danger']; } }, I'm unsure where to get the parameters for the function from when dealing with the dataProvider though. 回答1: Use $model instead $data. In my variant: 'rowOptions' =>

How to create a custom ActionColumn in the gridView of yii2?

喜欢而已 提交于 2019-12-31 21:23:06
问题 I have a gridView and i managed to get it to contain the data i need, but what i need to do next is to create a column which contains two buttons for has_facebook and has_twitter. <?= GridView::widget([ 'dataProvider'=>$dataProvider, 'filterModel' =>$searchModel, 'columns' =>[ ['class'=>'yii\grid\SerialColumn'], 'name', 'cm_name', 'has_facebook', 'has_twitter', ['class'=>'yii\grid\ActionColumn'], ], ]); ?> name | cm_name | platforms account1 | jack | btn1 btn2 where btn1 and btn2 refer to

How to create a custom ActionColumn in the gridView of yii2?

走远了吗. 提交于 2019-12-31 21:23:02
问题 I have a gridView and i managed to get it to contain the data i need, but what i need to do next is to create a column which contains two buttons for has_facebook and has_twitter. <?= GridView::widget([ 'dataProvider'=>$dataProvider, 'filterModel' =>$searchModel, 'columns' =>[ ['class'=>'yii\grid\SerialColumn'], 'name', 'cm_name', 'has_facebook', 'has_twitter', ['class'=>'yii\grid\ActionColumn'], ], ]); ?> name | cm_name | platforms account1 | jack | btn1 btn2 where btn1 and btn2 refer to

Yii 2 redirecting to Login if Guest visit the website

人盡茶涼 提交于 2019-12-31 03:47:07
问题 I need some advice how to make redirecting to login if someone does not login into the website and he is only Guest 回答1: use Yii; use \yii\helpers\Url; if ( Yii::$app->user->isGuest ) return Yii::$app->getResponse()->redirect(array(Url::to(['site/login'],302))); Use can use it in actions or views , but if you need to use it in lots of actions you probably need look at AccessControl and restrict access even before action is fired 回答2: There are two options: You can use the AccessControl as

Yii2 Create Database Connection

左心房为你撑大大i 提交于 2019-12-31 03:17:07
问题 I want to create a database connection programmatically without using the config file. I have a form wherein the user enters the database details like the hostname , username , password and the database name . On the basis of these details a new test connection is made and if it passes it should generate the necessary files. This is what I have tried: // Create Test DB Connection Yii::$app->set('id', [ 'class' => 'yii\db\Connection', 'dsn' => $dsn, 'username' => $form->username, 'password' =>

How to process Vue/Axios Json payload posted data on Yii2

南楼画角 提交于 2019-12-31 03:13:20
问题 It took me a while to understand this, being that it was a little obvious. I will answer myself, so other can benefit of the answer and ofcourse to see if there's a better way to do this. The problem was based on Axios/Yii2 but I guess this will apply equally to other frontend libraries/frameworks sending data to Yii2. I needed to post data from a small form made on Vuejs, sending the request Axios to a Action/Controller on Yii2, so data is sent on a simple POST request and the post is

Yii2 Pjax GridView action buttons issue

99封情书 提交于 2019-12-31 02:08:26
问题 I am trying to make an Ajax GridView using Pjax. Everything is working fine except the view, update and delete buttons are not AJAX. The code is: <?php yii\widgets\Pjax::begin(['id' => 'demo']); ?> <?= GridView::widget([ 'dataProvider' => $dataProvider, 'columns' => [ ['class' => 'yii\grid\SerialColumn'], 'id', 'name', ['class' => 'yii\grid\ActionColumn'], ], ]); ?> <?php yii\widgets\Pjax::end(); ?> The problem is that the links for delete, view and update have the attribute data-pjax=0 which

Yii2: Replace default confirmation message used by Gridview with Sweet alert

大城市里の小女人 提交于 2019-12-31 01:56:48
问题 I am using yii2mod/yii2-sweet-alert in my projects, I am using it on basic and advanced themes, and I love it. The question. How can I change the grid default confirmation dialog that is a plain javascript confirmation in order to use Sweet-alert to make it look better? I already tried modifying the button's template for the delete, because if you want to change the message you will do the following: [ 'class' => ActionColumn::className(), 'template' => '{update}{delete}', 'buttons' => [