学说错误:“需要打开失败'/tmp/__CG__Source.php'”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19066140/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 18:45:45  来源:igfitidea点击:

Doctrine error: "Failed opening required '/tmp/__CG__Source.php' "

phpsymfonydoctrine-orm

提问by Jorr.it

I am trying to migrate my PHP application to an Ubuntu server, but without succes. Any help would be appreciated.

我正在尝试将我的 PHP 应用程序迁移到 Ubuntu 服务器,但没有成功。任何帮助,将不胜感激。

First I installed Doctrine successfully into /jorrit/myapp, following the first part of Doctrine's Getting Startedmanual (till "Generating the Database Schema"). Secondly I placed my PHP scripts (which use Doctrine) in folder /jorrit/myapp.

首先,我按照 Doctrine入门手册的第一部分(直到“生成数据库架构”)将 Doctrine 成功安装到 /jorrit/myapp 中。其次,我将我的 PHP 脚本(使用 Doctrine)放在文件夹 /jorrit/myapp 中。

When I try to run my PHP script in the CLI, I get this error messages:

当我尝试在 CLI 中运行我的 PHP 脚本时,我收到以下错误消息:

PHP Warning: require(/tmp/__CG__Source.php): failed to open stream: No such file or directory in /jorrit/myapp/vendor/doctrine/common/lib/Doctrine/Common/Proxy/AbstractProxyFactory.php on line 200

PHP Fatal error: require(): Failed opening required '/tmp/__CG__Source.php' (include_path='.:/usr/share/php:/usr/share/pear') in /jorrit/myapp/vendor/doctrine/common/lib/Doctrine/Common/Proxy/AbstractProxyFactory.php on line 200

PHP 警告:需要(/tmp/__CG__Source.php):无法打开流:第 200 行的 /jorrit/myapp/vendor/doctrine/common/lib/Doctrine/Common/Proxy/AbstractProxyFactory.php 中没有这样的文件或目录

PHP 致命错误:require():在 /jorrit/myapp/vendor/doctrine/ 中无法打开所需的 '/tmp/__CG__Source.php' (include_path='.:/usr/share/php:/usr/share/pear')第 200 行的 common/lib/Doctrine/Common/Proxy/AbstractProxyFactory.php

Bootstrap.php looks like this:

Bootstrap.php 看起来像这样:

<?php
// bootstrap.php
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;

require_once "vendor/autoload.php";

// Create a simple "default" Doctrine ORM configuration for Annotations
$isDevMode = false;
$config = Setup::createAnnotationMetadataConfiguration(array(__DIR__."/src"), $isDevMode);

// the connection configuration
$dbParams = array(
        'driver'   => 'pdo_mysql',
        'host'     => 'xx',
        'user'     => 'xx',
        'password' => 'xx',
        'dbname'   => 'xx',
        'profiler' => 'false'
);


// obtaining the entity manager
$entityManager = EntityManager::create($dbParams, $config);

?>

The first lines of my PHP script:

我的 PHP 脚本的第一行:

<?php

require_once "bootstrap.php";
require_once 'classes.php';

$connection = $entityManager->getConnection();

The application works fine in my development environment (Windows). The /tmp folder exists and is accessible. The database is migrated succesfully and exists. I did not change anything in the vendor folder.

该应用程序在我的开发环境 (Windows) 中运行良好。/tmp 文件夹存在并可访问。数据库迁移成功并存在。我没有更改供应商文件夹中的任何内容。

Any ideas? Thanks in advance for your help.

有任何想法吗?在此先感谢您的帮助。

回答by Zorji

TL;DR You'll just need to generate your proxy classes manually

TL;DR 你只需要手动生成你的代理类

vendor/bin/doctrine orm:generate-proxies

Doctrine uses Proxies to connect the to database. Proxies are generated from the the Entity classes.

Doctrine 使用代理连接到数据库。代理是从实体类生成的。

In development mode, it generates a Proxies on every request because you could make changes to Entity classes.

在开发模式下,它会在每个请求上生成一个代理,因为您可以对实体类进行更改。

In production mode, it does not generate Proxies every time. For performance reason, it assumes the Proxies exist and include them directly.

在生产模式下,它不会每次都生成代理。出于性能原因,它假定代理存在并直接包含它们。

There are a few mode for Proxies generation:

代理生成有几种模式:

  1. ALWAYS - It alwayes generates Proxies, this is the default setting for development mode
  2. NEVER - It never generates Proxies, this is the default setting for production mode
  3. ON_DEMAND - It only generates the Proxies if the Proxy files do not exist. The drawback of this option is that it has to call file_exists() every time which could potentially cause a performance issue.
  1. ALWAYS - 它总是生成代理,这是开发模式的默认设置
  2. NEVER - 它从不生成代理,这是生产模式的默认设置
  3. ON_DEMAND - 如果代理文件不存在,它只生成代理。此选项的缺点是每次都必须调用 file_exists(),这可能会导致性能问题。

Now the command

现在命令

vendor/bin/doctrine orm:generate-proxies

generates Proxy classes to /tmp. I would say this might still cause trouble because other applications on your server might delete these files unexpectedlly. One option is you can change your /tmp directory access permission to 1777

将代理类生成到 /tmp。我会说这可能仍然会引起问题,因为您服务器上的其他应用程序可能会意外删除这些文件。一种选择是您可以将 /tmp 目录访问权限更改为 1777

sudo chmod 1777 /tmp

The stricky bit '1' in front of 777 means that, although everyone can read/write to the /tmp directory, but you can only operate on your own files. i.e. You can't remove files created by other users.

777前面的sricky位'1'表示,虽然每个人都可以读/写/tmp目录,但你只能对自己的文件进行操作。即您不能删除其他用户创建的文件。

For further reading, please have a look at http://docs.doctrine-project.org/en/latest/reference/advanced-configuration.html#auto-generating-proxy-classes-optional

如需进一步阅读,请查看 http://docs.doctrine-project.org/en/latest/reference/advanced-configuration.html#auto-generating-proxy-classes-optional

You can also set the Proxies directory to somewhere else so no other applications can modify them. http://docs.doctrine-project.org/en/latest/reference/advanced-configuration.html#autoloading-proxies

您还可以将代理目录设置为其他位置,以便其他应用程序无法修改它们。http://docs.doctrine-project.org/en/latest/reference/advanced-configuration.html#autoloading-proxies

回答by michalzuber

In code after $configline you could try $config->setAutoGenerateProxyClasses(true);

$config在行后的代码中,您可以尝试 $config->setAutoGenerateProxyClasses(true);

But the CLI version is much better, because it avoids on refresh regen as in code might not avoid.

但是 CLI 版本要好得多,因为它避免了刷新重新生成,因为在代码中可能无法避免。

To change cache dir you could try:

要更改缓存目录,您可以尝试:

$cacheDir = dirname(__FILE__).'/cache';
if (!is_dir($cacheDir)) {
    mkdir($cacheDir);
}


$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode, $cacheDir);

回答by MauroK

Looks like a permission problem, first should chek on permissions for the entire application folder.

看起来是权限问题,首先应该检查整个应用程序文件夹的权限。

Also try to hard-cleanup cache by deleting app/cache/* files, and try again.

还尝试通过删除 app/cache/* 文件来硬清理缓存,然后重试。

Good luck!

祝你好运!