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 请问为什么我配置IPsec后PC1 ping不通 PC2,抓包出来数据包也并没有被加密
  • ¥200 求博主教我搞定neo4j简易问答系统,有偿
  • ¥15 nginx的使用与作用
  • ¥100 关于#VijeoCitect#的问题,如何解决?(标签-ar|关键词-数据类型)
  • ¥15 一个矿井排水监控系统的plc梯形图,求各程序段都是什么意思
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了
  • ¥15 数学建模数学建模需要