micskubence
micskubence

Reputation: 11

Using Phalcon models inside migrations

Iam trying to use my phalcon models inside migrations like you can in laravel. The problem is with namespaces. I try to load the namespaces with loader to solve the namespace issues by calling a loader in my migration config that seems to solve namespacing but i get the error Fatal Error: A dependency injection container is required to access the services related to the ODM when trying to use my models in my migration files

<?php

use Phalcon\Autoload\Loader;
/*  
   * Modified: preppend directory path of current file, 
      because of this file own different ENV under between Apache and command line.  
   * NOTE: please remove this comment.  
*/

if (!file_exists(__DIR__ . '/config.php'))
    throw new Exception("config.php not exists, run composer copy-config!");
$config = require_once 'config.php';
defined('BASE_PATH') || define('BASE_PATH', getenv('BASE_PATH') ?: realpath(dirname(__FILE__) . '/..'));
defined('APP_PATH') || define('APP_PATH', BASE_PATH . '/app');

$loader = new Loader(true);
$loader->setNamespaces([
    'Api\WarrantyTicket\Models' => APP_PATH . '/api/warranty-ticket/models',
]);
$loader->register();

$db_config = [
    'adapter' => 'Mysql',
    'charset' => 'utf8',
];
return new \Phalcon\Config\Config([
    'database' => array_merge($db_config, $config['dbconnection'][APP_ENV]),

    'application' => [
        'logInDb' => true,
        'migrationsDir' => BASE_PATH . '/migrations/',
    ]
]);

migrations config above

test migration

<?php

use Api\WarrantyTicket\Models\WarrantyTicket;
use Phalcon\Migrations\Mvc\Model\Migration;

/**
 * Class TestMigration_105
 */
class TestMigration_105 extends Migration
{
    /**
     * Run the migrations
     *
     * @return void
     */
    public function up(): void
    {
        var_dump(get_declared_classes());
        $log = new WarrantyTicket();
        $log->assign([
            'crm_entity_id' => 1,
            'crm_module' => 'Test',
            'incoming_json' => 'date',
            'created' => date('Y-m-d H:i:s'),
            'created_by' => 1
        ]);
        $log->save();
    }

    /**
     * Reverse the migrations
     *
     * @return void
     */
    public function down(): void
    {
    }
}

i tried using loader to solve namespace but cant get through the di error

Upvotes: 1

Views: 47

Answers (0)

Related Questions