Skip to content
This repository has been archived by the owner on Jun 11, 2021. It is now read-only.

Exporting

tholder edited this page Oct 31, 2011 · 4 revisions

If you want to export a document as an array so you can throw it straight out to an API or something (obviously we'd convert to JSON first!) you can override the export method to add in additional data you don't want to save direct to Mongo.

For example:

<?php
className extends Shanty_Mongo_Document
{
    public function formatted()
    {
        return $this->forename . ' ' . $this->surname;
    }

    /**
    * This is neat. For things we want to infer when we export, we can add them in here.
    **/
    public function export() {     
        $this->formatted = $this->formatted();
        $export = parent::export();
        unset($this->formatted);
        return $export;
    }
}

It's important to unset the values in the above example so that it doesn't later get accidentally saved against the document.

Clone this wiki locally