Uploaded image for project: 'PHP Driver: Extension'
  1. PHP Driver: Extension
  2. PHPC-29

Ability to use a custom MongoCursor class

    • Type: Icon: New Feature New Feature
    • Resolution: Duplicate
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: None
    • Component/s: None
    • None
    • Environment:
      n/a

      Copied from my post on the dev mailing list:

      Would it be possible to add an option to a MongoCollection to allow
      for the ability to pass in the class name of a class that extends
      MongoCursor, so that it gets returned instead of the base MongoCursor?
      I'd like to extend MongoCursor to override a couple of methods and add
      some minor functionality. As it stands now, I have to create a wrapper
      class and write one line methods to expose the individual MongoCursor
      methods, like:

      class ResultSet
      {
      /**

      • @var MongoCursor
        */
        protected $rs;
        public function __construct( MongoCursor $rs ) { $this->rs = $rs; }

        public function current()

        { return $rs->current(); }

        public function valid()

        { return $this->rs->valid(); }

        public function key()

        { return $this->rs->key(); }

        }

      Although this works, it would be much better for extensibility (not to
      mention much cleaner) to be able to extend MongoCursor directly and
      have MongoCollection return the new class instead. The other option
      that I was just looking at is having a method in the MongoCursor class
      that would take a MongoCursor and basically clone it into the current
      object, but I'm not familiar with the underpinnings and how the C++
      code works with the driver and at first glance that seems more
      complicated.

      An example of how this would look on the PHP side:

      class ResultSet extends MongoCursor
      {
      public function current()

      { $doc = parent::current(); // do some other stuff return $doc; }

      }

      An option for specifying it could be:

      $db = new Mongo( $server, array( 'custom_cursor' => 'ResultSet' ) );

            Assignee:
            bjori Hannes Magnusson
            Reporter:
            bstovall Blake Stovall
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: