Skip to content

Instantly share code, notes, and snippets.

@tholder
Created October 2, 2011 14:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tholder/1257513 to your computer and use it in GitHub Desktop.
Save tholder/1257513 to your computer and use it in GitHub Desktop.
Problem updating document set of existing doc
<?
/**
* Represents the invite system. The invite collection contains one document for each invited email address.
**/
class Model_Mongo_Invite extends Shanty_Mongo_Document {
protected static $_db = 'contactzilla';
protected static $_collection = 'invite';
protected static $_requirements = array(
'secret' => 'Required',
'email' => 'Required',
'companies' => array('DocumentSet'),
'companies.$' => array('Document:Model_Mongo_CompanyInvite')
);
/**
* We've overridden save to catch urlKey clash.
**/
public function save() {
try {
parent::save();
} catch(Exception $e) {
$m = $e->getMessage();
//We might have had a email clash, in which case we just lookup the existing doc and save.
if (strpos($m,'duplicate') && strpos($m,'email')) {
//Get companies that have been added.
$companies = $this->companies;
//Save it to existing document.
$i = Model_Mongo_Invite::one(array('email' => $this->email));
foreach($this->companies as $ca) {
$uca = $i->companies->new();
$uca->company = $ca->company;
$uca->addedByUser = $ca->user;
}
$i->save();
} else {
throw $e;
}
}
}
}
class Model_Mongo_CompanyInvite extends Shanty_Mongo_Document {
protected static $_requirements = array(
'company' => array('Document:Model_Mongo_Company', 'AsReference'),
'addedByUser' => array('Document:Model_Mongo_User', 'AsReference')
);
protected function preSave() {
if($this->isNewDocument()) {
$this->createdAt = time();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment