当我做一个简单的查询时,比如查找所有用户,它会返回一个空数组。
$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());
答案 0 :(得分:3)
我的问题是我没有在我的数据库中创建公司而我的用户实体需要公司,因此Doctrine使用了INNER JOIN,因此没有用户。啊。