-
Notifications
You must be signed in to change notification settings - Fork 46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
for manual document upserting #106
Conversation
$sMongoID = md5('555' . 'www.apple.com'); $doc = new Document($collection); $doc->setId($sMongoID); $doc->set('manufacturer', 'apple.com'); $doc->set('name', 'iPad'); $doc->set('cap', '32GB'); // document created $doc->save(); unset($doc); $sMongoID = md5('555' . 'www.apple.com'); $doc = new Document($collection); // creating document wit SAME id $doc->setId($sMongoID); // set color $doc->set('color','white'); $doc->save() ; // it will update document, set color
merge upsert bulk
You can use '\Modules\Models\Collections\Users' instead of "users" in class and it will use your collection and your getDocumentClassName for hydrate returned related objects
in unit of work + autoformatter
I don't understand why you are trying to create document twice. In ORM document is an object representation of some data in database. So to update document you need to load document from database. But you are trying to upsert document instead of update. Correct use case is: <?php
// Create document
$sMongoID = md5('555' . 'www.apple.com');
$doc = $collection->createDocument()
->setId($sMongoID)
->set('manufacturer', 'apple.com')
->set('name', 'iPad')
->set('cap', '32GB')
->save();
unset($doc);
// update document
$doc = $collection
->getDocument($sMongoID);
->set('color','white')
->save();
<?php
class Document
{
public function isStored()
{
return $this->get('_id') && !$this->isModified('_id');
}
} And unstored document with defined id will be inserted, and of course fails with duplicale key. So if you really want to update document in your strange way, you need to define _id, but without mark it as modified. There is method for that: <?php
// Create document
$sMongoID = md5('555' . 'www.apple.com');
$doc = $collection->createDocument()
->defineId($sMongoID)
->set('color','white')
->save(); And persistence will work well. But it's weird. PS. Your pull request is not PSR compatible, and it broke tests for php 5.3. Also thank you for idea about events in unit of work, i'v forget about them. They will be added soon |
Because when you parse 30+ sites with python crawlers, why do you need to do additional Select from DB while you can just do upsert? I'v made fork from your repo, there is many changes above your project, you can check it. For example, documentEvents in Persist (with upsert too) |
So why you dont use directly $collection->update() with upsert? If you really need upsert in entity, use For example you can add beforeSave event which set one field relatively to value of another field. But at last i understand want you want and i will think how to do that in more clear way than setting upsert flag. i'm trying to hide all mongo options by clean interface. By the way doctrine's mongo add upsert when id set, maybe it is better solution than setId/defineId in this lib: http://doctrine-mongodb-odm.readthedocs.org/en/latest/reference/upserting-documents.html |
for not creating collection anytime on getRelated called
Because we need it in batch.. |
merge from Sokil last master to Horrower branch
Prevents mongo error: could not set and unset field at the same time if Document have Set and Unset fields at the samae time
Impelmented batch processing of operations instad of documents: <?php
$collection
->createBatchUpdate()
->update(
array('a' => 1),
array('$set' => array('b' => 'updated1')),
$multiple,
$upsert
)
->execute('majority'); |
$sMongoID = md5('555' . 'www.apple.com');
$doc = new Document($collection);
$doc->setId($sMongoID);
$doc->set('manufacturer', 'apple.com');
$doc->set('name', 'iPad');
$doc->set('cap', '32GB');
// document created
$doc->save();
unset($doc);
$sMongoID = md5('555' . 'www.apple.com');
$doc = new Document($collection);
// creating document wit SAME id
$doc->setId($sMongoID);
// set color
$doc->set('color','white');
$doc->save() ; // it will update document, set color