We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Laravel Inspired Devtools: Migrations, Model Factories, Database Seeding

Laravel Inspired Devtools for Phalcon

https://github.com/zachleigh/yarak
Currently, I've got migrations, model factories, database seeding, and user defined custom commands done. Here's a run down of each feature:

Migrations

  • Migration files are in a single directory and are timestamped to keep them in order.
  • Just like the migrations in the official devtools, migrations use Phalcon's Database Abstraction Layer to interact with the database.
  • Migrations can be rolled back in steps.
  • Migrations can be used to reset and refresh the database.

Model Factories

  • Model factories allow you to create model instances and persist them on the fly. If you want three test users, simply do this:
    • factory(Users::class, 3)->create();
  • Factories make use of the Faker library to create sample test data.
  • You can create multiple factories for a single model and name them. This is very useful if you want to do something specific like create an admin user and then a guest user.
  • Default model factory logic can be overridden to create custom models that don't use Faker.
    • factory(Users::class)->create(["username" => "bobsmith"]);

Database Seeding

  • The database seeding component allows you to instantly create test data or even seed a production database.
  • Seeders can call other seeders so you can better organize seeding logic.
  • Integrated with the migration component to allow seemless database reset. Want to rollback everything and re-seed? No problem:
    • php yarak migrate --refresh --seed

Custom Commands

  • You can create your own commands that can be called just like any other Yarak command.
  • Yarak commands wrap around the Symfony console component and allow you to write commands in a simple manner. Here's an example command definition:
    • protected $signature = "spell {word=example} {--b|backwards}";
  • Call commands exactly as you write them:
    • php yarak spell myWord --backwards

I'd love to get some feedback on it. If there's anything you think I can do better or have suggestions, please let me know!



85.5k

superb man. well done!

Yea looks nice, but i just wonder why not make changes into phalcon devtools?

Yea looks nice, but i just wonder why not make changes into phalcon devtools?

The main reason I created this package was the migrations. They are structured completely differently than the official devtools migrations so would have been incompatable. Because migrations are so closely tied to the other functionalities, it made sense to add them to this project. However, the logic that performs the actions is separated from the command line interface so the different components should be easy to extract and integrate into the official devtools if somebody wanted to.

Well for me migrations in phalcon devtools are fine enough.

I added custom, user defined commands to this. You can now create custom commands by using a simple, easy to use interface. This is the example command from the project:

use Yarak\Console\Command;

class ExampleCommand extends Command
{
    /**
     * The command signature.
     *
     * @var string
     */
    protected $signature = 'example
                            {word=example : A word of your choice.}
                            {--b|backwards : Spell the word backwards}';

    /**
     * The command description.
     *
     * @var string
     */
    protected $description = 'Basic example command.';

    /**
     * Handle the command.
     */
    protected function handle()
    {
        $word = ucfirst($this->argument('word'));

        if ($this->option('backwards')) {
            $this->spellBackwards($word);
        } else {
            $this->spell($word);
        }
    }

    /**
     * Spell the word backwards.
     *
     * @param  string $word
     */
    protected function spellBackwards($word)
    {
        $backwards = strrev($word);

        $spelling = strtolower(implode('-', str_split($backwards)));

        $this->output->writeInfo("{$word} spelled backwards is {$spelling}.");
    }

    /**
     * Spell the word.
     *
     * @param  string $word
     */
    protected function spell($word)
    {
        $spelling = strtolower(implode('-', str_split($word)));

        $this->output->writeInfo("{$word} is spelled {$spelling}.");
    }
}

I've been thinking about making this even more modular than it already is. If I build a basic command system and a component registration system, then people can easily create addons for it and all the components(migrations etc.) can be installed spearately. This would make it much more customizable and wouldn't force people to use components they don't want. Thoughts?

Really good package with a lot of potential! Finally something that really helps testist! Phalcon is really behind in this.

Probably in the medium/long run this whole package should be merged into the dev tools, but there is still much work to do. I hope the whole community can contribute! :)

We definitely need a good seeding tool in Phalcon.

HI Guys, I am new to phalcon and I have a question. I want to seed data to my tables. Can I use this with palcon 4.0? Is this compatible with it? If not, could you point me out to the right direction? Many Thanks, Adam