【问题标题】:Doctrine not finding data on Google App Engine?Doctrine 没有在 Google App Engine 上找到数据?
【发布时间】:2015-06-19 15:24:04
【问题描述】:

当我做一个简单的查询时,比如查找所有用户,它返回一个空数组。 $users = $em->getRepository('MyApp\\Model\\Entity\\User')->findAll();

但是,当我使用 PDO 手动连接到我的数据库时,它会找到数据。我正在使用 ArrayCache 方法,以确保它与没有文件系统的 GAE 无关。 GAE 文档说你可以使用sys_get_temp_dir(),所以我不认为这是我的代理。我不知道为什么 Doctrine 什么都不返回,也没有抛出任何错误。

这是我的应用程序的引导文件:

<?php

$baseDir = dirname(dirname(__FILE__));

define('TIMEZONE_OFFSET', \MyApp\Library\Date::getMyTimezoneOffset());

use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\AnnotationRegistry;

// globally used cache driver, in production use APC or memcached
$cache = new Doctrine\Common\Cache\ArrayCache;
// standard annotation reader
$annotationReader = new AnnotationReader;
AnnotationReader::addGlobalIgnoredName('dummy');
AnnotationRegistry::registerFile(__DIR__ . "/doctrine/orm/lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php");
AnnotationRegistry::registerFile(__DIR__ . "/Gedmo/Timestampable/Mapping/Driver/Annotation.php");
AnnotationRegistry::registerAutoloadNamespace("\\MyApp\\Model\\Entity", $baseDir);
$cachedAnnotationReader = new Doctrine\Common\Annotations\CachedReader(
    $annotationReader, // use reader
    $cache, // and a cache driver
    $debug = LOCAL
);
// create a driver chain for metadata reading
$driverChain = new Doctrine\ORM\Mapping\Driver\DriverChain();
// load superclass metadata mapping only, into driver chain
// also registers Gedmo annotations.NOTE: you can personalize it
Gedmo\DoctrineExtensions::registerAbstractMappingIntoDriverChainORM(
    $driverChain, // our metadata driver chain, to hook into
    $cachedAnnotationReader // our cached annotation reader
);

// now we want to register our application entities,
// for that we need another metadata driver used for Entity namespace
$annotationDriver = new Doctrine\ORM\Mapping\Driver\AnnotationDriver(
    $cachedAnnotationReader, // our cached annotation reader
    array(ENTITY_PATH) // paths to look in
);
// NOTE: driver for application Entity can be different, Yaml, Xml or whatever
// register annotation driver for our application Entity namespace
$driverChain->addDriver($annotationDriver, 'MyApp\\Model\\Entity');

// general ORM configuration
$config = new Doctrine\ORM\Configuration;
$config->setProxyDir(sys_get_temp_dir());
$config->setProxyNamespace('Proxy');
$config->setAutoGenerateProxyClasses(Doctrine\Common\Proxy\AbstractProxyFactory::AUTOGENERATE_FILE_NOT_EXISTS); // this can be based on production config.
// register metadata driver
$config->setMetadataDriverImpl($driverChain);
// use our already initialized cache driver
$config->setMetadataCacheImpl($cache);
$config->setQueryCacheImpl($cache);

// create event manager and hook preferred extension listeners
$evm = new Doctrine\Common\EventManager();
// gedmo extension listeners, remove which are not used

// timestampable
$timestampableListener = new Gedmo\Timestampable\TimestampableListener;
$timestampableListener->setAnnotationReader($cachedAnnotationReader);
$evm->addEventSubscriber($timestampableListener);

// mysql set names UTF-8 if required
$evm->addEventSubscriber(new Doctrine\DBAL\Event\Listeners\MysqlSessionInit());

$dbParams = array(
    'driver' => 'pdo_mysql',
    'user' => DB_USER,
    'password' => DB_PASSWORD,
    'dbname' => DB_NAME,
    'host' => DB_HOST,
    'port' => DB_PORT,
    'unix_socket' => DB_UNIX_SOCKET
);

// Finally, create entity manager
$em = Doctrine\ORM\EntityManager::create($dbParams, $config, $evm);

更新

为了清楚起见:

这会返回一个空数组:

$users = $em->getRepository('MyApp\\Model\\Entity\\User')->findAll();
\Doctrine\Common\Util\Debug::dump($users);

这会返回一个包含用户的数组。好困惑。

$pdo = $em->getConnection();
$users = $pdo->query('SELECT * FROM user');
var_dump($users->fetchAll());

【问题讨论】:

  • "not throwing any errors" - 在打开 PHP 标记之后添加错误报告到文件顶部,例如 &lt;?php error_reporting(E_ALL); ini_set('display_errors', 1);,然后是其余部分代码,看看它是否产生任何东西。如果您还没有这样做,请检查您的 PDO 上的错误php.net/manual/en/pdo.error-handling.php
  • @Fred-ii- 我按照你说的做了,仍然没有错误。 =/
  • 您可能应该发布findAll() 函数的作用。这不是 PDO 核心功能。查阅手册php.net/manual/en/pdostatement.fetch.php
  • @Fred-ii- findAll() 是 Doctrine 的基本功能。与SQL中的SELECT * FROM user基本相同。

标签: php google-app-engine pdo doctrine-orm google-cloud-sql


【解决方案1】:

我的问题是我没有在我的数据库中创建公司,而我的用户实体需要一个公司,所以 Doctrine 使用了 INNER JOIN,因此没有用户。呃。

更新

看到这个问题:Why does Doctrine2 do an INNER JOIN for findAll()?

【讨论】:

    猜你喜欢
    • 2012-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 2019-07-10
    • 2020-02-26
    • 2012-07-31
    相关资源
    最近更新 更多