如何在将数据保存到 MongoDB 之前将变量设置为集合,即在beforeSave()
方法中
假设我有控制器
class BooksController extends AppController
{
$model_name = 'Books';
function save() {
if($this->request->is('post')) {
$data = $this->request->data['forms'];
$this->$model_name->save($data); // Already loaded the model
}
}
}
和型号
class Books extends AppModel
{
function beforeSave($options = array()) {
$this->data['Books']['active'] = '1';
// Tried this as well but found unstructured (key,value) pair `$this->data['active'] = '1';`
//print_r($this->data) found correct format but active field is not getting saved in MongoDB
return true;
}
}
请让我知道需要进一步澄清。任何帮助真的很感激!