0

我在 Cake 2.3.5 上使用这个:https ://github.com/ichikaway/cakephp-mongodb

数据库配置

public $mongo = array(
    'datasource' => 'Mongodb.MongodbSource',
    'database' => 'hello_forms',
    'host' => 'localhost',
    'port' => 27017,
    'persistent' => 'true',
);

该模型:

<?php
class Form extends AppModel {
    var $name = 'Form';
    var $primaryKey = '_id';
    var $useDbConfig = 'mongo';
    var $useTable = false;

    public $validate = array(
        'name' => array(
            'rule' => array('notEmpty'),
            'message' => 'A list name is required.',
            'required' => true
        ),
    );

    public function schema() {
        $this->_schema = array(
            '_id' => array('type' => 'integer', 'null' => false, 'default' => NULL, 'length' => 40, 'key' => 'primary'),
            // '_id' => array('type' => 'integer', 'key' => 'primary',  'length' => 40),
            'name' => array('type' => 'string'),
            'unique_string' => array('type' => 'string'),
            'account_id' => array('type' => 'integer'),
            'description' => array('type' => 'string'),
            'success_text' => array('type' => 'string'),
            'success_redirect' => array('type' => 'string'),
            'created'=>array('type'=>'datetime'),
            'modified'=>array('type'=>'datetime'),
            'form_elements' => array(
                array(
                    'field_description' => array('type' => 'string'),
                    'field_label' => array('type' => 'string'),
                    'field_type' => array('type' => 'string'),
                    'weight' => array('type' => 'integer'),
                    'date_format' => array('type' => 'string'),
                    'is_required' => array('type' => 'boolean'),
                    'allow_duplicates' => array('type' => 'boolean'),
                    'meta' => array(
                        array(
                            'meta_label' => array('type' => 'string'),
                            'meta_default_value' => array('type' => 'string'),
                            'meta_checked' => array('type' => 'boolean')
                        )
                    )
                ),
            )
        );
        return $this->_schema;
    }
}

错误:

<b>Notice</b> (8)</a>: Undefined index: length [<b>CORE\Cake\Model\Model.php</b>, line <b>1793</b>]

我一直在努力解决这个问题,但我没有想法。我找到了这个:https ://cakephp.lighthouseapp.com/projects/42648/tickets/3138-undefined-index-length-on-modelsave但我不知道如何“处理”该页面上的信息。

声明架构时我错过了什么吗?我试图为所有字段添加“长度”,但仍然出现该错误。是什么赋予了?

编辑:只是为了澄清,因为它只是 a Notice,数据仍被保存到 MongoDB 中。但是,它弄乱了我的 JSON 数据。

4

1 回答 1

2

我在提交这个问题后几分钟就想通了 X_X

事实证明,我使用的是“旧方式”来声明模式。我正在使用 Mark Story 的示例: http: //mark-story.com/posts/view/using-mongodb-with-cakephp,它已经过时了。正确的方法是这样的:https ://github.com/ichikaway/cakephp-mongodb/blob/cake2.2/samples/Model/Post.php

TL;DR 如果帖子有 2 年历史,我应该阅读评论:|

<?php
class Form extends AppModel {
    var $name = 'Form';
    var $primaryKey = '_id';
    var $useDbConfig = 'mongo';

    var $mongoSchema  = array(
        'name' => array('type' => 'string'),
        'unique_string' => array('type' => 'string'),
        'account' => array('type' => 'integer'),
        'description' => array('type' => 'string'),
        'success_text' => array('type' => 'string'),
        'success_redirect' => array('type' => 'string'),
        'created'=>array('type'=>'datetime'),
        'modified'=>array('type'=>'datetime'),
        'form_elements' => array(
            array(
                'field_description' => array('type' => 'string'),
                'field_label' => array('type' => 'string'),
                'field_type' => array('type' => 'string'),
                'weight' => array('type' => 'integer'),
                'date_format' => array('type' => 'string'),
                'is_required' => array('type' => 'boolean'),
                'allow_duplicates' => array('type' => 'boolean'),
                'meta' => array(
                    array(
                        'meta_label' => array('type' => 'string'),
                        'meta_default_value' => array('type' => 'string'),
                        'meta_checked' => array('type' => 'boolean')
                    )
                )
            )
        )
    );

    public $validate = array(
        'name' => array(
            'rule' => array('notEmpty'),
            'message' => 'A list name is required.',
            'required' => true
        ),
    );
}
于 2013-05-23T02:58:42.853 回答