Doctrine find()方法返回stdClass而不是实体实例

时间:2014-10-22 15:09:58

标签: symfony doctrine-orm

我做了一些搜索,并没有得到一个可以帮助我的答案。

我使用doctrine和symfony 2并根据Doctrine的文档(http://doctrine-orm.readthedocs.org/en/latest/reference/working-with-objects.html#by-primary-key),find()方法应该返回null或实体的实例。

当传递有效的客户端ID并且无论如何抛出异常时,以下代码返回stdClass。实体管理器正在注入服务的构造函数。

对对象使用注释检查可以正常工作,并且不会抛出异常。

$client = $this->em->find('Entity\Clients\Client', $clientId);

\Doctrine\Common\Util\Debug::dump($client);

//if (!is_object($client)) {
if (!$client instanceof Entity\Clients\Client) {
    throw new ClientDoesNotExistException($clientId);
}

是否有人对此有解释?我错过了一些非常基本的东西吗

感谢。

谢谢塞拉德。我在config.yml中的教义位是以下

# Doctrine Configuration
doctrine:
    dbal:
        driver:   "%database_driver%"
        host:     "%database_host%"
        port:     "%database_port%"
        dbname:   "%database_name%"
        user:     "%database_user%"
        password: "%database_password%"
        charset:  UTF8
        # if using pdo_sqlite as your database driver, add the path in parameters.yml
        # e.g. database_path: "%kernel.root_dir%/data/data.db3"
        # path:     "%database_path%"

orm:
    auto_generate_proxy_classes: "%kernel.debug%"
    auto_mapping: true
    filters:
        portal_filter:  path\to\BaseBundle\Filter\PortalFilter
        enabled:        true;'

Composer.json

{
"name": "symfony/framework-standard-edition",
"license": "MIT",
"type": "project",
"description": "The \"Symfony Standard Edition\" distribution",
"autoload": {
    "psr-0": { "": "src/", "SymfonyStandard": "app/" }
},
"require": {
    "php": ">=5.3.3",
    "symfony/symfony": "2.5.*",
    "doctrine/orm": "~2.2,>=2.2.3",
    "doctrine/doctrine-bundle": "~1.2",
    "twig/extensions": "~1.0",
    "symfony/assetic-bundle": "~2.3",
    "symfony/swiftmailer-bundle": "~2.3",
    "symfony/monolog-bundle": "~2.4",
    "sensio/distribution-bundle": "~3.0",
    "sensio/framework-extra-bundle": "~3.0",
    "incenteev/composer-parameter-handler": "~2.0",
    "sensio/generator-bundle": "~2.3",
    "friendsofsymfony/rest-bundle": "1.3.*",
    "jms/serializer-bundle": "0.13.0",
    "friendsofsymfony/user-bundle": "~2.0@dev",
    "friendsofsymfony/jsrouting-bundle": "1.5.3",
    "doctrine/migrations": "dev-master",
    "doctrine/doctrine-migrations-bundle": "dev-master",
    "dunglas/angular-csrf-bundle": "1.0.*@dev",
    "oldsound/rabbitmq-bundle": "1.3.*",
    "oyejorge/less.php": "~1.5",
    "nelmio/api-doc-bundle": "dev-master",
    "peekmo/jsonpath": "dev-master",
    "uecode/api-key-bundle": "dev-master",
    "aws/aws-sdk-php-zf2": "1.2.*"},
"require-dev": {
    "doctrine/doctrine-fixtures-bundle": "dev-master",
    "phpunit/phpunit": "3.7.*",
    "liip/functional-test-bundle":"dev-master"
},
"scripts": {
    "post-root-package-install": [
        "SymfonyStandard\\Composer::hookRootPackageInstall"
    ],
    "post-install-cmd": [
        "Incenteev\\ParameterHandler\\ScriptHandler::buildParameters",
        "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap",
        "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache",
        "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets",
        "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile",
        "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::removeSymfonyStandardFiles"
    ],
    "post-update-cmd": [
        "Incenteev\\ParameterHandler\\ScriptHandler::buildParameters",
        "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap",
        "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache",
        "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets",
        "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile",
        "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::removeSymfonyStandardFiles"
    ]
},
"config": {
    "bin-dir": "bin"
},
"extra": {
    "symfony-app-dir": "app",
    "symfony-web-dir": "web",
    "incenteev-parameters": {
        "file": "app/config/parameters.yml"
    },
    "branch-alias": {
        "dev-master": "2.5-dev"
    }
}
}

同样的问题,这次来自控制器。仅供参考,以前我是在向这项服务注入学说的实体经理:

DocumentManager:
    class: DocumentHandlerBundle\Services\DocumentManager
    arguments: [@doctrine.orm.entity_manager]

在控制器中:

public function newDocumentAction() {          
    $em = $this->getDoctrine()->getManager();
    $entity = $em->find('Entity\Documents\Document', 1400);

    echo get_class($entity); // output: Entity\Documents\Document

    if ($entity instanceof Entity\Documents\Document) {
        echo "<p>YEYYY, we have an entity!!!</p>";
    } else {
        echo "<p>&/%#$&%#, not an entity!!!</p>";
    }
    return new Response('');
}

0 个答案:

没有答案