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

How to update and delete in documentSet ? #105

Open
madani opened this issue Nov 29, 2012 · 1 comment
Open

How to update and delete in documentSet ? #105

madani opened this issue Nov 29, 2012 · 1 comment

Comments

@madani
Copy link

madani commented Nov 29, 2012

output:
"attribute" : [
{
"attributeGuid" : "fixedTag",
"viewOrder" : "0"
},
{
"attributeGuid" : "fixedTitle",
"viewOrder" : "1"
}
],
"description" : "Tag Content",
"guid" : "tag"

question: how to update/delete attributeGuid:fixedTag to attributeGuid:fixedContent ??

i did just:
$profile = Models_Mongo_Profile::one(array('guid'=>'tag'));
$attribute = $profile->attribute->new();
$attribute->attributeGuid = 'fixedContent'
$attribute->save();

but that didn't work, it created new documents

when i try :
class Models_Mongo_Profile extends Shanty_Mongo_Document
{
protected static $_db = 'monggo';
protected static $_collection = 'profile';
protected static $_requirements = array(
'guid' => 'Required',
'attribute' => 'DocumentSet',
'attribute.$.attributeGuid' => 'Required',
'attribute.$.defaultValues' => array('Required' => null),
'attribute.$.viewOrder' => array('Required' => null)
);
}

$profile = Models_Mongo_Profile::one(array('guid'=>'tag'));
$profile->attribute->find(array('viewOrder'=>0));

error:
Shanty_Mongo_DocumentSet::$_collection is null

@tholder
Copy link
Collaborator

tholder commented Dec 17, 2012

You're best of creating your own documentset class and then putting any functions in there you need. Then, the definition in your document looks something like:

'photos' => array('DocumentSet:Model_Mongo_PhotosDocumentSet'),
'photos.$' => array('Document'),

Then your custom documentset class looks something like:

class Model_Mongo_PhotosDocumentSet extends Shanty_Mongo_DocumentSet
{

private $_primary = false;

/**
 * Returns primary item from a plural collection.
 *
 * @return void
 * @author Tom Holder
 **/
public function getPrimary() {

    if ($this->_primary) { return $this->_primary; }

    for($i= 0; $i < count($this->_cleanData); $i++) {
        if(array_key_exists('primary',$this->_cleanData[$i])) {
            $this->_primary = $this->getProperty($i);
        }
    }

    if (!$this->_primary) {
        $this->_primary = new Shanty_Mongo_Document(array('value' => ''));
    }

    return $this->_primary;

}

Then you can do things like:

$document->photos->getPrimary()

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants