Doctrine not finding data on Google App Engine?

2020-08-17 17:54发布

问题:

When I do a simple query, like finding all users, it returns an empty array. $users = $em->getRepository('MyApp\\Model\\Entity\\User')->findAll();

However, when I connect to my database manually, using PDO, it finds the data. I am using the ArrayCache method, to make sure it has nothing to do with GAE not having a filesystem. The GAE docs say you can use sys_get_temp_dir(), so I don't think it's my proxies. I'm at a loss for why Doctrine is returning nothing and not throwing any errors as well.

Here is my bootstrap file for my app:

<?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);

Update

Just for clarity:

This returns an empty array:

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

And this returns an array with users in it. So confused.

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

回答1:

My issue was that I didn't create a Company in my database and my User entity requires a Company, so Doctrine used an INNER JOIN and thus, no users. Ugh.

Update

See this question: Why does Doctrine2 do an INNER JOIN for findAll()?