havvg/propel-uniquekey-instancepooling-behavior

UniqueKeyInstancePoolingBehaviors 为操作唯一键的findOne查询方法添加了额外的实例池。

v0.9.2 2013-04-05 09:51 UTC

This package is not auto-updated.

Last update: 2024-09-14 14:06:03 UTC


README

Build Status

有关如何安装第三方行为的详细信息,请参阅Propel文档:安装第三方行为

用法

只需在您的 schema.xml 文件中添加以下XML标签:

<behavior name="unique_key_instance_pooling" />

例如

<database name="default" defaultIdMethod="native">
    <table name="user">
        <column name="id" type="integer" autoIncrement="true" primaryKey="true" />
        <column name="email" type="varchar" size="255" required="true" primaryString="true" />

        <behavior name="unique_key_instance_pooling" />

        <unique>
            <unique-column name="email" />
        </unique>
    </table>
</database>

该行为将为每个唯一键添加两个方法:

  1. 一个静态密钥生成器;例如,这个就是 createUniquePoolingKeyForEmail。此方法返回用于访问实例池的密钥。

  2. 将实现 findOneByEmail 方法。此方法将原始方法包装在实例池周围。

<?php

$email = 'mail@example.com';

/*
 * This retrieves the user from the database.
 * The hydrated object will be put into the instances pool under a special key.
 */
$user = UserQuery::create()->findOneByEmail($email);

// .. more code ..

/*
 * As this user is in the instances pool of the behavior,
 * the cached object will be returned without accessing the database.
 */
$user = UserQuery::create()->findOneByEmail($email);