Skip to content

Latest commit

 

History

History
56 lines (43 loc) · 1.56 KB

01_archivable.md

File metadata and controls

56 lines (43 loc) · 1.56 KB

Archivable

The Archivable behavior makes a copy of a document in an other collection, on-demand or when the document is created/updated/deleted

Configuration

array(
    'Model\Article' => array(
        'fields' => array(
            'title' => 'string',
        ),
        'behaviors' => array(
            array(
                'class' => 'Mongator\Behavior\Archivable',
            ),
        ),
    ),
);

Options

  • archive_class (default '%class%\Archive'): the new model used to make the copies
  • collection_class (default '%collection%Archive'): the collection where the copies will be stored
  • id_field (default 'document'): the field of the copy where the original id will be saved
  • archived_at_field (default 'archived_at_field'): the field of the original document where the archived id will be saved
  • archive_on_insert (default false): create a copy of the document when it is created
  • archive_on_update (default false): create a copy of the document when it is updated
  • archive_on_delete (default true): create a copy of the document when it is deleted
  • fields (default array()): the list of fields used to create the copy. If empty, all fields will be used

Usage

$article = $this->mongator->create('Model\Article')
    ->setTitle('foo')
    ->save();

$id = $article->getId();
$article->delete();

$copy = $this->mongator
    ->getRepository('Model\Article\Archive')
    ->createQuery(array('document' => $id))
    ->one();

echo $copy->getTitle(); // foo