简体   繁体   中英

Default Fields and values in MongoDB, using cakePHP

I am building an application with cakephp 2.1 and mongodb 2.03 , I am using ishikaway's mongodb datasource

I need to set some default values that would be added to a model , I am doing it like so

<?php
class DynamicFormResponse extends AppModel{
    public $useDbConfig = 'mongodb';  
    public $useTable = 'dynamicFormsResponse';
    public $primaryKey = '_id';
    public $validate=array();

    public $mongoschema = array(
        'created' => array('type' => 'datetime'),
        'modified' => array('type' => 'datetime'),
        'escalation'=>array(
            'type'=>"integrer",
            "default"=>0
            ),
        "status"=>array(
            "type"=>"string",
            "default"=>"pending"
            ),

    );

    public function setSchema($schema) {
        $this->_schema=$schema;        
    }
    public function getSchema(){
        return $this->_schema;
    }

}

Obviously I cannot set default values directly in MongoDb like MySQL, and obviously since I am asking the question the above method is not working.

Any suggestions on how I can solve this ?

Ps:

EDIT:

I have currently solved this problem by committing an MVC sin,

I am adding the default values in the controller before saving the data with the model

<?php
class DynamicFormResponse extends AppModel {
    public $name="DynamicFormResponse";
    public $useDbConfig = 'mongodb';
    public $useTable = 'dynamicFormResponse';
    public $primaryKey = '_id';
    public $validate = array();

    public function getDefaults(){
        $defaultValues=array(
            "escalation"=>0,
            "status"=>"pending",
            "department_id"=>NULL,
            "user_agent"=>env("HTTP_USER_AGENT")
        );
        return $defaultValues;
    }

  ...
  ...
class DynamicFormsController extends AppController {
    ...
    ...

    public function getForm($id=null){
    ...
    ...
        /**
         * Set defaults values
         */
        foreach ($this->DynamicFormResponse->getDefaults() as $fieldName => $defaultValue) {
                if (empty($this-> request-> data[$this-> DynamicFormResponse -> alias][$fieldName]))
                     $this->request->data[$this-> DynamicFormResponse -> alias][$fieldName] = $defaultValue;
        }
        /**
         * Data Validation 
         */
        if($this->DynamicFormResponse->save($this->request->data) == true ){
            $this->set("ticket_id",  $this->DynamicFormResponse->id);
            $this->render('ticket_successfully_saved');
            return;
        }

Is there a better solution ? because that seems like a bad way to do it .

it's not really a mongoDB question but anyways, i suggest you to merge your userdata with your default values in beforeSave().

We declare default values in each Model like this:

public $defaultValues = array(
    'report' => 't',
    'reportinterval' => '7',
    'type' => '0'
);

And merge it in beforeSave():

/**
 * Extends beforeSave() to add default values
 *
 * @param array $options
 * @return bool
 */
public function beforeSave($options = array()) {
    // Add default values if not set already
    foreach ($this->defaultValues as $fieldName => $defaultValue) {
        if (empty($this->data[$this->alias][$fieldName]))
        $this->data[$this->alias][$fieldName] = $defaultValue;
        }
    return parent::beforeSave($options);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM