dqtdz08206 2013-12-06 16:38
浏览 77
已采纳

为什么扩展MongoDate(php)的类不存储为ISODate?

I have a class that extends MongoDate with just a constructor and an overloaded __toString().

class MyClass extends MongoDate
{
    public function __construct( $param )
    {
        if( is_a( $param, 'MongoDate' ) )
        {
            parent::__construct( $param->sec, $param->usec );
        }
        else if ( is_a( $param, 'DateTime' ))
        {
            parent::__construct( $param->getTimestamp() );
        }
        else if( is_string( $param ) )
        {
            $datetime = new DateTime( $param );
            parent::__construct( $datetime->getTimestamp() );
        }
        else
        {
            parent::__construct(); 
        }   
    }

    public function __toString()
    {
        return date( 'D, d M Y H:i', $this->sec );
    }
}

The instances of this class are well constructed and before storage have right values.

This class has this form when is stored in MongoDB:

{ "sec" : 1387576800, "usec" : 0 }

Instead of this one that I have when the value is stored with MongoDate:

ISODate("2013-12-30T22:00:00Z")

The code the value is stored with, used in both cases, where $date is a MyClass or a MongoDate:

$query = array( '_id' => $item_id );
$update = array( '$set' => array( $date_field => $date ) );
$collection->update( $query, $update );

Why the class that inherits from MongoDate is not strored as MongoDate is (as ISODate object) and how can I solve it?

  • 写回答

1条回答 默认 最新

  • doumu5023 2014-01-02 19:03
    关注

    Currently, the PHP driver only checks the object's class name during BSON serialization (see the clazz == mongo_ce_Date comparison logic here).

    This could possibly be changed in the future to use the PHP core's instanceof_function() (from zend_operators.h) and accept any object that extends the core MongoDate class; however, one concern would be that an inheriting class might change its use of the internal properties, which are read during serialization. This could lead to unexpected behavior when converting the extending class into BSON.

    You can track this feature in PHP-954.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿