viernes, 7 de julio de 2017

Diferentes querys para usar en yii2

Funcion SUM

<?=app\modules\soporte\models\InvBaterias::find()->where(['id_periodo'=>1])->sum('cantidad');


Funcion orderBy

$resultado = CatPlanteles::find()->orderBy('id')->all();

app\modules\admin\models\EstadoBaja::find()->orderBy('nombre')->asArray()->all()

Funcion Count

<?=app\modules\soporte\models\InvImpresoras::find()->where(['id_plantel'=>$value['id']])->andWhere(['estado'=>1])->count(); ?>

Función group by

InvNobreak::find()->select(['marca'])->where(['id_plantel'=>Yii::$app->user->identity->id_plantel])->groupBy(['marca'])->all();

Funcion queryOne como arreglo

\Yii::$app->db2->createCommand($sql)->queryOne();


$var['var'];


Funcion find ONE

$model = InvBajas::findOne($id_baja);

$model->progresivo;
$model->tipoEquipo->nombre;




$inventario = \Yii::$app->db2->createCommand($sql)->queryOne();

domingo, 18 de junio de 2017

Graficos en yii2

agregar al composer

"miloschuman/yii2-highcharts-widget": "dev-master",




<?php
use yii\helpers\Html;
use miloschuman\highcharts\Highcharts;
use yii\web\JsExpression;

/*use app\modules\student\models\StuMaster;
use \app\modules\course\models\Batches;
$this->title = Yii::t('course', 'Manage Course Modules');
$this->params['breadcrumbs'][] = $this->title;
$this->registerCss(".disp-count{cursor:default;} .disp-count:hover {background-color:none !important}");*/
?>
<?php
  echo Highcharts::widget([
    'scripts' => [
    'modules/exporting',
    'themes/grid-light',
    'highcharts-3d',
  ],
   'options' => [
   'exporting'=>[
      'enabled'=>true
    ],
    'credits'=>[
            'enabled'=>false
         ],
      'chart'=> [
        'type'=>'pie',
      ],
      'title' => ['text' => 'Fruit Consumption'],
      'xAxis' => [
         'categories' => ['Apples', 'Bananas', 'Oranges']
      ],
      'yAxis' => [
         'title' => ['text' => 'Fruit eaten']
      ],
      'series' => [
         ['name' => 'Jane', 'data' => [1, 0, 4]],
         ['name' => 'John', 'data' => [5, 7, 3]]
      ]
   ]
]);

?>

Actualizar composer yii2

sudo composer global require fxp/composer-asset-plugin:~1.3@dev

martes, 23 de mayo de 2017

Poner un estilo a GridView

 [
              'attribute'=>'estado_baja',
              'value' => 'estadoBaja.nombre',
              'contentOptions' => function($model)
                    {
                        return ['style' => 'color:' . $model->estadoBaja->color];
                    },
              'filter' => yii\helpers\ArrayHelper::map(app\modules\admin\models\EstadoBaja::find()->orderBy('nombre')->asArray()->all(),'id','nombre')
            ],

miércoles, 17 de mayo de 2017

Escenarios en Yii2

Se crea el escenario en el modelo en la funcion rules

[['usuario', 'id_area', 'id_piso'], 'required', 'on'=>'upuser'],

y en el controlador

si es update

 $model = $this->findModel($id);
 $model->scenario = 'upuser';


Si es insert
$model = new InvEquipos(['scenario'=>'upuser']);

martes, 25 de abril de 2017

Crear relaciones entre modelos

use app\modules\soporte\models\CatAntiguedad;
     public function getCatAntiguedad()
    {
        return $this->hasOne(CatAntiguedad::className(),['id'=>'clasif']);
    }




Para mandar llamarla relacion es por medio

<?= $model->catAntiguedad->nombre ?>

Dropdownlist Dependientes yii2

<div class="form-group">
                                            <label for="curl" class="control-label col-lg-2">Marca</label>
                                            <div class="col-lg-4">
                                                 <?= $form->field($model, 'marca', ['inputOptions'=>[ 'class'=>'form-control', 'placeholder' => 'tipo'] ] )->dropDownList(ArrayHelper::map(app\modules\soporte\models\CatMarca::find()->orderBy(['id'=>SORT_ASC])->all(),'id','nombre'),
                                                 [
                                                    'prompt'=>Yii::t('app', '--- Selecciona Marca ---'),
                                                   'onchange'=>'
                                                        $.post( "'.Yii::$app->urlManager->createUrl('soporte/inv-equipos/modelos?id=').'"+$(this).val(), function( data ) {
                                                          $( "select#invequipos-modelo" ).html( data );
                                                        });



                                                    '])->label(false); ?>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label for="ccomment" class="control-label col-lg-1">Modelo</label>
                                            <div class="col-lg-4">
                                                 <?= $form->field($model, 'modelo', ['inputOptions'=>[ 'class'=>'form-control', 'placeholder' => 'modelo'] ] )->dropDownList(ArrayHelper::map(app\modules\soporte\models\CatModelo::find()->orderBy(['id'=>SORT_ASC])->all(),'id','modelo'),['prompt'=>Yii::t('app', '--- Selecciona modelo ---')])->label(false); ?>
                                            </div>
                                        </div>


----Action--

   public function actionModelos($id)
    {
        $cuentaModelos = CatModelo::find()->where(['id'=>$id])->count();
        $modelos = CatModelo::find()->where(['id'=>$id])->all();

        if ($cuentaModelos > 0) {
            foreach ($modelos as $key => $value) {
                echo "<option value=". $value->id . ">". $value->modelo. "</option>";
            }
        }else{
            echo "<option>-</option>";
        }
    }