coen-hyde/Shanty-Mongo — Issue #50: Instantiating correct concrete classes in constructor
Author: tholder
Created: 2011-10-31 16:53:31+00:00
State: open

There seems to be an issue when an object is instantiated with an array of data.

The sub data/document elements are not being initialised as the correct objects, meaning you obviously lack functionality in those rich embeded objects you might need in preInsert etc.

My solution is currently to do something like the following in the contructor, but, in my class level it feels a bit dirty! Should shanty be doing something similar lower down?

``` array('Document:Model_Mongo_Poco', 'Required')
);

/**
* Bit of a nasty hack, but fixes issue with Shanty where setting array in
* constructor doesn't correctly setup child object. This causes preInsert to fail.
*
* @return void
* @author Tom Holder
**/
public function __construct($data = array(), $config = array())
{
parent::__construct($data, $config);

//We need to make sure the poco element of the incoming data is a concrete ponco class.
if(array_key_exists('poco', $data) && is_array($data['poco'])) {
$this->poco = new Model_Mongo_Poco($data['poco']);
}
}