Skip to content

Instantly share code, notes, and snippets.

@coen-hyde
Created February 4, 2010 21:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coen-hyde/295112 to your computer and use it in GitHub Desktop.
Save coen-hyde/295112 to your computer and use it in GitHub Desktop.
<?php
class ResistanceFighter extends Shanty_Mongo_Document
{
protected static $_collectionName = 'resistancefighter';
protected $_requirements = array(
'name' => 'Document:ResistanceFighter_Name',
'email' => array('NotEmpty', 'EmailAddress'),
'address' => 'Document',
'address.state' => 'NotEmpty',
'address.suburb' => 'NotEmpty',
'address.postCode' => array('NotEmpty', 'Int'),
'invitedBy' => array('Document:ResistanceFighter', 'AsReference')
);
}
class ResistanceFighter_Name extends Shanty_Mongo_Document
{
protected $_requirements = array(
'first' => 'NotEmpty',
'last' => 'NotEmpty',
);
public function full()
{
return $this->first.' '.$this->last;
}
public function __toString()
{
return $this->full();
}
}
$connection = new Shanty_Mongo_Connection($config->mongo);
Shanty_Mongo::setDefaultConnection($connection);
$sarah = new ResistanceFighter();
$sarah->name->first = 'Sarah';
$sarah->name->last = 'Conner';
$sarah->email = 'sarah@theresistance.com';
$sarah->address->country = 'US';
$sarah->address->state = 'CA';
$sarah->address->suburb = 'TacoBell Estate';
$sarah->address->postCode = 54333;
$sarah->save();
print($sarah->name->full());
// prints 'Sarah Conner'
print($sarah->name);
// prints 'Sarah Conner'
$john = new ResistanceFighter();
$john->name->first = 'John';
$john->name->last = 'Conner';
$john->email = 'john@theresistance.com';
$john->invitedBy = $sarah;
// Find sarah, change her address and do a partial update
$sarah = ResistanceFighter::find($id);
$sarah->address->country = 'Australia';
$sarah->address->state= 'QLD';
$sarah->address->suburb = 'Brisbane';
$sarah->address->postCode = 4000;
$sarah->address->save();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment