LoginSignup
1
2

More than 3 years have passed since last update.

symfony 1.4 メモ(モデル)

Last updated at Posted at 2017-03-31
  1. symfony 1.4 メモ(アクション)
  2. symfony 1.4 メモ(context)
  3. symfony 1.4 メモ(モデル)
  4. symfony 1.4 メモ(schemaの書き方)
  5. symfony 1.4 メモ(Doctrine)
  6. symfony 1.4 メモ(フォーム)
  7. symfony 1.4 メモ(コマンド)
  8. symfony 1.4 メモ(ユーティリティ)

モデルClassの継承

Classの継承
Model > BaseModel > sfDoctrineRecord > Doctrine_Record > Doctrine_Record_Abstract > Doctrine_Access > Doctrine_Locator_Injectable ...

ProjectConfiguration にて Dctrine のオプションを変更することができる。
そのうちの baseClassName を使えばオリジナルのモデルClassを承継に組み込むことができる。

ProjectConfiguration.class.php
class ProjectConfiguration extends sfProjectConfiguration
{
    public function setup()
    {
        $this->enablePlugins('sfDoctrinePlugin');
    }

    public function configureDoctrine(Doctrine_Manager $manager)
    {
        $manager->setAttribute(Doctrine_Core::ATTR_USE_DQL_CALLBACKS, true);
        $manager->setAttribute(Doctrine_Core::ATTR_AUTO_ACCESSOR_OVERRIDE, false);
        sfConfig::set
        (
            'doctrine_model_builder_options',
            array
            (
                'baseClassName' => 'originalDoctrineRecord',
            )
        );
    }
}
Classの継承
Model > BaseModel > originalDoctrineRecord > sfDoctrineRecord > Doctrine_Record > Doctrine_Record_Abstract > Doctrine_Access > Doctrine_Locator_Injectable ...

ちなみにデフォルトの Dctrine のオプション設定は以下

'generateBaseClasses'  => true,                 // 基底クラスを生成するかどうか
'generateTableClasses' => true,                 // テーブルクラスを生成するかどうか
'packagesPrefix'       => 'Plugin',
'suffix'               => '.class.php',         // 生成クラス用に使うサフィックス
'baseClassesDirectory' => 'base',               // 基底クラスを生成するディレクトリ
'baseClassName'        => 'sfDoctrineRecord',   // モデルが継承するスーパークラス

ビヘイビア

schema ファイルで actAs を定義しているモデルは
自動的に以下の機能を有している。

Timestampable:

自動的にcreated_atとupdated_atフィールドが生成され、
生成時、更新時にそれぞれ自動更新される。

SoftDelete:

自動的にdeleted_atフィールドが生成され、deleteメソッドを使って削除すると日付が入り、論理削除される。
論理削除されたデータは問い合わせてもヒットしない。

オリジナルのビヘイビアも設定可能

schema.yml
ModelName:
  actAs:
    Timestampable:      ~
    SoftDelete:         ~
    originalBasicModelTemplate: ~

デフォルトで用意されているメソッド

// プロパティを追加できる。
$this->mapValue('updater');

// そのオブジェクトが新規か更新かを判別できる。
$this->isNew()

// 更新されたフィールドがあるかどうか。
$this->isModified()

// イベントの前後に処理を追加したいときに使う。
$this->preSave($event)
$this->postSave($event)
$this->preDelete($event)
$this->postDelete($event)
$this->preUpdate($event)
$this->postUpdate($event)
$this->preInsert($event)
$this->postInsert($event)

// モデルのプロパティを配列にしたものを取得する。
$this->toArray($deep = true, $prefixKey = false)

// 配列でモデルのプロパティを更新する。
$this->fromArray(array $array, $deep = true)

// モデルをtypeで指定した形式にしたものを取得する。type: array, xml, yml, json
$this->exportTo($type, $deep = true)

// typeで指定した形式のデータでモデルのプロパティを更新する。
$this->importFrom($type, $data, $deep = true)

// メモリ解放。
$this->free()

1
2
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
1
2