"Error [Semantical Error] The annotation "@Doctrine\ORM\Mapping\OnetoMany" in 属性" 在 Gitlab CI 但不在本地环境或生产中

"Error [Semantical Error] The annotation "@Doctrine\ORM\Mapping\OnetoMany" in property" on Gitlab CI but not in local env or production

我已经在 symfony 4.4 应用程序上编写了一些测试,我想 运行 在我的 gitlab CI 上进行这些测试,但是我得到了这个错误:

Error [Semantical Error] The annotation "@Doctrine\ORM\Mapping\OnetoMany" in property App\Entity\Client::$logsRegions was never imported . Did you maybe forget to add a "use" statement for this annotation?

但是我在本地环境和生产环境中都没有这个错误,我在我的实体中导入 Doctrine\ORM\Mapping as ORM

这是我的客户实体

<?php

namespace App\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use App\Entity\Region\AccessLogRegion;
use App\Entity\Traits\ClientTrait;
use Symfony\Component\Validator\Constraints as Assert;
/**
 * Client
 *
 * @ORM\Table(name="client")
 * @ORM\Entity(repositoryClass="App\Repository\ClientRepository")
 * @ORM\HasLifecycleCallbacks
 */
class Client
{
    use ClientTrait;
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

   //...

    /**
     * @ORM\OnetoMany(targetEntity=AccessLogRegion::class, mappedBy="client")
     */
    private $logsRegions;

    public function __construct()
    {
        $this->logsRegions = new ArrayCollection();
    }

    public function getLogsRegions()
    {
        return $this->logsRegions;
    }
}

我的.gitlab-ci.yml

image: jakzal/phpqa:php7.4

before_script:
  - composer install

cache:
  paths:
    - vendor/

stages:
  - UnitTests

phpunit:
  image: php:7.4-apache
  stage: UnitTests
  services:
    - name: mysql:5.7
      alias: mysql
  variables:
    MYSQL_ROOT_PASSWORD: project
    MYSQL_DATABASE: project_test
    MYSQL_USER: root
    MYSQL_PASSWORD: project
    DATABASE_URL: 'mysql://root:project@mysql:3306/project_test'
  before_script:
    - apt-get update && apt-get install -y git libzip-dev
    - curl -sSk https://getcomposer.org/installer | php -- --disable-tls && mv composer.phar /usr/local/bin/composer
    - docker-php-ext-install mysqli pdo pdo_mysql zip
    - php bin/console doctrine:database:drop --force --env=test
    - php bin/console doctrine:database:create --env=test
    - php bin/console doctrine:schema:update --env=test --force
  script:
    - php bin/phpunit
  allow_failure: false

完整的gitlab错误日志:

$ php bin/console doctrine:database:drop --force --env=test Dropped database /tmp/test/test_db.sqlite for connection named default

$ php bin/console doctrine:database:create --env=test Created database /tmp/test/test_db.sqlite for connection named default $ php bin/console doctrine:schema:update --env=test --force

In AnnotationException.php line 39: [Semantical Error] The annotation "@Doctrine\ORM\Mapping\OnetoMany" in prop erty App\Entity\Client::$logsRegions was never imported . Did you maybe forget to add a "use" statement for this annotation?

doctrine:schema:update [--em EM] [--complete] [--dump-sql] [-f|--force] [-h|--help] [-q|--quiet] [-v|vv|vvv|--verbose] [-V|--version] [--ansi] [--no-ansi] [-n|--no-interaction] [-e|--env ENV] [--no-debug] [--]

Cleaning up file based variables

ERROR: Job failed: exit code 1

我不明白为什么它不起作用,欢迎任何帮助:)

看来问题是你的注释 OnetoMany 因大小写而无效,正确的拼写应该是 OneToMany.

尝试将其替换为:

/**
 * @ORM\OneToMany(targetEntity=AccessLogRegion::class, mappedBy="client")
 */
private $logsRegions;