Menus

Tuesday, 11 April 2017

Yii2 DetailsView Operations


DetailView is best used for displaying a model in a regular format (e.g. each model attribute is displayed as a row in a table.) The model can be either an instance of yii\base\Model or an associative array.
DetailView uses the $attributes property to determines which model attributes should be displayed and how they should be formatted.
A typical usage of DetailView is as follows:
echo DetailView::widget([
    'model' => $model,
    'attributes' => [
        'title',               // title attribute (in plain text)
        'description:html',    // description attribute in HTML
        [                      // the owner name of the model
            'label' => 'Owner',
            'value' => $model->owner->name,
        ],
        'created_at:datetime', // creation date formatted as datetime
    ],
]);

Examples of DetailsView


View Form

View Code

<?php
use yii\helpers\Html;
use yii\widgets\DetailView;
use backend\modules\tools\models\Groupitems;
$this->title = 'Note : '. $model->slno;
$this->params['breadcrumbs'][] = ['label' => 'Students Instructions Note', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="studentsgblinvoicenote-view box box-body table-responsive">  
    <p>
        <?= Html::a('Update', ['update', 'ccid' => $model->ccid, 'slno' => $model->slno], ['class' => 'btn btn-primary']) ?>
        <?= Html::a('Delete', ['delete', 'ccid' => $model->ccid, 'slno' => $model->slno], [
            'class' => 'btn btn-danger',
            'data' => [
                'confirm' => 'Are you sure you want to delete this item?',
                'method' => 'post',
            ],
        ]) ?>
    </p>

    <?= DetailView::widget([
        'model' => $model,
        'attributes' => [
            'slno',
            'course',
             'note1',
            'note2'                  
        ],
    ]) ?>
</div>

Controller action

 public function actionView($ccid, $slno)
    {
        return $this->render('view', [
            'model' => $this->findModel($ccid, $slno),
        ]);
    }


More Details

Karthik DetailView: http://demos.krajee.com/detail-view

Put line break in DetailsView

Textarea saved data to display in DeatilsView



<?php

use yii\helpers\Html;
use yii\widgets\DetailView;
use backend\modules\tools\models\Groupitems;

$this->title = 'Note : '. $model->slno;
$this->params['breadcrumbs'][] = ['label' => 'Students Instructions Note', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>

<div class="studentsgblinvoicenote-view box box-body table-responsive">

 

    <p>
        <?= Html::a('Update', ['update', 'ccid' => $model->ccid, 'slno' => $model->slno], ['class' => 'btn btn-primary']) ?>
        <?= Html::a('Delete', ['delete', 'ccid' => $model->ccid, 'slno' => $model->slno], [
            'class' => 'btn btn-danger',
            'data' => [
                'confirm' => 'Are you sure you want to delete this item?',
                'method' => 'post',
            ],
        ]) ?>
    </p>

    <?= DetailView::widget([
        'model' => $model,
        'attributes' => [
            //'ccid',
            'slno',                    
            [
                'label'  => 'Course',
                'value'  => Groupitems::getGroupitems($model->course),           
            ],
         
            [
                'label' => 'Note 1',
                'format'=>'raw',
                'value' => nl2br($model->note1 ),
            ],    
   
            [
                'label' => 'Note 2',
                'format'=>'raw',
                'value' => nl2br($model->note2),
            ],  
         
        ],
    ]) ?>

</div>


Changing value of an attribute in DetailView widget

The above example call from function to display DetailsView
                'value'  => Groupitems::getGroupitems($model->course),       


<?= DetailView::widget([
    'model' => $model,
    'attributes' => [
        'name',
        'max_people_count',
        'type',
        [
             'attribute' => 'recurring',
             'format'=>'raw',
             'value'=> function ($model) {
                        if($model->recurring == 1)
                        {

                            return 'yes';

                        } 
                        else {
                        return 'no';
                        }
                      },
        ],
        'day',
        'time',
        ...

'value' => $model->recurring == 1 ? 'yes' : 'no'
The nl2br() function inserts HTML line breaks (<br> or <br />) in front of each newline (\n) in a string.


Add Button in DetailsView




<?php

use yii\helpers\Html;
use yii\widgets\DetailView;
use backend\modules\master\models\Masterfunctions;

/* @var $this yii\web\View */
/* @var $model backend\modules\tools\models\Centermessage */

$this->title = $model->ctm_id;
$this->params['breadcrumbs'][] = ['label' => 'Centermessages', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;

$model->ctm_centers = Masterfunctions::getCenters($model->ctm_centers);
?>
<div class="centermessage-view box box-body table-responsive">
 
 
<?php

$button1 = Html::a('<span class = "glyphicon glyphicon-download"></span> Download', ['docs-download', 'filename' => $model->ctm_filename,], ['class' => 'btn-primary', 'title' => '', 'data' => ['method' => 'post']]);
$model->ctm_filename=$button1;
?>
   
    <?= DetailView::widget([
        'model' => $model,
 
        'attributes' => [                      
            [
          'attribute' => 'ctm_date',
          'format' =>  ['date', 'php:d-m-Y'],    
   ],
            'ctm_heading',
            'ctm_message',
            [
                'attribute' =>'ctm_filename',                
                'format'=>'html',
            ],
           
            'ctm_centers',
            'ctm_status',
        ],
    ]) ?>

</div>







Related Links

DropDownList

Yii2 Dependent Drop Down Lists

CheckboxList Advanced Operations

Yii2 ListBox Field



Wednesday, 5 April 2017

Mysql composite primary key with set foreign key


Foreign key referencing only part of composite primary key


Primary Key = UNIQUE + NOT NULL + Automatic Indexed


Primary key can be defined for a combination of columns, When a primary key consist of multiple columns, then it is called a Composite Primary Key.


MySql Create Foreign Key

Create foreign key referencing to primary key


ALTER TABLE  `tb2` ADD FOREIGN KEY (  `tb1_ccid` ) REFERENCES  `test`.`tb1` (
`ccid`) ON DELETE RESTRICT ON UPDATE RESTRICT ;


Create foreign key referencing to composite primary key

ALTER TABLE  `tb2` ADD FOREIGN KEY (  `tb1_ccid` ,  `tb1_sid` ) REFERENCES  `test`.`tb1` ( `ccid` ,`sid`) ON DELETE RESTRICT ON UPDATE RESTRICT


Example

create table tb1
( A integer not null
, B char(2) not null
, C integer not null
, primary key (A,B,C)
, unique (A,B)
);
create table tb2
( M integer not null
, N char(2) not null
, Z datetime
, foreign key (M,N) references tbl1 (A,B)
)