yii2-model

how to keep selected value on Yii2 html::dropddownlist?

老子叫甜甜 提交于 2019-12-20 07:48:42
问题 i have the following code in my view: <?=Html::dropdownList('region',null, ArrayHelper::map(Ethioregion::find()->all(),'region','region'),[ 'prompt' => 'Select Region..','style'=>'width:200px',]) ?> and submit button <?= Html::submitButton('Search', ['name' => 'dele','class' => 'btn btn-primary']) ?> my code working well but I have one big problem. I select a region and click the search button, the result comes correctly, and 10 result is shown per page when I click the next page, it gives me

Yii2 unique validator ignored

只谈情不闲聊 提交于 2019-12-12 17:35:34
问题 In the rules() of my RegisterForm model: [ 'user_username', 'unique', 'targetClass' => 'app\models\User', 'message' => 'This username is already been taken.' ], In my controller: $model = new RegisterForm(); if ( $model->load( Yii::$app->request->post() ) ) { if ( $user = $model->register() ) { return $this->redirect( [ '/login' ] ); } } In RegisterForm: public function register() { $user = new User(); $user->user_firstname = $this->user_firstname; $user->user_lastname = $this->user_lastname;

Yii2 model search between query

烂漫一生 提交于 2019-12-12 05:57:53
问题 I Want to impliment below mysql query in yii2 model search() SELECT * FROM `parking_availability` WHERE ('09:00' BETWEEN `time_start` AND `time_end` ) AND ( '11:00' BETWEEN `time_start` AND `time_end` ) I have applied like this $query->andFilterWhere([$this->arrivaltime,'between','time_star', 'time_end']) ->andFilterWhere([$this->departuretime,'between','time_star', 'time_end']); But its showing error Operator '09:00' requires two operands. Please help me , Thanks 回答1: You were in the right

How to pass subquery as table name to another query in yii2

北城以北 提交于 2019-12-11 14:18:08
问题 I have a query which I am trying to convert into yii2 syntax. Below is the query SELECT project_id, user_ref_id FROM ( SELECT `project_id`, `user_ref_id` FROM `projectsList` WHERE user_type_ref_id = 1) AS a WHERE user_ref_id = '.yii::$app->user->id; I am trying to convert it into yii2 format like $subQuery = (new Query())->select(['p.project_id', 'p.user_ref_id'])->from('projectsList')->where(['user_type_ref_id' => 1]); $uQuery = (new Query())->select(['p.project_id', 'p.user_ref_id'])->from(

Yii2 multiple models in one form

孤者浪人 提交于 2019-12-10 13:28:17
问题 How to use multiple models in one form in Yii2? My case: In my create action I can save into agenda_fiscalizacao table, but in update I receive this error when I try to load the form: Call to a member function formName() on array My Update Action: public function actionUpdate($id) { $model = $this->findModel($id); $modelAgenda = AgendaFiscalizacao::findAll(['fiscalizacao_id' => $id]); if ($model->load(Yii::$app->request->post()) && Model::loadMultiple($modelAgenda, Yii::$app->request->post())

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

Yii2: How to set default attribute values in ActiveRecord?

江枫思渺然 提交于 2019-12-09 08:46:50
问题 This may seem like a trivial question, however all of the obvious solutions that I can think of have their own flaws. What we want is to be able to set any default ActiveRecord attribute value for new records only, in a way that makes it readable before and during validation and does not interfere with derived classes used for search. The default values need to be set and ready as soon as we instantiate the class, so that (new MyModel)->attr returns the default attr value. Here are some of

Yii2 how to check if two models are already linked

試著忘記壹切 提交于 2019-12-05 15:14:40
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 junction table and query for the proper record. The result of that query would tell if I need to perform the

How to join two tables and get values in Yii2 gridview

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-04 22:02:01
问题 I'm using Yii2 gridview widget to display the data. I'm using two tables named as message and message_trigger . In message table columns are object_model , Object_id . In message_trigger the column are object_id , object_name . The grid get the values from the table message . So grid fields are Object_model , Object_id . Now my problem is I need to show Object_name from the table message_trigger based on the object_id from the table message . In my form I have used grid like this <?= GridView

How to join two tables and get values in Yii2 gridview

风流意气都作罢 提交于 2019-12-03 13:05:14
I'm using Yii2 gridview widget to display the data. I'm using two tables named as message and message_trigger . In message table columns are object_model , Object_id . In message_trigger the column are object_id , object_name . The grid get the values from the table message . So grid fields are Object_model , Object_id . Now my problem is I need to show Object_name from the table message_trigger based on the object_id from the table message . In my form I have used grid like this <?= GridView::widget([ 'dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'columns' => [ ['class' =>