working on it ...

Filters

Explore Public Snippets

Sort by

Found 756 snippets matching: yii

    public by vss modified Apr 15, 2016  2408  1  4  1

    Hidden filed - old style

    Так лучше уже не делать, поскольку для невидимых полей не нужны обвязки форма для валидации введенных данных и прочее
    $form->field($cart, 'Goods_idGoods', ['value' => 'test'])->hiddenInput()->label(false)
    
    //нужно длеать вот так
    <?= Html::activeHiddenInput($cart, 'Goods_idGoods', ['value' => $model->idGoods])?>

    public by azazqadir modified Nov 21, 2017  564  0  5  1

    CDN integration in Yii2 App

    How to integrate CloudwaysCDN with Yii2 applications: https://www.cloudways.com/blog/cdn-in-yii2/
    // ...
    
    'components' => [
    
       // ...
    
    'cdn' => [
                'class' => '\yii2cdn\Cdn',
                'baseUrl' => defined ('YII2CDN_OFFLINE')
                    ? 'http://localhost/yii2/cdn'
                    : '//71265-358070-raikfcquaxqncofqfm.stackpathdns.com/cdn',
                'basePath' => dirname(__DIR__, 2) . '/cdn',
                'components' => [
                    'font-awesome' => [
                        'css' => [
                            [
                                // local version
                                'font-awesome.min.css',
                                // cdn version
                                '@cdn' => '//71265-358070-raikfcquaxqncofqfm.stackpathdns.com/cdn/font-awesome/css/font-awesome.min.css',
                            ]
                        ]
                    ]
                ],
            ],
     // ...
    
    ],
    
    // ...

    public by xavsio4 modified Nov 26, 2014  2279  0  4  1

    Yii - Get the user ID

    yii
    Yii::app()->user->id;
    
    Any other session data attached to the user
    
    Yii::app()->user->lastname;

    public by kbudylov modified Jan 26, 2017  2022  14  5  0

    Yii2 $form->field

    <?= $form->field($model,'field',[
      //'template' => '{input}',
      //'options' => [
        //'class' => ''
      //]
    ])->textField([
      //'class' => ''
    ]) ?>

    public by tneumiller modified Apr 14, 2015  2651  1  3  0

    install YII2

    sudo composer global require "fxp/composer-asset-plugin:1.0.0-beta2"
    
    
    composer create-project --prefer-dist yiisoft/yii2-app-basic cmsprime
    
    
    composer require --prefer-dist yiisoft/yii2-bootstrap
    
     composer require kartik-v/yii2-widgets "*"
    

    public by tneumiller modified Apr 16, 2015  1898  0  2  0

    yii2

     
    sudo composer global require "fxp/composer-asset-plugin:1.0.0-beta2"
    
    
    composer create-project --prefer-dist yiisoft/yii2-app-basic cmsprime
    
    
    composer require --prefer-dist yiisoft/yii2-bootstrap
    
     composer require kartik-v/yii2-widgets "*"
    
    composer require kartik-v/yii2-grid "*"
    

    public by petschephp modified May 9, 2013  3091  0  4  0

    ActiveForm

    TbActiveForm Yii
    <?php $form=$this->beginWidget('bootstrap.widgets.TbActiveForm',array(
    		'id'=>'line-item-category-form',
    		'enableAjaxValidation'=>false,
    	)); ?>
    		<div class="" >
    			<?php $this->widget('bootstrap.widgets.TbButton', array(
    				'buttonType'=>'submit',
    				'type'=>'primary',
    				'label'=>$model->isNewRecord ? 'Create' : 'Save',
    			)); ?>
    		</div>
    	
    <?php $this->endWidget(); ?>

    external by Tom Worster modified Aug 14, 2015  151185  0  3  0

    Using 3-way join tables in Yii 2 Active Record

    Using 3-way join tables in Yii 2 Active Record: 3-way-joins.md
    I have an interesting Yii 2 AR design problem.
    
    [The DB has](http://musicbrainz.org/relationships) 12 primary entity tables related via a full mesh of 72 join tables. Each relation also has a type (and attributes) but those aren't stored in the join tables – they are in additional tables that the 72 join tables reference. So each of the 72 join tables provides [a 3-way join](http://musicbrainz.org/doc/MusicBrainz_Database/Schema#Relationship_table_structure) between one entity, another entity and another table called link that points to the link_type.
    
    My problem is to write the AR models and relation methods.
    
    Let's make this more concrete with two of the primary entities: artist and recording, related through l_artist_recording. Each record of l_artist_recording has an FK to the link table, through which I can load the link_type name. For example, an artist-recording relation might have link_type "performer", "conductor", or "producer" etc.
    
    This is a simplified schma good enough for thinking about the AR problem.
    
    ![Simplified schema](https://monosnap.com/file/qUjynxqri5JTz0Peu6jaLDfk2NcjOw.png)
    
    An obvious approach is to write an AR model for each of the tables in the diagram. Then Artist models can relate to Recordings and LinkTypes via ArtistRecording. This allows Artist to know the LinkType of the relation to individual Recording model because Recording and LinkType are both properties of each ArtistRecording model. But this requires 72 models for the 72 join tables and it's clumsy to work this way.
    
    I would far prefer if link_type.name were populated into related Recording models when they are loaded. Then, for example, if `$artist` is an Artist model instance it would have `$artist->recordings` which is an array of Recording models, each of which has a `Recording::$name` column attribute loaded from recording.name and a `Recording::$link_type_name` instance variable loaded from link_type.name.
    
    I can relate Artist to Recording without needing a model for artist_recording
    
    ```php
        public function getRecordings() {
            return $this
                ->hasMany(Recording::className(), ['id' => 'recording_id'])
                ->viaTable('artist_recording', ['artist_id' => 'id']);
        }
    ```
    
    But I don't know how to elaborate that so it loads fields from tables related via the third leg of the 3-way join into the Recording models. I am inclined to believe AR *can* do this. But I don't know *how*.
    
    
    
    

    external by jkovacs618 modified Aug 26, 2014  114  0  2  0

    Yii2 custom AppActiveQuery class used to extend yii\db\ActiveQuery to provide a cache function for backwards-compatibility with Yii 1.1 CActiveRecord->cache.

    Yii2 custom AppActiveQuery class used to extend yii\db\ActiveQuery to provide a cache function for backwards-compatibility with Yii 1.1 CActiveRecord-cache.: AppActiveQuery.php
    PHP
    <?php
    namespace common\models\db;
    
    class AppActiveQuery extends \yii\db\ActiveQuery
    {
        private $cacheDuration = null;
        private $functionName = '';
        private $q = null;
        
        /**
         * Use query caching for this ActiveQuery.
         * 
         * @param int|null $duration Seconds to cache; Use 0 to indicate that the cached data will never expire; NULL indicates No Cache.
         * @return \common\models\db\AppActiveQuery
         */
        public function cache($duration = null)
        {
            if(!is_null($duration) && $duration >= 0) {
                $this->cacheDuration = $duration;
            }
            return $this;
        }
        
        public function all($db = null)
        {
            return $this->run('all', null, $db);
        }
        
        public function one($db = null)
        {
            return $this->run('one', null, $db);
        }
        
        public function count($q = '*', $db = null)
        {
            return $this->run('count', $q, $db);
        }
        
        public function sum($q, $db = null)
        {
            return $this->run('sum', $q, $db);
        }
        
        public function max($q, $db = null)
        {
            return $this->run('max', $q, $db);
        }
        
        public function min($q, $db = null)
        {
            return $this->run('min', $q, $db);
        }
        
        public function average($q, $db = null)
        {
            return $this->run('average', $q, $db);
        }
        
        public function scalar($db = null)
        {
            return $this->run('scalar', null, $db);
        }
        
        public function column($db = null)
        {
            return $this->run('column', null, $db);
        }
        
        public function exists($db = null)
        {
            return $this->run('exists', null, $db);
        }
        
        protected function run($functionName, $q, $db = null)
        {
            $this->functionName = $functionName;
            $this->q = $q;
            if(is_null($db)) {
                $modelClassName = $this->modelClass;
                $db = $modelClassName::getDb();
            }
            if(!is_null($this->cacheDuration)) {
                return $db->cache(function(\yii\db\Connection $db) {
                    $functionName = $this->functionName;
                    if(!is_null($this->q)) {
                        return parent::$functionName($this->q, $db);
                    }
                    else {
                        return parent::$functionName($db);
                    }
                }, $this->cacheDuration);
            }
            else {
                if(!is_null($this->q)) {
                    return parent::$functionName($this->q, $db);
                }
                else {
                    return parent::$functionName($db);
                }
            }        
        }
    
    }
    
    

    external by jkovacs618 modified Aug 26, 2014  177  0  2  0

    Yii2 custom AppActiveRecord class used to extend yii\db\ActiveRecord to provide a static find($config) function for backwards-compatibility with Yii 1.1 CActiveRecord->find*(array) functions.

    Yii2 custom AppActiveRecord class used to extend yii\db\ActiveRecord to provide a static find($config) function for backwards-compatibility with Yii 1.1 CActiveRecord-find*(array) functions.: AppActiveRecord.php
    PHP
    <?php
    namespace common\models\db;
    
    class AppActiveRecord extends \yii\db\ActiveRecord
    {
        /**
         * Override the \yii\db\ActiveRecord::find() static function to allow passing in an array of configuration properties.
         * Also, return a custom AppActiveQuery object that overrides functionality of ActiveQuery and allows Caching.
         * 
         * @param Array|null $config The array of key value pairs to set the ActiveQuery properties on construction.
         * @return \common\models\db\AppActiveQuery
         */
        public static function find($config = null)
        {
            $activeQuery = \Yii::createObject(\common\models\db\AppActiveQuery::className(), [get_called_class()]);
            if(is_array($config) && sizeof($config) > 0) {
                if(isset($config['select']) && is_string($config['select'])) {
                    $config['select'] = self::_selectStringToArray($config['select']);
                }
                if(isset($config['orderBy']) && is_string($config['orderBy'])) {
                    $config['orderBy'] = self::_orderByStringToArray($config['orderBy']);
                }
                if(!isset($config['from'])) {
                    // If the 'from' clause is not specified, create one using the called class Table Name.
                    // Include the default table alias of 't' for backwards compatibility. Store 'from' as an array of one string.
                    $className = get_called_class();
                    $tableName = $className::tableName();
                    $config['from'] = [$tableName.' t'];
                }
                foreach($config as $key => $value) {
                    $activeQuery->$key = $value;
                }
            }
            return $activeQuery;
        }
        
        public static function _selectStringToArray($string) {
            $array = explode(',',$string);
            if(is_array($array) && sizeof($array) > 0) {
                foreach($array as $index => $column) {
                    $array[$index] = trim($column);
                }
            }
            return $array;
        }
        
        public static function _orderByStringToArray($string) {
            $strings = explode(',',$string);
            $orderByArray = array();
            if(is_array($strings) && sizeof($strings) > 0) {
                foreach($strings as $index => $orderBy) {
                    $orderBy = trim($orderBy);
                    $lastSpacePos = strrpos($orderBy,' ');
                    $sort = SORT_ASC;
                    if($lastSpacePos!==false) {
                        $column = substr($orderBy,0,$lastSpacePos);
                        $sort = strtoupper(substr($orderBy,$lastSpacePos));
                        if($sort === 'DESC') {
                            $sort = SORT_DESC;
                        }
                    }
                    else {
                        $column = $orderBy;
                    }
                    $orderByArray[$column] = $sort;
                }
            }
            return $orderByArray;
        }
    }
    
    
    • Public Snippets
    • Channels Snippets