【问题标题】:Is there a clean way to set up class autoloading when calling Phing to run a Propel 1 database migration?在调用 Phing 运行 Propel 1 数据库迁移时,是否有一种干净的方法来设置类自动加载?
【发布时间】:2016-03-26 19:29:58
【问题描述】:

我在 Propel 1 中使用migrations,效果很好。对于某些数据操作,我需要访问 Propel 类,并访问父类以获取常用迁移方法,但由于我是从 Phing 请求迁移,这似乎并不简单。

我使用此代码调用迁移:

php \
    /project/backend-app/vendor/phing/phing/bin/phing.php \
    -f /project/backend-app/vendor/propel/propel1/generator/build.xml \
    -Dusing.propel-gen=true \
    -Dproject.dir=/project/backend-app/db \
    -Dpropel.database.url='mysql:dbname=job_crawler_test;host=127.0.0.1' \
    -Dpropel.buildtime.conf.file='buildtime/job_crawler_test.xml' \
    -quiet \
    migrate

只要我在每个需要它的类文件的开头都有自动加载和初始化代码,就可以了:

$root = realpath(__DIR__ . '/../..');
require_once $root . '/vendor/autoload.php';
require_once $root . '/lib/autoload.php';
set_include_path($root . '/lib' . PATH_SEPARATOR . get_include_path());
Propel::init($root . '/config/propel-conf.php');

好的,这行得通,但它有点混乱 - 尽管这是官方推荐(参见上面的手册页链接底部)。为了整洁,我想去掉这个重复的代码块。

我当然可以把它放在一个文件中,并在每个文件中使用一个 require 行,这会减少一些麻烦,但这并不是很令人满意。我想知道是否有一个 -D 标志我可以传递给 Phing,也许就像一个引导 PHP 文件?

我想知道-Dphp.classpath 是否会做点什么,因为这似乎是a Phing core property,但这似乎没有任何区别。

【问题讨论】:

  • 如果 Phing 允许这样的覆盖,你可以使用-Dauto_prepend_file= 由 PHP 提供。
  • 谢谢@mario - 给它一个乐观的尝试,但无济于事。看来available console params的信息有点少!
  • 啊哈,我明白你的意思了 - 将它添加到 php 命令中 - 真是个好主意。试试看……
  • 不错的@mario,已经解决了,php -d 'auto_prepend_file=/path/to/init.php' phing.php ...。你能把这个放在答案中吗?

标签: php migration propel bootstrapping


【解决方案1】:

@mario 在 cmets 中提出了一个善意的建议,完美地解决了这个问题。我将自动加载序言移至单独的脚本:

<?php

/*
 * db/scripts/propel-migration.php
 *
 * Bootstrap file for Propel migrations that need autoloading or access
 * to the Propel sub-system
 */

$root = realpath(__DIR__ . '/../..');
require_once $root . '/vendor/autoload.php';
require_once $root . '/lib/autoload.php';
set_include_path($root . '/lib' . PATH_SEPARATOR . get_include_path());
\Propel::init($root . '/config/JobCrawler-conf.php');

然后我修改了php 调用(即,而不是phing.php 调用),因此:

php \
    -d 'auto_prepend_file=/project/backend-app/db/scripts/propel-migration.php' \
    <rest of call here>

Propel 迁移类现在可以完全自动加载并访问 Propel 的系统,而无需任何类前样板代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-10
    • 2011-06-25
    • 2015-04-28
    • 2023-03-05
    • 1970-01-01
    • 2015-09-19
    相关资源
    最近更新 更多