Yii2 Commands

April 29, 2020

Install Yii

composer create-project --prefer-dist yiisoft/yii2-app-basic <project_directory_name>

Check if Table Exists

$tableSchema = Yii::$app->db->schema->getTableSchema('tableName');

if ($tableSchema === null) {
    // Table does not exist
}

Source: https://stackoverflow.com/questions/31671067/check-table-exists

Database Exception Class

https://www.yiiframework.com/doc/api/2.0/yii-db-exception

Database Connection Class

https://www.yiiframework.com/doc/api/2.0/yii-db-connection#open()-detail

Adding Yii2 .env support

I was surprised that Yii2 did not have .env support out of the box. Here is how to add it.

# add this to your project
composer require vlucas/phpdotenv
# Add this to your yii console file
// Load dotenv?
if (class_exists('Dotenv\Dotenv') && file_exists('.env')) {
    $dotenv = Dotenv\Dotenv::createImmutable('./');
    $dotenv->load();
} else {
    echo PHP_EOL . PHP_EOL . "Either the .env file does not exist, or the Dotenv class file does not exist.  Exiting." . PHP_EOL . PHP_EOL;
    exit();
}
# add this to your index php file
// Load dotenv?
if (class_exists('Dotenv\Dotenv') && file_exists('.env')) {
    $dotenv = Dotenv\Dotenv::createImmutable('../');
    $dotenv->load();
} else {
    echo PHP_EOL . PHP_EOL . "Either the .env file does not exist, or the Dotenv class file does not exist.  Exiting." . PHP_EOL . PHP_EOL;
    exit();
}

Adding Yii2 to your PHP project with Composer Require

You first need to add Yii 2 Core Bower Asset

https://github.com/yidas/yii2-bower-asset

composer require yidas/yii2-bower-asset

Then you can require yii2

composer require yiisoft/yii2

Password Hidden Prompt

composer require seld/cli-prompt
Personal Updates

This is a password protected section available to those who know me in some fashion. If you know me and you do not already have a login, you can request a login here.

Personal Updates
Book Reviews

I am nuts about books. I read on all kinds of topics. I attempt to review each book I read for the sake of my own enrichment as well as conversation starters with others.

My Book Reviews
Pastoral Ministry

God has called me to be a pastor, and occasionally I have some pastoral thoughts I like to share.

Pastoral Posts
The Attic

You never know what you will find in an attic! Usually there is a hodgepodge of things buried under dust.

Explore the Attic
Technology

Most of what is included here are notes to myself. The majority of folks will not find interest in these posts.

Technology Posts