如何在正确的目录中使用名称空间构建推进模型?

How to make propel build models with namespaces in the right directory?

我的 类 的默认目录是:app/

Composer 自动加载配置为:

{
    "autoload": {
        "psr-4": { "App\": "app/" }
    }
}

在 propel 的 schema.xml 上,我的命名空间是 "App\Models"

<database name="default" defaultIdMethod="native"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:noNamespaceSchemaLocation="http://xsd.propelorm.org/1.6/database.xsd"
          namespace="App\Models"
        >
    <table name="user">
        <column name="id" type="integer" required="true" primaryKey="true" autoIncrement="true"/>
        <column name="email" type="varchar" size="255" required="true"/>
        <column name="password" type="varchar" size="60" required="true"/>
        <column name="country_id" type="integer" required="true"/>
        <column name="timezone" type="varchar" size="50" required="true"/>
        <foreign-key foreignTable="country">
            <reference local="country_id" foreign="id"/>
        </foreign-key>
    </table>

    <table name="country">
        <column name="id" type="integer" required="true" primaryKey="true" autoIncrement="true"/>
        <column name="name" type="varchar" size="128" required="true"/>
    </table>
</database>

我的propel.php.dist是:

<?php
return [
    'propel' => [
        'paths' => [
            // The directory where Propel expects to find your `schema.xml` file.
            'schemaDir' => 'database/schemas',

            // The directory where Propel should output generated object model classes.
            'phpDir' => 'app/Models'
        ]
    ]
];

当我执行vendor/bin/propel model:build时,结果结构是这样的:

/app/
|___Models/
    |___App/
        |___Models/
            |___Base/
            |___Map/
            |___Country.php
            |___CountryQuery.php
            |___User.php
            |___UserQuery.php

我需要这样的结构:

/app/
|___Models/
    |___Base/
    |___Map/
    |___Country.php
    |___CountryQuery.php
    |___User.php
    |___UserQuery.php

我尝试了本手册中的所有组合:Database Attributes

是否有不同的方法来解决这个问题?

提前致谢。

查看您的 build.properties 文件。

你应该有类似这样的东西:

propel.packageObjectModel               = true
propel.generator.targetPackage          = App\Models

这是一种按要求工作的方法:

composer.json

}
    "autoload": {
        "classmap": ["app/"]
    }
}

... 告诉 Composer 在此文件夹中自动加载 类

propel.php.dist

<?php
return [
    'propel' => [
        'paths' => [
            // The directory where Propel expects to find your `schema.xml` file.
            'schemaDir' => 'database/schemas',

            // The directory where Propel should output generated object model classes.
            'phpDir' => 'app'
        ]
    ]
];

... 告诉 Propel 将您的模型 类 放在 Composer 将自动加载的同一文件夹(应用程序)中

schema.xml

<database ... namespace="Models">
    ...
</database>

... 告诉 Propel 为所有模型使用 Models 命名空间

然后你可以使用任何模型

use \Models\Country;
new Country();

您必须更改 schema.xml 中的数据库命名空间,您必须将命名空间设置为:

命名空间="\app\Models"

您想将 generator.namespaceAutoPackage 设置为 "false"。然后确保您仍然将 paths.phpDir 设置为 "app/Models" 并在 schema.xml 文件中,将数据库命名空间设置保持为 "App\Models".

http://propelorm.org/documentation/reference/configuration-file.html#generator-settings

问题中指定的配置看起来是正确的,除了缺少一件东西,这使得它在我的情况下有效:将 namespaceAutoPackage 设置为 false。对于问题的 php 配置文件,它看起来像这样:

<?php
return [
    'propel' => [
        'paths' => [
            // The directory where Propel expects to find your `schema.xml` file.
            'schemaDir' => 'database/schemas',

            // The directory where Propel should output generated object model classes.
            'phpDir' => 'app/Models'
        ],
        'generator' => [
            'namespaceAutoPackage' => 'false'
        ]
    ]
];