createDBRef support?

22 views
Skip to first unread message

cdmn

unread,
Dec 21, 2009, 9:03:17 AM12/21/09
to Morph for MongoDB
Does Morph support Coll::createDBRef?

Basicly I need reference support :-)

Jon Moss

unread,
Dec 30, 2009, 2:26:40 PM12/30/09
to Morph for MongoDB
Hi cdmn,

Yes Morph does support referenced. In fact it handles them
automatically. When building a morph object, you can add a HasOne or
HasMany property. These property types allow you so access one (or
more) related morph objects directly whilst storing the parent and
child object separately and using a DBRef internally.

e.g.

class Parent extends Morph_Object
{

public function __construct()
{
$this->addProperty(new Morph_Property_HasOne('child',
'Child'));
$this->addProperty(new Morph_Property_String('Name'));
}
}

class Child extends Morph_Object
{

public function __construct()
{
$this->addProperty(new Morph_Property_String('Name'));
}
}


With the above example you can use them like this:

$p = new Parent();
$p->name ='Mum';
$p->child = new Child();
$p->child->name = 'baby';


internally this is stored in 2 collections within MongoDB and a DBRef
is placed inside the parent object which point to the child one.

Morph also takes care of fetching it again for you. So if you have the
parent object (as $p for example) you can do the following:

echo 'The childs name is: ' . $p->child->name;

Just to cover the whole picture, the ComposeOne and ComposeMany works
in exactly the same way except that the child object is stored inside
the parent object itself, not in a separate collection.

I hope all that makes sense, and sorry it took a while to get back to
you (my laptop died over christmas :( ).

Regards,
Jon

Message has been deleted

cdmn

unread,
Jan 4, 2010, 8:25:48 AM1/4/10
to Morph for MongoDB
It explained a lot.

Still, I have HasMany relations and I'm getting this error:

Fatal error: Class Morph_Property_HasMany contains 4 abstract methods
and must therefore be declared abstract or implement the remaining
methods (ArrayAccess::offsetExists, ArrayAccess::offsetGet,
ArrayAccess::offsetSet, ...) in
phar://C:/xampp/htdocs/app/plugins/morph.phar/Property/HasMany.php
on line 124

The object is pretty much simple:

class articles2 extends Morph_Object
{
public function __construct($id = null)
{
parent::__construct($id);

$this->addProperty(new Morph_Property_String('url'))
->addProperty(new Morph_Property_String('title'))
->addProperty(new Morph_Property_Date('pubdate'))
->addProperty(new Morph_Property_String('source_name'))
->addProperty(new Morph_Property_Integer('source_id'))
->addProperty(new Morph_Property_Integer('source_rank'))
->addProperty(new Morph_Property_HasMany
('channel', 'channel'))
->addProperty(new Morph_Property_HasMany
('category', 'category'))
->addProperty(new Morph_Property_HasMany
('entity_type',
'entity_type'))
->addProperty(new Morph_Property_HasMany
('entity_values',
'entity_values'));
}

}

Am I doing something wrong or HasMany isnt implemented yet?

Mike

cdmn

unread,
Jan 4, 2010, 8:56:44 AM1/4/10
to Morph for MongoDB
I tried your examples with HasOne.


This example works, creates collection in test database.

$mongo = new Mongo(); //or MongoAuth etc..
$storage = new Morph_Storage($mongo->selectDB('test'));

$aBook = new articles2();
$aBook->title = 'MongoDB Rocks!';
$aBook->pubdate = strtotime('2009-09-12');

$storage->save($aBook)

This example:

$mongo = new Mongo(); //or MongoAuth etc..
$storage = new Morph_Storage($mongo->selectDB('test'));

$p = new Parents();


$p->name ='Mum';
$p->child = new Child();
$p->child->name = 'baby';

$storage->save($p);

Spits out fatal error: Fatal error: Call to a member function
setStorage() on a non-object in phar://C:/xampp/htdocs/app/plugins/morph.phar/Object.php
on line 119
The parents and child objects are from your example.

Am I doing something wrong, or yet again, it isnt implemented?

Mike

cdmn

unread,
Jan 4, 2010, 9:09:27 AM1/4/10
to Morph for MongoDB
The example you have, I just changed HasOne to ComposeMany.
I'm getting:

Message: Argument 1 passed to Morph_Property_ComposeMany::setValue()
must be an instance of Morph_Collection, instance of Child given ...

class Parents extends Morph_Object
{
public function __construct()
{
$this->addProperty(new Morph_Property_ComposeMany('child',
'Child'));
$this->addProperty(new Morph_Property_String('name'));
}
}

Well? :-)

Jon Moss

unread,
Jan 4, 2010, 11:43:58 AM1/4/10
to Morph for MongoDB
Ok,

Looks like a bug or two has crept into the latest release. I will
spend some time fixing them up and get back to you.

Thanks,
Jon

Jon Moss

unread,
Jan 4, 2010, 1:18:31 PM1/4/10
to Morph for MongoDB
Ok looks like there are 3 issues:

Firstly, a coding issue on my side leading to the abstract method
warning - corrected in SVN and will be in the next release.

The second is the setStorage message you are getting. This is the
result of not calling the parent constructor in the Morph_Object
extended objects.

class Parents extends Morph_Object
{
public function __construct()
{

parent::__construct(null); //very important!!


$this->addProperty(new Morph_Property_ComposeMany('child',
'Child'));
$this->addProperty(new Morph_Property_String('name'));
}

}

The final issue is that when you use ComposeMany or HasMany the
property now acts like an array so you need to do:

$child = new Child();
$p->child[] = $child;

The error message you are seeing is that you are trying to assign an
object directly rather than appending it to the array.

I hope that all makes sense?

To save you waiting for the next release with the fix in it I am about
to upload the phar file directly to this group as version 0.3.1

Regards,
Jon

Reply all
Reply to author
Forward
0 new messages