1

当我从我的 API 获取实体时,出现错误:

PHP 致命错误:在第 442 行的 /Sites/system.local/vendor/zfcampus/zf-apigility-doctrine/src/Server/Resource/DoctrineResource.php 中的非对象上调用成员函数 createQuery()

DoctrineResource.php(第 442 行的函数 - fetchAll):

    /**
     * Fetch all or a subset of resources
     *
     * @see    Apigility/Doctrine/Server/Resource/AbstractResource.php
     * @param  array $data
     * @return ApiProblem|mixed
     */
    public function fetchAll($data = array())
    {
        // Build query
        $queryProvider = $this->getQueryProvider('fetch_all');
        $queryBuilder = $queryProvider->createQuery($this->getEvent(), $this->getEntityClass(), $data);

        if ($queryBuilder instanceof ApiProblem) {
            // @codeCoverageIgnoreStart
            return $queryBuilder;
        }
            // @codeCoverageIgnoreEnd

        // Run fetch all pre with query builder
        $event = new DoctrineResourceEvent(DoctrineResourceEvent::EVENT_FETCH_ALL_PRE, $this);
        $event->setQueryBuilder($queryBuilder);
        $event->setResourceEvent($this->getEvent());
        $event->setEntity($this->getEntityClass());
        $eventManager = $this->getEventManager();
        $response = $eventManager->trigger($event);
        if ($response->last() instanceof ApiProblem) {
            return $response->last();
        }

        $adapter = $queryProvider->getPaginatedQuery($queryBuilder);
        $reflection = new \ReflectionClass($this->getCollectionClass());
        $collection = $reflection->newInstance($adapter);

        $results = $this->triggerDoctrineEvent(DoctrineResourceEvent::EVENT_FETCH_ALL_POST, null, $collection);
        if ($results->last() instanceof ApiProblem) {
            return $results->last();
        }

        // Add event to set extra HAL data
        $entityClass = $this->getEntityClass();
        StaticEventManager::getInstance()->attach(
            'ZF\Rest\RestController',
            'getList.post',
            function ($e) use ($queryProvider, $entityClass, $data) {
                $halCollection = $e->getParam('collection');
                $collection = $halCollection->getCollection();

                $collection->setItemCountPerPage($halCollection->getPageSize());
                $collection->setCurrentPageNumber($halCollection->getPage());

                $halCollection->setAttributes(
                    array(
                    'count' => $collection->getCurrentItemCount(),
                    'total' => $collection->getTotalItemCount(),
                    'collectionTotal' => $queryProvider->getCollectionTotal($entityClass),
                    )
                );

                $halCollection->setCollectionRouteOptions(
                    array(
                    'query' => ArrayUtils::iteratorToArray($data)
                    )
                );
            }
        );

        return $collection;
    }

我的作曲家.json:

{
    "name": "zfcampus/zf-apigility-skeleton",
    "description": "Skeleton Application for Apigility",
    "type": "library",
    "license": "BSD-3-Clause",
    "keywords": [
        "api",
        "apigility",
        "framework",
        "zf2"
    ],
    "homepage": "http://apigility.org/",
    "support": {
        "email": "apigility-users@zend.com",
        "irc": "irc://irc.freenode.net/apigility",
        "source": "https://github.com/zfcampus/zf-apigility-skeleton",
        "issues": "https://github.com/zfcampus/zf-apigility-skeleton/issues"
    },
    "config": {
      "process-timeout": 5000
    },
    "extra": {
      "branch-alias": {
        "dev-master": "1.0-dev",
        "dev-develop": "1.1-dev"
      }
    },
    "repositories": [
      {
        "type": "git",
        "url": "https://github.com/stefanotorresi/zf-hal.git"
      }
    ],
    "require": {
      "php": ">=5.3.23",
      "zendframework/zendframework": ">=2.3.2,<3.0.0",
      "zfcampus/zf-apigility": "~1.0",
      "zfcampus/zf-apigility-documentation": "~1.0",
      "zfcampus/zf-development-mode": "~2.0",
      "zfcampus/zf-hal": "dev-master#57980316543edfa329d7b1d65106fb73988c60ad",
      "zfcampus/zf-doctrine-querybuilder": "0.1.*"
    },
    "require-dev": {
      "zendframework/zftool": "dev-master",
      "zendframework/zend-developer-tools": "dev-master",
      "zfcampus/zf-apigility-admin": "~1.0",
      "zfcampus/zf-apigility-welcome": "~1.0",
      "doctrine/doctrine-mongo-odm-module": "dev-master",
      "zfcampus/zf-apigility-doctrine": "dev-master",
      "doctrine/mongodb-odm": "dev-master",
      "doctrine/doctrine-module": "dev-master",
      "zfcampus/zf-deploy": "~1.0"
    }
}

我的模块.config.php:

<?php

return array(

    'doctrine' => array(
        'driver' => array(
            'my_driver' => array(
                'class' => 'Doctrine\\ODM\\MongoDB\\Mapping\\Driver\\AnnotationDriver',
                'namespace' => 'My\\Entity',
                'paths' => array(
                    0 => __DIR__ . '/../src/My/Entity',
                ),
            ),
            'odm_default' => array(
                'drivers' => array(
                    'My\\Entity' => 'my_driver',
                ),
            ),
        ),
    ),

    'service_manager' => array(
        'factories' => array(
            'My\\V1\\Rest\\Track\\TrackResource' => 'My\\V1\\Rest\\Track\\TrackResourceFactory',
            'ZF\\Apigility\\Doctrine\\Server\\Resource\\DoctrineResource' => 'ZF\\Apigility\\Doctrine\\Server\\Resource\\DoctrineResourceFactory',
        ),
    ),


    'zf-apigility-doctrine-query-provider' => array(
        'invokables' => array(
            'default_odm' => 'My\\Query\\Provider\\DefaultOdm',
            'update' => 'My\\Query\\Provider\\Update',
            'delete' => 'My\\Query\\Provider\\Delete',
        ),
    ),

    'zf-apigility-doctrine-query-create-filter' => array(
        'invokables' => array(
            'default' => 'My\\Query\\CreateFilter\\DefaultCreateFilter',
        ),
    ),

    'router' => array(
        'routes' => array(
            'my.rest.track' => array(
                'type' => 'Segment',
                'options' => array(
                    'route' => '/my/track[/:track_id]',
                    'defaults' => array(
                        'controller' => 'My\\V1\\Rest\\Track\\Controller',
                    ),
                ),
            ),
        ),
    ),

    'zf-versioning' => array(
        'uri' => array(
            0 => 'my.rest.track',
        ),
    ),

    'zf-rest' => array(
        'My\\V1\\Rest\\Track\\Controller' => array(
            'listener' => 'My\\V1\\Rest\\Track\\TrackResource',
            'route_name' => 'my.rest.track',
            'route_identifier_name' => 'track_id',
            'collection_name' => 'track',
            'entity_http_methods' => array(
                0 => 'GET',
                1 => 'PATCH',
                2 => 'PUT',
                3 => 'DELETE',
            ),
            'collection_http_methods' => array(
                0 => 'GET',
                1 => 'POST',
            ),
            'collection_query_whitelist' => array(),
            'page_size' => 25,
            'page_size_param' => null,
            'entity_class' => 'My\\Entity\\Track',
            'collection_class' => 'My\\V1\\Rest\\Track\\TrackCollection',
            'service_name' => 'track',
        ),
    ),
    'zf-content-negotiation' => array(
        'controllers' => array(
            'My\\V1\\Rest\\Track\\Controller' => 'HalJson',
        ),
        'accept_whitelist' => array(
            'My\\V1\\Rest\\Track\\Controller' => array(
                0 => 'application/vnd.my.v1+json',
                1 => 'application/hal+json',
                2 => 'application/json',
            ),
        ),
        'content_type_whitelist' => array(
            'My\\V1\\Rest\\Track\\Controller' => array(
                0 => 'application/vnd.my.v1+json',
                1 => 'application/json',
            ),
        ),
    ),
    'zf-hal' => array(
        'metadata_map' => array(
            'My\\V1\\Rest\\Track\\Track' => array(
                'entity_identifier_name' => 'id',
                'route_name' => 'My.rest.track',
                'route_identifier_name' => 'track_id',
                'hydrator' => 'My\\V1\\Rest\\Track\\TrackHydrator',
            ),
            'My\\V1\\Rest\\Track\\TrackCollection' => array(
                'entity_identifier_name' => 'id',
                'route_name' => 'my.rest.track',
                'route_identifier_name' => 'track_id',
                'is_collection' => true,
            ),
        ),
    ),

    'zf-apigility' => array(
        'doctrine-connected' => array(
            'My\\V1\\Rest\\Track\\TrackResource' => array(
                'object_manager' => 'doctrine.documentmanager.odm_default',
                'hydrator' => 'My\\V1\\Rest\\Track\\TrackHydrator',
                'query_providers' => array(
                    'update' => 'update',
                    'patch' => 'update',
                    'delete' => 'delete',
                ),
            ),
        ),
    ),

    'doctrine-hydrator' => array(
        'My\\V1\\Rest\\Track\\TrackHydrator' => array(
            'entity_class' => 'My\\Entity\\Track',
            'object_manager' => 'doctrine.documentmanager.odm_default',
            'by_value' => true,
            'strategies' => array(
                'track' => 'ZF\\Apigility\\Doctrine\\Server\\Hydrator\\Strategy\\CollectionExtract',
            ),
            'use_generated_hydrator' => true,
        ),
    ),
);

请帮忙。谢谢!

4

0 回答 0