LoginSignup
9
10

More than 5 years have passed since last update.

SimpleAuthのリレーション

Last updated at Posted at 2014-06-02

はじめに

FuelPHPを使い始めてだいぶたちました。
最近、SimpleAuthをよく使うのですが、ユーザとそれが投稿した記事とでリレーションを組みたいなあと思っていました。その解決策を紹介します。

Model_Userを作成

APPPATH/classes/model/user.php
<?php

class Model_User extends \Orm\Model
{
    protected static $_properties = array(
        'id',
        'username',
        'group',
        'profile_fields' => ['data_type' => 'serialize'],
    );

    protected static $_observers = array(
        'Orm\Observer_Typing' => ['events' => ['after_load']],
    );

    protected static $_table_name = 'users';

}

解説

  1. 'password'は読み込むことを想定していないのでプロパティから削除します。
  2. 'profile_fields'はPHPの配列がシリアライズされているので、データタイプを指定します。
  3. そして、Observer_Typingを追加します。これは、データタイプに従ってデータを加工してくれます。ロードした後に加工してくれるよう、'after_load'のみ指定します。

読み込み専用なので、ユーザ情報を変更したり追加したり削除したりする際はAuth::を使いましょう。

参考

9
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
10