Friday 11 November 2016

Yii2 include css file in view

It would be better to register your css and js files in an asset bundle, but you could also do:
$this->registerCss("body { background: #f00; }");
The code above will result in adding the following to the head section of the page:
<style>
body { background: #f00; }
</style>
$this->registerCssFile(Yii::$app->request->baseUrl.'/views/A_view/A.css');

$this->registerCssFile("http://example.com/css/themes/black-and-white.css", [
    'depends' => [BootstrapAsset::className()],
    'media' => 'print',
], 'css-print-theme');

The code above will add a link to CSS file to the head section of the page.
  • The first argument specifies the CSS file to be registered.
  • The second argument specifies the HTML attributes for the resulting <link> tag. The option depends is specially handled. It specifies which asset bundles this CSS file depends on. In this case, the dependent asset bundle is BootstrapAsset. This means the CSS file will be added after the CSS files in BootstrapAsset.
  • The last argument specifies an ID identifying this CSS file. If it is not provided, the URL of the CSS file will be used instead
It is highly recommended that you use asset bundles to register external CSS files rather than using registerCssFile(). Using asset bundles allows you to combine and compress multiple CSS files, which is desirable for high traffic websites.

Yii2 include js file in view

Register your js file on given possion

$this->registerJsFile('path/to/file.js', ['position' => \yii\web\View::POS_END]);

The first argument is the actual JS code we want to insert into the page. The second argument determines where script should be inserted into the page. Possible values are:

    View::POS_HEAD for head section.

    View::POS_BEGIN for right after opening .

    View::POS_END for right before closing .

    View::POS_READY for executing code on document ready event.

This will register jQuery automatically. View::POS_LOAD for executing code on document load event. This will register jQuery automatically. The last argument is a unique script ID that is used to identify code block and replace existing one with the same ID instead of adding a new one. If you don't provide it, the JS code itself will be used as the ID.

An external script can be added like the following:

$this->registerJsFile('http://example.com/js/main.js', ['depends' => [\yii\web\JqueryAsset::className()]]);

The arguments for registerJsFile() are similar to those for registerCssFile(). In the above example, we register the main.js file with the dependency on JqueryAsset. This means the main.js file will be added AFTER jquery.js. Without this dependency specification, the relative order between main.js and jquery.js would be undefined.

Soruce: http://stackoverflow.com/questions/27656777/yii2-registering-js-files-to-a-view


Friday 7 October 2016

Yii2 check if record is exist

In Yii2 we can check if record is exist or not through exist function

Example:

$exist = Record::find()->where( [ 'id' => 1 ] )->exists(); 
if($exist){
   echo "Record is exist";
}else{
   echo "No Record is exist";
}
The generated SQL looks like this: 
SELECT 1 FROM `tbl_record` WHERE `id`=1.
Query->exists()taken from the Yii2 source.

Here is library function code. (reference only)
     /**
     * Returns a value indicating whether the query result contains any row of data.
     * @param Connection $db the database connection used to generate the SQL statement.
     * If this parameter is not given, the `db` application component will be used.
     * @return boolean whether the query result contains any row of data.
     */
    public function exists($db = null)
    {
            $select = $this->select;
            $this->select = [new Expression('1')];
            $command = $this->createCommand($db);
            $this->select = $select;
            return $command->queryScalar() !== false;
    }