プロが教える店舗&オフィスのセキュリティ対策術

PHP5.2.4を使用しています。
Sabelフレームワーク内でのトランザクションの記述のサンプルというか例を
教えて頂きたいのですが、以前はHPが見れてそのサンプルを見れたような気がするのですが、
今はアクセスできる状態じゃなく(http://www.sabel.jp/)コミュニティのようなものもあるわけではないので
ここで質問させて下さい。

app/logics/aspects/Transaction.php

class Logics_Aspects_Transaction implements Sabel_Aspect_MethodInterceptor
{
public function invoke(Sabel_Aspect_MethodInvocation $inv)
{
if (!$active = Sabel_Db_Transaction::isActive()) {
Sabel_Db_Transaction::activate();
}

try {
$result = $inv->proceed();

if (!$active) {
Sabel_Db_Transaction::commit();
}

return $result;
} catch (Exception $e) {
if (!$active) {
Sabel_Db_Transaction::rollback();
}

throw $e;
}
}
}

詳しくは分からないのですがここにトランザクションのロジックがあるようなのですが、
これを例えばデータベースのデータを保存するさいにどのような記述で使えばよいのか
というサンプルを示して頂けないでしょうか?


ここでダウンロードができます
Sabel ドキュメントとか(非公式)
http://ebine.org/sabel/doc/index.html

A 回答 (1件)

過去のドキュメントを参考URLに記載しました。



class TransactionConfig extends Sabel_Container_Injection
{
public function configure()
{
$this->aspect("User")->advice("TransactionAdvice");
}
}

class TransactionAdvice
{
/**
* @around movePoint
*/
public function processTransaction($invocation)
{
Sabel_DB_Transaction::activate(); # トランザクション有効化

try {
$result = $invocation->proceed();

Sabel_DB_Transaction::commit(); # 正常終了

return $result;
} catch (Exception $e) {
Sabel_DB_Transaction::rollback(); # 例外が発生したらロールバック
throw $e;
}
}
}

class User extends Sabel_DB_Model
{
public function movePoint()
{
$fromUser = MODEL("User", 1);

if ($fromUser->point < $point) {
throw new ...
} else {
$toUser = MODEL("User", 2);
if ($toUser->isValid()) {
...
} else {
throw new ...
}
}
}
}

として各クラスを、クラスパス上に配置します。

下記利用時のサンプルです。

$user = load("User", new TransactionConfig());
$user->movePoint();

とすれば、TransactionAdviceによるアスペクト処理が実行されます。

これは、@around movePoint としてアドバイスクラスにmovePointメソッドが指定されているので、movePoint()実行時の周辺(around)、つまりメソッドの実行前と実行後にアスペクトが介入します。

参考URL:http://old.sabel.php-framework.org/doc/container …
    • good
    • 0
この回答へのお礼

ご返答ありがとうございます。
そのようなページがあったんですか。
まだ使い方を完璧に把握したわけではありませんが、
とりあえず試してみたらできました。ありがとうございます。

お礼日時:2009/10/30 21:36

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!