yii2

Yii2: How to put null values to the end of an object list when sorting is ascending?

有些话、适合烂在心里 提交于 2019-12-10 11:07:55
问题 Here is my DataProvider: $dataProvider = new ActiveDataProvider([ 'query' => $query, 'pagination' => ['pageSize' => 50], 'sort' => [ 'defaultOrder' => [ 'priority' => SORT_DESC, 'date_targeted' => SORT_ASC ] ] ]); What I want is to move ("not set") to the end of the results. How can I do this? 回答1: You may add new field in select . And set in IF case like that: $query->select([ '*', new \yii\db\Expression('IF(date_targeted IS NULL, 1, 0) AS date_targeted_flag') ]); And in sort add date

Yii2 rules compareAttribute compare with attribute of another model

拥有回忆 提交于 2019-12-10 10:34:30
问题 during validation I would like to compare with an attribute from another model. Is it possible? If yes, I would be grateful if you would point me to the right direction. I imagine it somehow to access model B in model A, but maybe my logic is not good, and I have no clue how can this be achieved. Thanks. 回答1: You can try to build an inline validator see this doc for validator and for inline validator this is a brief sample public function rules() { return [ ..... ['my_field',

Yii2 dropDownList mark option selected

女生的网名这么多〃 提交于 2019-12-10 10:18:06
问题 Hi i'm trying to make a drop down list with selected value but there is still no progress, drop down is rendenering but always first option is selected. $company_id = (int) $params['company_id']; $options = [ 'options' => [ $company_id => [ 'selected' => 'selected', 'label' => 'test' ] ] ]; echo $form->field($model, 'company_id')->dropDownList($companies_list, $options); whats wrong with that code? I edited my code and i set 'label' => 'test' in my option, and this works, but selected still

Adding an attribute to yii2 active record model that is not in database

我的未来我决定 提交于 2019-12-10 10:16:20
问题 I have a mySQL database that has a table videos and two columns, start_time and end_time , which are in the format 2017-01-24 15:38:11 . I have an active record model Videos that extends \yii\db\ActiveRecord and i would like to add a few additional attribute that are not in the database. I am trying to split the time stamp into a separate date and time that i can then display as columns in my gridview widget. I successfully did this by setting the column values directly in the view to

Yii2 Gridview merge two columns

☆樱花仙子☆ 提交于 2019-12-10 10:15:33
问题 i have a gridview in Yii2 with two columns, first_name and last_name. I want to merge this two values into one single column named full_name made like tihs: 'first_name'.' '.'last_name' , searchable and filterable. How can i do it? 回答1: try this way: <?= GridView::widget([ 'dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'columns' => [ ['class' => 'yii\grid\SerialColumn'], [ 'attribute' => 'an_attributeid', 'label' => 'yourLabel', 'value' => function($model) { return $model-

Split ActiveForm fields into different tabs with Tabs widget

你离开我真会死。 提交于 2019-12-10 10:06:02
问题 I'm creating a form view and I want to organize the form fields with tabs structure, using the official Tabs widget. Is it possible init the Tabs widget with the id (or class) of the div elements that contains the active form fields? 回答1: One example of how you can manage it is doing like this: First, divide your contact-form into one view-file for each tab. Place the ActiveForm::begin() and ActiveForm::end() around the Tabs::widget() Render the contact-form pages into content, with

Yii2 database session - store additional attributes and user information

≯℡__Kan透↙ 提交于 2019-12-10 10:04:11
问题 I'm using Yii2's DBSession class to store web application sessions into a database table, called session . This table by default only has 3 columns - id , expire and data . I'd like to store additional information into this table, like the user_id of a logged in user. Edit : So there's a parent class called yii\web\MultiFieldSession but no examples about how it's used. I'll see what I can discover... 回答1: create migration: $this->createTable('session', [ 'id' => $this->char(40)->notNull(),

Yii2 set db connection at runtime

左心房为你撑大大i 提交于 2019-12-10 09:54:35
问题 In my Yii2 (basic application) web.php I configure a NULL db connection as 2nd database connection. This needs to be filled with valid parameters which are coming from a record on the main db connection: 'db' => require(__DIR__ . '/db.php'), 'db2' => [ 'class' => 'yii\db\Connection', 'dsn' => NULL, 'username' => NULL, 'password' => NULL, 'charset' => 'utf8', ], After initializing the app() i need to fill out the NULL parameters with values that i retrieve from another database to further use

Yii2 how to check if two models are already linked

大城市里の小女人 提交于 2019-12-10 07:46:42
问题 I have two models related through a junction table. $model->link() is the method used to establish the relationship between the two models. It basically populates the junction table with the corresponding keys of both models. If two models are linked and I try to link them again, there will be an error because the key pair already exists in the junction table. Then I'd need to check if this relation exists before attempting to link the models. I think I could just create a model for the

Docker containers: how do they work together?

前提是你 提交于 2019-12-10 07:16:04
问题 I have started working with docker and built a working example as seen in https://codeable.io/wordpress-developers-intro-docker. I need a quite small footprint of the docker containers since the deployment will be on an emebedded system. But I have no clue about how this fits together. There are two Dockerfiles, one for Nginx: FROM nginx:1.9-alpine COPY nginx.conf /etc/nginx/conf.d/default.conf The nginx.conf is defined as: server { server_name _; listen 80 default_server; root /var/www/html;