{"issue":{"state":"closed","created_at":"2011-10-31T16:53:31Z","updated_at":"2011-12-05T08:50:10Z","comments":4,"user":{"gravatar_id":"0b668da31995d4dfb4b2e3a66b68fb61","id":41235,"url":"https://api.github.com/users/tholder","avatar_url":"https://secure.gravatar.com/avatar/0b668da31995d4dfb4b2e3a66b68fb61?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png","login":"tholder"},"body":"There seems to be an issue when an object is instantiated with an array of data.\r\n\r\nThe 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.\r\n\r\nMy 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?\r\n\r\n```php\r\n<?php\r\nclass Model_Mongo_Contact extends Shanty_Mongo_Document {\r\n\r\n    protected static $_collection = 'contact';\r\n    \r\n    protected static $_requirements = array(\r\n        'poco' => array('Document:Model_Mongo_Poco', 'Required')\r\n    );\r\n    \r\n    /**\r\n     * Bit of a nasty hack, but fixes issue with Shanty where setting array in \r\n     * constructor doesn't correctly setup child object. This causes preInsert to fail.\r\n     *\r\n     * @return void\r\n     * @author Tom Holder\r\n     **/\r\n    public function __construct($data = array(), $config = array())\r\n\t{\r\n\t    parent::__construct($data, $config);\r\n\t    \r\n\t    //We need to make sure the poco element of the incoming data is a concrete ponco class.\r\n\t    if(array_key_exists('poco', $data) && is_array($data['poco'])) {\r\n\t        $this->poco = new Model_Mongo_Poco($data['poco']);\r\n\t    }\r\n\t}","assignee":null,"id":2099167,"labels":[],"pull_request":{"patch_url":null,"html_url":null,"diff_url":null},"number":50,"title":"Instantiating correct concrete classes in constructor","html_url":"https://github.com/coen-hyde/Shanty-Mongo/issues/50","closed_at":"2011-12-05T08:50:10Z","url":"https://api.github.com/repos/coen-hyde/Shanty-Mongo/issues/50","milestone":null},"action":"created","comment":{"created_at":"2011-12-05T08:50:10Z","updated_at":"2011-12-05T08:50:10Z","user":{"gravatar_id":"3efe13b633ae575523a5b27f465ed982","id":123503,"url":"https://api.github.com/users/coen-hyde","avatar_url":"https://secure.gravatar.com/avatar/3efe13b633ae575523a5b27f465ed982?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png","login":"coen-hyde"},"body":"Hi Tom,\r\n\r\nGive that a try. Basically it just initialised any sub documents created with the data passed into the constructor. Possibly a bit inefficient but should resolve your issue.\r\n\r\nCheers,\r\nCoen","id":3013228,"url":"https://api.github.com/repos/coen-hyde/Shanty-Mongo/issues/comments/3013228"}}