0

For something I'm writing, I want to get an empty copy of a model, or a copy with default values.

So, normally it might return something like

array(
  'id' => '4',
  'name' => 'Object name',
  'somekey' => 'Some key\'s value',
  'someotherkey' => '1',
  'created' => '2012-04-22 17:11:31',
  'modified' => '2012-04-22 17:11:31'
)

Instead I would like it to return an array with the keys intact, but the values empty completely, or even better (if possible) with a default values that I could specify in the model itself. Is this possible?

4

2 に答える 2

1

デフォルト値が入力されたユーザーを作成するだけの場合は、を使用できますModel::create()。データベースに定義したデフォルト値をスキーマで検索し、それに応じてレコードデータを入力します。

于 2012-04-28T23:14:46.017 に答える
1

Model メソッドschema()を探しているようです。これにより、すべてのテーブル フィールド/タイプが返されます。例えば:

class User extends AppModel {

    public function emptyDataset() {
        $fields = $this->schema();

        if(is_array($fields)) {
            return array_keys($fields);
        } else {
            return false;
        }
    }

}
于 2012-04-28T20:04:24.473 に答える