protected function fillEmbed($key, $value)
    {
        $attrs = $this->getAttrs();
        $cache = &$this->_cache;
        $embed = $attrs[$key];
        $model = $embed['model'];
        $type = $embed['type'];

        if ($type == self::DATA_TYPE_EMBED) {
            $model = $embed['model'];
            if ($value instanceof $model) {
                $value->embed = true;
                $return  = $value->toArray(array('_type','_id'));
            } elseif ($value === null) {
                $return = null;
            } elseif (isset($value, $value['$ref'], $value['$id'])) {
                $value = $model::id($value['$id']);
                $ref = $value->makeRef();
                $return = $ref;
            } else {
                throw new \Exception("{$key} is not instance of '$model'");
            }

        } elseif ($type == self::DATA_TYPE_EMBEDS) {
            $arr = array();
            if (is_array($value)) {
                foreach ($value as $item) {
                    if(! ( $item instanceof Model ) ) continue;
                    $item->embed = true;
                    $arr[] = $item;
                }
                $value = Collection::make($arr);
            }

            if ($value instanceof Collection) {
                $return = $value->toEmbedsArray();
            } elseif ($value === null) {
                $return = null;
            } else {
                throw new \Exception("{$key} is not instance of '$model'");
            }
        }

        $cache[$key] = $value;

        return $return;
    }