为什么在 Symfony 框架中使用 App\Entity 而不是 App\Model 的学说命名空间不起作用? (请参阅下面的错误消息)
Why do doctrine namespaces with App\Entity instead of App\Model in Symfony framework not work? (See error message below)
我是 Symfony 的新手,所以我一直在关注 YouTube 教程(https://www.youtube.com/watch?v=4LotIQWVfew&list=PL4zTp90ptQJNjL5xzhn0fgVgd0k1YkpL-&index=7&ab_channel=thomasd_codes - 第 7 集),但从 8:00 分钟开始就卡住了。我做了那里显示的:
我将文件夹的名称“Model”更改为“Entity”,还有 Subscriptions.php 和 SubscriptionsController.php 文件中给出的命名空间。 doctrine.yaml 文件也已正确设置,前缀为“App\Entity” - 一切如视频中所示。但是 symfony 框架总是抛出如下异常:
Screenshot of the Error Message
“使用”语句也已设置。
我在 IIS 10 上使用 PHP 版本 7.1 以及 Symfony 4.4.18(关于版本的修正声明)运行 - 也许与此有关?
我已经对此进行了研究,但到目前为止我还没有找到解决这个问题的方法。
如果有人可以帮助我,那就太好了!提前致谢!
SubscriptionsController.php:
<?php
declare(strict_types=1);
namespace App\Controller;
use App\Entity\Subscription;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
class SubscriptionsController extends AbstractController
{
public function list(): Response
{
$response = new Response();
$dataArray = [
'success' => true,
'subscriptions' => $this->generateSubscriptions(),
];
return $this->json($dataArray);
}
protected function generateSubscriptions(): array
{
$returnArray = [];
$returnArray[] = (new Subscription)
->setStartDate(new \DateTime())
->setName("Netflix");
$returnArray[] = (new Subscription)
->setStartDate(new \DateTime("2021-11-26"))
->setName("Amazon-Prime");
$returnArray[] = (new Subscription)
->setStartDate(new \DateTime())
->setName("ADAC");
return $returnArray;
}
}
和Subscriptions.php:
<?php
declare(strict_types=1);
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
class Subscription implements \JsonSerializable
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
protected $id;
/**
* @ORM\Column(type="string", length=255)
*/
protected $name;
/**
* @ORM\Column(type="datetime")
*/
protected $startDate;
public function jsonSerialize()
{
return [
'name' => $this->name,
'startDate' => $this->startDate,
];
}
public function getName(): string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getStartDate(): \DateTime
{
return $this->startDate;
}
public function setStartDate(\DateTime $startDate): self
{
$this->startDate = $startDate;
return $this;
}
}
composer.json:
{
"type": "project",
"license": "proprietary",
"require": {
"php": ">=7.1.3",
"ext-ctype": "*",
"ext-iconv": "*",
"composer/package-versions-deprecated": "1.11.99.1",
"doctrine/doctrine-bundle": "^2.2",
"doctrine/doctrine-migrations-bundle": "^2.2",
"doctrine/orm": "^2.7",
"symfony/console": "4.4.*",
"symfony/dotenv": "4.4.*",
"symfony/flex": "^1.3.1",
"symfony/framework-bundle": "4.4.*",
"symfony/proxy-manager-bridge": "4.4.*",
"symfony/yaml": "4.4.*"
},
"config": {
"preferred-install": {
"*": "dist"
},
"sort-packages": true
},
"autoload": {
"psr-4": {
"App\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"App\Tests\": "tests/"
}
},
"replace": {
"paragonie/random_compat": "2.*",
"symfony/polyfill-ctype": "*",
"symfony/polyfill-iconv": "*",
"symfony/polyfill-php71": "*",
"symfony/polyfill-php70": "*",
"symfony/polyfill-php56": "*"
},
"scripts": {
"auto-scripts": {
"cache:clear": "symfony-cmd",
"assets:install %PUBLIC_DIR%": "symfony-cmd"
},
"post-install-cmd": [
"@auto-scripts"
],
"post-update-cmd": [
"@auto-scripts"
]
},
"conflict": {
"symfony/symfony": "*"
},
"extra": {
"symfony": {
"allow-contrib": false,
"require": "4.4.*"
}
}
}
感谢 Jakumi 和 Hamham 的评论!
将文件重命名为“Subscription.php”终于解决了问题!
我是 Symfony 的新手,所以我一直在关注 YouTube 教程(https://www.youtube.com/watch?v=4LotIQWVfew&list=PL4zTp90ptQJNjL5xzhn0fgVgd0k1YkpL-&index=7&ab_channel=thomasd_codes - 第 7 集),但从 8:00 分钟开始就卡住了。我做了那里显示的: 我将文件夹的名称“Model”更改为“Entity”,还有 Subscriptions.php 和 SubscriptionsController.php 文件中给出的命名空间。 doctrine.yaml 文件也已正确设置,前缀为“App\Entity” - 一切如视频中所示。但是 symfony 框架总是抛出如下异常:
Screenshot of the Error Message
“使用”语句也已设置。
我在 IIS 10 上使用 PHP 版本 7.1 以及 Symfony 4.4.18(关于版本的修正声明)运行 - 也许与此有关?
我已经对此进行了研究,但到目前为止我还没有找到解决这个问题的方法。 如果有人可以帮助我,那就太好了!提前致谢!
SubscriptionsController.php:
<?php
declare(strict_types=1);
namespace App\Controller;
use App\Entity\Subscription;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
class SubscriptionsController extends AbstractController
{
public function list(): Response
{
$response = new Response();
$dataArray = [
'success' => true,
'subscriptions' => $this->generateSubscriptions(),
];
return $this->json($dataArray);
}
protected function generateSubscriptions(): array
{
$returnArray = [];
$returnArray[] = (new Subscription)
->setStartDate(new \DateTime())
->setName("Netflix");
$returnArray[] = (new Subscription)
->setStartDate(new \DateTime("2021-11-26"))
->setName("Amazon-Prime");
$returnArray[] = (new Subscription)
->setStartDate(new \DateTime())
->setName("ADAC");
return $returnArray;
}
}
和Subscriptions.php:
<?php
declare(strict_types=1);
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
class Subscription implements \JsonSerializable
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
protected $id;
/**
* @ORM\Column(type="string", length=255)
*/
protected $name;
/**
* @ORM\Column(type="datetime")
*/
protected $startDate;
public function jsonSerialize()
{
return [
'name' => $this->name,
'startDate' => $this->startDate,
];
}
public function getName(): string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getStartDate(): \DateTime
{
return $this->startDate;
}
public function setStartDate(\DateTime $startDate): self
{
$this->startDate = $startDate;
return $this;
}
}
composer.json:
{
"type": "project",
"license": "proprietary",
"require": {
"php": ">=7.1.3",
"ext-ctype": "*",
"ext-iconv": "*",
"composer/package-versions-deprecated": "1.11.99.1",
"doctrine/doctrine-bundle": "^2.2",
"doctrine/doctrine-migrations-bundle": "^2.2",
"doctrine/orm": "^2.7",
"symfony/console": "4.4.*",
"symfony/dotenv": "4.4.*",
"symfony/flex": "^1.3.1",
"symfony/framework-bundle": "4.4.*",
"symfony/proxy-manager-bridge": "4.4.*",
"symfony/yaml": "4.4.*"
},
"config": {
"preferred-install": {
"*": "dist"
},
"sort-packages": true
},
"autoload": {
"psr-4": {
"App\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"App\Tests\": "tests/"
}
},
"replace": {
"paragonie/random_compat": "2.*",
"symfony/polyfill-ctype": "*",
"symfony/polyfill-iconv": "*",
"symfony/polyfill-php71": "*",
"symfony/polyfill-php70": "*",
"symfony/polyfill-php56": "*"
},
"scripts": {
"auto-scripts": {
"cache:clear": "symfony-cmd",
"assets:install %PUBLIC_DIR%": "symfony-cmd"
},
"post-install-cmd": [
"@auto-scripts"
],
"post-update-cmd": [
"@auto-scripts"
]
},
"conflict": {
"symfony/symfony": "*"
},
"extra": {
"symfony": {
"allow-contrib": false,
"require": "4.4.*"
}
}
}
感谢 Jakumi 和 Hamham 的评论!
将文件重命名为“Subscription.php”终于解决了问题!