unable to save PHP objects in mongodb

浪尽此生 提交于 2019-12-07 13:32:55

问题


Getting 'zero-length keys are not allowed' FATAL error while saving object.

Is it due to presence of __construct() ?

I believe PHP object is allowed in save() method.

class Address{
    private $name;
    private $company;
    private $zip;

    public function __construct($name,$company,$zip){
        $this->name = $name;
        $this->company = $company;
        $this->zip = $zip;
    }
}

$newAddress = new Address("james","google",678);

print_r($newAddress);
// Address Object ( [name:Address:private] => james [company:Address:private] =>
// google [zip:Address:private] => 678 )

$addresses->save($newAddress);    

Fatal error: Uncaught exception 'MongoException' with message 'zero-length keys are not allowed, did you use $ with double quotes?' in /var/www/html/index.php:105 Stack trace: #0 /var/www/html/index.php(105): MongoCollection->save(Object(Address)) #1 {main} thrown in /var/www/html/index.php on line 105


回答1:


Umm, how do you expect mongo to read your private class variables.



来源:https://stackoverflow.com/questions/9809103/unable-to-save-php-objects-in-mongodb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!