Skip to content

How to change the schema of saving data in Model::beforeSave()

ichikaway edited this page May 6, 2011 · 1 revision

When you don't use Model::mongoSchema property and want to add other keys(columns) in Model::beforeSave(), It needs to call Model::schema() before return in Model::beforeSave().

<?php
class Foo extends AppModel {
 function register() {
      $data['Foo'] = array('name' => 'test', 'age' => 22);
      $this->set($data);
      return $this->save();
 }

 function beforeSave() {
      $this->data['Foo']['count'] = 1;
      $this->data['Foo']['modified'] = new MongoDate();
      $this->schema(true); 
      return true;
 }
}