Php 调用Phing运行Propel 1数据库迁移时,是否有一种干净的方法来设置类自动加载?

Php 调用Phing运行Propel 1数据库迁移时,是否有一种干净的方法来设置类自动加载?,php,migration,propel,bootstrapping,Php,Migration,Propel,Bootstrapping,我用的是Propel 1,它工作得很好。对于某些数据操作,我需要访问spreep类,对于常见的迁移方法,我需要访问父类,但是由于我请求从Phing进行迁移,所以这似乎非常重要 我使用以下代码调用迁移: php \ /project/backend-app/vendor/phing/phing/bin/phing.php \ -f /project/backend-app/vendor/propel/propel1/generator/build.xml \ -Dusing

我用的是Propel 1,它工作得很好。对于某些数据操作,我需要访问spreep类,对于常见的迁移方法,我需要访问父类,但是由于我请求从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
是否会做些什么,因为这似乎是,但这似乎没有任何区别。

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

<?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');

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

如果Phing允许这样的覆盖,您可以使用PHP提供的
-Dauto\u prepend\u file=
。感谢@mario-给了它一个乐观的尝试,但没有任何效果。似乎有关的信息有点稀疏!啊哈,我明白你的意思了-将它添加到
php
命令中-这是个好主意。尝试一下…很好的一个@mario,这是固定的,使用
php-d'auto\u prepend\u file=/path/to/init.php'phing.php…
。请你把这个问题写在回信里好吗?
php \
    -d 'auto_prepend_file=/project/backend-app/db/scripts/propel-migration.php' \
    <rest of call here>