cakephp / elastic-search

Elastic search datasource for CakePHP
Other
88 stars 53 forks source link

Documents are not getting marshalled correctly with ES2.3.4 #102

Closed lilHermit closed 8 years ago

lilHermit commented 8 years ago

I'm using ES2.3.4 and branch 1.0 and during the marshalling process the values passed to the __construct seem incorrect.

_GET /library/apps/search

{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "library",
        "_type": "apps",
        "_id": "AVYJCVkoBgZSBgn8x1jS",
        "_score": 1,
        "_source": {
          "title": "this title",
          "questions": [
            {
              "title": "question1"
            },
            {
              "title": "question2"
            }
          ]
        }
      }
    ]
  }
}

Now when I do the following in my controller

        $apps = TypeRegistry::get('Apps')
          ->get('AVYJCVkoBgZSBgn8x1jS');
        debug($apps);

I get the following

/src/Controller/Admin/InstanceController.php (line 64)
object(App\Model\Document\App) {
    [protected] _data => [
        'markNew' => false,
        'markClean' => true,
        'useSetters' => false,
        'source' => 'apps'
    ]
    [protected] _docAsUpsert => false
    [protected] _autoPopulate => false
    [protected] _upsert => null
    [protected] _params => [
        '_id' => [
            'title' => 'this title',
            'questions' => [
                (int) 0 => object(Cake\ElasticSearch\Document) {

                    'title' => 'question1',
                    '[new]' => false,
                    '[accessible]' => [
                        '*' => true
                    ],
                    '[dirty]' => [],
                    '[original]' => [],
                    '[virtual]' => [],
                    '[errors]' => [],
                    '[invalid]' => [],
                    '[repository]' => 'apps'

                },
                (int) 1 => object(Cake\ElasticSearch\Document) {

                    'title' => 'question2',
                    '[new]' => false,
                    '[accessible]' => [
                        '*' => true
                    ],
                    '[dirty]' => [],
                    '[original]' => [],
                    '[virtual]' => [],
                    '[errors]' => [],
                    '[invalid]' => [],
                    '[repository]' => 'apps'

                }
            ],
            'id' => 'AVYJCVkoBgZSBgn8x1jS'
        ],
        '_type' => '',
        '_index' => ''
    ]
    [protected] _rawParams => []
}

Doing $apps->title and $apps->questions result in

Field title does not exist
Elastica\Exception\InvalidException

On inspection of the Document __construct $id and $data seems to being passed incorrectly as

class App extends Document {

    public function __construct($id = '', $data = array(), $type = '', $index = '') {
        parent::__construct($id, $data, $type, $index);
        debug($id);
    }
}

outputs the following (what seems like data rather than id)

[
    'title' => 'this title',
    'questions' => [
        (int) 0 => object(Cake\ElasticSearch\Document) {

            'title' => 'question1',
            '[new]' => false,
            '[accessible]' => [
                '*' => true
            ],
            '[dirty]' => [],
            '[original]' => [],
            '[virtual]' => [],
            '[errors]' => [],
            '[invalid]' => [],
            '[repository]' => 'apps'

        },
        (int) 1 => object(Cake\ElasticSearch\Document) {

            'title' => 'question2',
            '[new]' => false,
            '[accessible]' => [
                '*' => true
            ],
            '[dirty]' => [],
            '[original]' => [],
            '[virtual]' => [],
            '[errors]' => [],
            '[invalid]' => [],
            '[repository]' => 'apps'

        }
    ],
    'id' => 'AVYJCVkoBgZSBgn8x1jS'
]

Sorry for the long post ;-)

lorenzo commented 8 years ago

I don't understand where the constructor public function __construct($id = '', $data = array(), $type = '', $index = '') comes from

lilHermit commented 8 years ago

Doh! Just spotted my issue!

I was extending the wrong Document (Elastica\Document instead of Cake\ElasticSearch\Document). Which is where public function __construct($id = '', $data = array(), $type = '', $index = '') came from. Thanks for spotting that! ;-)

lorenzo commented 8 years ago

:)