yii2

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

Why create a separate application for RESTful API?

半城伤御伤魂 提交于 2019-12-09 06:02:52
问题 In the guide for Yii 2 it is said: While not required, it is recommended that you develop your RESTful APIs as a separate application, different from your Web front end and back end for easier maintenance. Source: RESTful Web Services - Quick Start What does this mean? Would this be a completely different application or can it be in the same folder as the 'normal' web application? I've just started with my application so I can change things easily, more or less. But I'm wondering: if I would

Hide Yii2 GridView Action buttons

半世苍凉 提交于 2019-12-09 04:20:33
问题 I want to hide Yii2 GridView Action Column buttons on the base of model field status. If status is = 1 then hide view button only. How I can? Code: [ 'class' => 'yii\grid\ActionColumn', 'contentOptions' => ['style' => 'width:260px;'], 'header'=>'Actions', 'template' => '{view} {delete}', 'buttons' => [ //view button 'view' => function ($url, $model) { return Html::a('<span class="fa fa-search"></span>View', $url, [ 'title' => Yii::t('app', 'View'), 'class'=>'btn btn-primary btn-xs', ]); }, ],

url management in Yii 2

帅比萌擦擦* 提交于 2019-12-09 02:56:53
问题 I have this url http://example.com/index.php/controller_name/action_name?queryString=123 This url is working fine but when I am trying to use the queryString like in the old version of Yii http://example.com/index.php/controller_name/action_name/queryString/123 I get an "unable to solve request" error. I 've already enable prettyurl in my config file and the following url is working http://example.com/index.php/controller_name/action_name. My config looks like : 'urlManager' => [ 'class' =>

Yii2 GridView Customize Header Row

邮差的信 提交于 2019-12-09 02:15:21
问题 In my view code I have this: <?= GridView::widget([ 'dataProvider' => $dataProvider, 'columns' => [ ['class' => 'yii\grid\SerialColumn'], ['label' => 'Training Score', 'attribute' => 'scoreTraining', 'format' => ['decimal',2], ], ['label' => 'Exam Score', 'attribute' => 'scoreExam', 'format' => ['decimal',2], ], ], ]); Normally the header name will be "Training Score" and "Exam Score" Is that possible in yii2 gridview to customize the header row? so that my header row looks like in 2 line..

Yii2: update field with query builder

感情迁移 提交于 2019-12-09 02:10:39
问题 How can I update field with query builder in Yii2? I can't find this in documentation. Thanks! UPD This is the solution: // UPDATE $connection = Yii::$app->db; $connection->createCommand()->update('user', ['status' => 1], 'age > 30')->execute(); 回答1: Query builder is for select queries only (sum, max, count too). You should use other methods - AR or raw queries (https://github.com/yiisoft/yii2/blob/master/docs/guide/db-dao.md#basic-sql-queries) 回答2: Create command can be used directly as

GridView row as link, except action column items in Yii2

拈花ヽ惹草 提交于 2019-12-08 23:48:55
问题 When i use the below code it overrides the action-column delete/update links. 'rowOptions' => function ($model, $key, $index, $grid) { return [ 'id' => $model['id'], 'onclick' => 'location.href="' . Yii::$app->urlManager->createUrl('accountinfo/update') .'?id="+(this.id);', ]; }, As I have many columns it would be good to specify link url in one place instead of using the below code in each column: 'value' => function ($data) { return Html::url('site/index'); } So is there any best way to

Cant use tockens and extrapattern together for REST services in Yii2

时光怂恿深爱的人放手 提交于 2019-12-08 19:57:28
Yii2 REST query I found this for using custom action in the controller for that i added the extrapattern mentioned in the above link And its working fine when we search .but cant use the normal actions for the controller 'urlManager' => [ 'enablePrettyUrl' => true, 'enableStrictParsing' => true, 'showScriptName' => false, 'rules' => [ [ 'class' => 'yii\rest\UrlRule', 'controller' => 'v1/country', 'extraPatterns' => [ 'GET search' => 'search' ], 'tokens' => [ '{id}' => '<id:\\w+>' ] ] ], ] Regards Thanks all this solved my problem after lots of trying.. 'rules' => [ [ 'class' => 'yii\rest

Delete multiple rows in YII2

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-08 18:51:17
问题 I have an array of objects fetched from database: $masterListContacts = MasterListContacts::find() ->select('master_list_contacts.*') ->innerJoin('master_contacts', '`master_contacts`.`id` = `master_list_contacts`.`master_contact_id`') ->with('masterContact') ->where(['user_id' => \Yii::$app->user->identity->id, 'slug' => $slug]) ->all(); Under certain circumstances, I need to delete all rows from the database represented in this array. But with both delete() and deleteAll() methods I got an

How to open view page in popup in yii2?

假如想象 提交于 2019-12-08 16:08:35
I have to open view page contain in popup instead of page.After click on view open view contain in popup yii2 You should try this code for opening popup. and also you need to render with ajax view using this $this->renderAjax() . controller public function actionView($id) { if (Yii::$app->request->isAjax) { return $this->renderAjax('view', [ 'model' => $this->findModel($id), ]); } else { return $this->render('view', [ 'model' => $this->findModel($id), ]); } } View <?= GridView::widget([ 'dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'columns' => [ ................... [ 'class'