Why Fork a Git Repository

If your not a contributor to a repository but use this library in your project, you may find there are times that the code does not work for some reason. It could be a simple bug, a missing feature or maybe even a conflict with another library.

To get around this issue you can fork the repository, make your own changes, update your project to use your version. And if you feel brave enough put in a pull request to the original developers to incorporate your changes.

In this example I am going to take coderius/yii2-hit-counter in GitHub, fork the project, apply a fix and then update my Yii2 project to use my version of the extension.

The Bug

When I installed the yii2-hit-counter library into my Yii2 project and wired up a page to the hit-counter widget, I noticed a jQuery GET call returning a status 500. The error reported was The table does not exists: hit_counter

The cause for this error was obvious. My database config was using a tablePrefix (line 7):

        'db' => [
            'class' => yii\db\Connection::class,
            'dsn' => 'mysql:host=localhost;dbname=project_db',
            'username' => 'usr_project_db',
            'password' => '************',
            'charset' => 'utf8',
            'tablePrefix' => 't_',
        ],

But the method used to fetch the table name of the Hit Counter's active record in vendor/coderius/yii2-hit-counter/scr/entities/HitCounter.php did not return the table name with the prefix`!

So, we need to change the tableName() function from:

    public static function tableName()
    {
        return 'hit_counter';
    }

to this:

    public static function tableName()
    {
        return '{{%hit_counter}}';
    }

Forked Repository

To make this change we are going to fork the coderius/yii2-hit-counter project.

We have to log onto our GitHub portal and open the repo https://github.com/coderius/yii2-hit-counter. Then click on the Fork icon and create a new fork.

Clone Repository

Now back on our development machine, we clone this newly forked repository

cd /www/dloosley
git clone https://github.com/dloosley/yii2-hit-counter.git

Next, correct the tableName() function in vendor/coderius/yii2-hit-counter/scr/entities/HitCounter.php and commit the change:

git add -A
git commit -m "Corrected AR HitCounter::tableName()"

Using our Forked Repro in our Project

With this bug fixed, we can now update our Yii2 application to use our version of this hit counter library by updating the composer.json file with the following two commands:

First we need to tell composer which vcs (Version Control System) repository to use for yii2-hit-counter.

composer config repositories.hit-counter vcs git@github.com:dloosley/yii2-hit-counter.git

The above command will update composer's repositories section as show below:

    "repositories": {
        "0": {
            "type": "composer",
            "url": "https://asset-packagist.org"
        },
        "hit-counter": {
            "type": "vcs",
            "url": "git@github.com:dloosley/yii2-hit-counter.git"
        }
    }

Next we tell composer to install the coderius/yii2-hit-counter from our master branch.

composer require coderius/yii2-hit-counter:dev-master

Now when we run out hit-counter widget from our Yii2 application, we see no error and our hit-counter table is updated.