原则在Google App Engine上找不到数据? [英] Doctrine not finding data on Google App Engine?

查看:171
本文介绍了原则在Google App Engine上找不到数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



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

 <?php 

$ baseDir = dirname(dirname(__ FILE__));

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

使用Doctrine\Common\Annotations\AnnotationReader;
使用Doctrine\Common\Annotations\AnnotationRegistry;

//全局使用的缓存驱动,在生产中使用APC或memcached
$ cache = new Doctrine\Common\Cache\ArrayCache;
//标准注释阅读器
$ 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,//使用reader
$ cache,//和缓存驱动
$ debug = LOCAL
);
//创建用于元数据读取的驱动程序链
$ driverChain = new Doctrine\ORM\Mapping\Driver\DriverChain();
//仅加载超类元数据映射到驱动链
//还注册Gedmo注释。注意:您可以个性化它
Gedmo\DoctrineExtensions :: registerAbstractMappingIntoDriverChainORM(
$ driverChain ,//我们的元数据驱动程序链,钩入
$ cachedAnnotationReader //我们的缓存注释阅读器
);

//现在我们要注册我们的应用程序实体
//因为我们需要另一个用于Entity命名空间的元数据驱动
$ annotationDriver = new Doctrine\ORM\ Mapping\Driver\AnnotationDriver(
$ cachedAnnotationReader,//我们的缓存注释读取器
数组(ENTITY_PATH)//要查找的路径
);
//注意:应用程序的驱动程序实体可以不同,Yaml,Xml或任何
//注册注册驱动程序实体名称空间
$ driverChain-> addDriver($ annotationDriver,' MyApp\\Model\\Entity');

//一般ORM配置
$ config = new Doctrine\ORM\Configuration;
$ config-> setProxyDir(sys_get_temp_dir());
$ config-> setProxyNamespace('Proxy');
$ config-> setAutoGenerateProxyClasses(Doctrine\Common\Proxy\AbstractProxyFactory :: AUTOGENERATE_FILE_NOT_EXISTS); //这可以基于生产配置。
//注册元数据驱动
$ config-> setMetadataDriverImpl($ driverChain);
//使用我们已经初始化的缓存驱动程序
$ config-> setMetadataCacheImpl($ cache);
$ config-> setQueryCacheImpl($ cache);

//创建事件管理器和挂接首选扩展侦听器
$ evm = new Doctrine\Common\EventManager();
// gedmo扩展侦听器,删除哪些不使用

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

// mysql设置名称UTF-8(如果需要)
$ 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
);

//最后,创建实体管理器
$ em = Doctrine\ORM\EntityManager :: create($ dbParams,$ config,$ evm);



更新



为了清楚起见: / p>

这将返回一个空数组:

  $ 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());


解决方案

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



更新



看到这个问题:为什么Doctrine2做一个INNER JOIN for findAll()?


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

解决方案

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

这篇关于原则在Google App Engine上找不到数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆