emiliomg/propel-provider-behavior

该软件包已被 废弃,不再维护。没有推荐替代软件包。
最新版本(1.1.2)的软件包没有可用的许可证信息。

这是一个 Propel ORM 行为,它添加非静态提供者来获取新的查询对象、新的模型、同伴和连接。

1.1.2 2015-08-24 09:26 UTC

This package is not auto-updated.

Last update: 2020-10-16 20:55:01 UTC


README

一个 Propel ORM 行为,为您提供查询/模型/同伴对象的提供者。

需求

  • Propel > 1.6.0

安装

通过您的 composer.json 文件将此行为添加到项目中

"require": {
    "emiliomg/propel-provider-behavior": "~1.0"
}

或者简单地运行 composer require emiliomg/propel-provider-behavior

然后,将以下配置添加到您的 build.properties

propel.behavior.providerBase.behavior = vendor.emiliomg.propel-provider-behavior.src.ProviderBaseBehavior.ProviderBaseBehavior
propel.behavior.providerFassade.behavior = vendor.emiliomg.propel-provider-behavior.src.ProviderFassadeBehavior.ProviderFassadeBehavior
propel.behavior.default = providerBase, providerFassade

propel.behavior.default 确保您的每个模型都使用此行为。

最后,重新构建您的模型。提供者类将与您的模型和查询类一起生成。

配置

您可以使用 propel.behavior.provider.cachefile = true 开关。这将在输出目录中生成一个 providerCache.json 文件,其中包含所有生成的提供者列表。这很有用,例如,用于自动生成工厂(Symfony2 Bundle 会自动为您生成)。

生成的代码

生成的提供者代码相当简单。我们使用一个简单的 schema.xml 如此

<?xml version="1.0" encoding="utf-8"?>
<database name="bookstore" defaultIdMethod="native" namespace="Foo\Bar\Model">
    <table name="Author" idMethod="native">
        <column name="id" type="INTEGER" primaryKey="true" autoIncrement="true" required="true"/>
        <column name="name" type="VARCHAR" size="255" required="false"/>
        <vendor type="mysql">
            <parameter name="Engine" value="InnoDB"/>
        </vendor>
    </table>
</database>

一个虚构的 Author 模型的输出示例如下

BaseAuthorProvider

<?php

namespace Foo\Bar\Model\om;

use \Propel;
use Foo\Bar\Model\Author;
use Foo\Bar\Model\AuthorPeer;
use Foo\Bar\Model\AuthorQuery;

/**
 * This is a provider base class for the 'Author' table.
 * It provides query, model, peer and connection instances.
 * You can use Dependency Injection to inject this provider into services who need
 * 'Author'-queries or -models and fetch them via this provider.
 * There is no need to access the models or queries directly, since this provider can be mocked.
 */
abstract class BaseAuthorProvider
{
    /**
     * Returns a new query instance.
     *
     * @param     string $modelAlias The alias of a model in the query
     * @param   AuthorQuery|Criteria $criteria Optional Criteria to build the query from
     *
     * @return AuthorQuery
     */
    public function getQuery($modelAlias = null, $criteria = null)
    {
        $query = AuthorQuery::create($modelAlias, $criteria);

        return $query;
    }

    /**
     * Returns a new Model instance.
     *
     * @return Author
     */
    public function getModel()
    {
        $model = new Author();

        return $model;
    }

    /**
     * Returns a new Peer Instance;
     *
     * @return AuthorPeer
     */
    public function getPeer()
    {
        $peer = new AuthorPeer();

        return $peer;
    }

    /**
     * Returns the connection for this table.
     *
     * @return \PropelPDO
     */
    public function getConnection()
    {
        $peer = $this->getPeer();
        $connection = Propel::getConnection($peer::DATABASE_NAME);

        return $connection;
    }

}

AuthorProvider

行为还会生成 fassade 类,如果存在没有 fassade 类,则会生成。一个 fassade 类看起来像这样

<?php

namespace Foo\Bar\Model;

use Foo\Bar\Model\om\BaseAuthorProvider;

/**
 * This is a provider fassade class for the 'Author' table.
 * It provides query, model, peer and connection instances.
 * You can use Dependency Injection to inject this provider into services who need
 * 'Author'-queries or -models and fetch them via this provider.
 * There is no need to access the models or queries directly, since this provider can be mocked.
 *
 * You should add additional methods to this class to meet the
 * application requirements.  This class will only be generated as
 * long as it does not already exist in the output directory.
 */
class AuthorProvider extends BaseAuthorProvider
{

}