Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7722819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:17:10+00:00 2026-06-01T04:17:10+00:00

I am building an application with cakephp 2.1 and mongodb 2.03 , I am

  • 0

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:

  • This did not help CakePHP – Set Default Field Values in the Model
  • The documentation is not clear either
  • Full code available here

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 .

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T04:17:11+00:00Added an answer on June 1, 2026 at 4:17 am

    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);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am building an application that will need to send emails on numerous occasions
I'm building a web application, and I need to use an architecture that allows
I am building a PHP application with CakePHP 2 . I need to have
I am building a web application using CakePHP. I am using meioupload to upload
I am building application that required some data from iPhone's Call log(read only). The
I'm building a web application in CakePHP. I'm using the Paginator object to render
I'm building a prototype search application using CakePHP and I have a search controller
I'm building an application in C# using WPF. How can I bind to some
I am building an application which would store some information into the database and
(SOLVED) I'm building an application that can create some of its control dynamically, based

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.