如何在Yii MongoDB Suite中保存嵌入文档

时间:2013-09-23 10:05:11

标签: php mongodb yii yii-extensions

我有两个班级“users”和“userName

class users extends EMongoDocument {

public $name;
public $address;

public static function model($className = __CLASS__) {
    return parent::model($className);
}

// This method is required!
public function getCollectionName() {
    return 'users';
}

public function getMongoDBComponent() {
    return Yii::app()->mongodb;
}

public function behaviors() {
    return array(
        array(
            'class' => 'ext.YiiMongoDbSuite.extra.EEmbeddedArraysBehavior',
            'arrayPropertyName' => 'name', // name of property
            'arrayDocClassName' => 'userName' // class name of documents in array
        ),
    );
}

public function rules() {
    return array(
        array('name, address', 'required'),
        array('name, address', 'length', 'max' => 255),
    );
}

public function attributeLabels() {
    return array(
        'name' => 'Full name',
        'address' => 'Address',
    );
}
}

class userName extends EMongoEmbeddedDocument {

public $firstname;
public $middlename;
public $lastname;

public static function model($className = __CLASS__) {
    return parent::model($className);
}

// This method is required!
public function getCollectionName() {
    return 'userName';
}

public function rules() {
    return array(
        array('firstname, middlename,lastname', 'required'),
        array('firstname, middlename,lastname', 'length', 'max' => 255),
    );
}

public function attributeLabels() {
    return array(
        'firstname' => 'First Name',
        'middlename' => 'Middle Name',
        'lastname' => 'Last Name',
    );
}
}

我有代码:

enter image description here

我无法保存嵌入文件。我有一个错误:

mb_strlen() expects parameter 1 to be string, array given

enter image description here

1 个答案:

答案 0 :(得分:0)

您有name属性的最大长度验证规则。此规则使用函数mb_strlen。但由于某种原因你写了$u->name[0] = new UserName(),所以你name属性变成了一个数组,但不是一个字符串。这就是原因。