在坚持教义时遇到问题

问题描述 投票:0回答:1

我在尝试使用 Doctrine ORM 在我的 PHP 应用程序中保留 Thos\Index 类的实例时遇到问题。我收到以下错误:“未捕获的 Doctrine\ORM\Mapping\MappingException:类‘Thos\Index’不是有效的实体或映射的超类”。 Index 类在我的项目中已正确定义,我相信它已正确映射。是什么导致了这个错误,我该如何解决?

这是正确生成的实体:

<?php

namespace Thos;

/**
 * Index
 */
class Index
{
    /**
     * @var string
     */
    private $idx;

    /**
     * @var string
     */
    private $des;

    /**
     * @var \Doctrine\Common\Collections\Collection
     */
    private $accions = array();

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->accions = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * Set idx.
     *
     * @param string $idx
     *
     * @return Index
     */
    public function setIdx($idx)
    {
        $this->idx = $idx;

        return $this;
    }

    /**
     * Get idx.
     *
     * @return string
     */
    public function getIdx()
    {
        return $this->idx;
    }

    /**
     * Set des.
     *
     * @param string $des
     *
     * @return Index
     */
    public function setDes($des)
    {
        $this->des = $des;

        return $this;
    }

    /**
     * Get des.
     *
     * @return string
     */
    public function getDes()
    {
        return $this->des;
    }

    /**
     * Add accion.
     *
     * @param \Thos\Accio $accion
     *
     * @return Index
     */
    public function addAccion(\Thos\Accio $accion)
    {
        $this->accions[] = $accion;

        return $this;
    }

    /**
     * Remove accion.
     *
     * @param \Thos\Accio $accion
     *
     * @return boolean TRUE if this collection contained the specified element, FALSE otherwise.
     */
    public function removeAccion(\Thos\Accio $accion)
    {
        return $this->accions->removeElement($accion);
    }

    /**
     * Get accions.
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getAccions()
    {
        return $this->accions;
    }
}

运行此脚本时出现此错误:

PHP Fatal error:  Uncaught Doctrine\ORM\Mapping\MappingException: Class "Thos\Index" is not a valid entity or mapped super class.

这是剧本

<?php
require_once './bootstrap.php';

use Thos\Index;


//Objetos del tipo indice (idx:varchar(10), des:varchar(50))
$ibex35 = (new Index())
    ->setIdx('Ibex35')
    ->setDes('Índex de la Borsa Esanyola (35 més grans)');


$entityManager->persist($ibex35);
$entityManager->flush();

这是我的 bootstrap.php 文件:


<?php
// bootstrap.php
require_once __DIR__ . "/vendor/autoload.php";

use Doctrine\DBAL\DriverManager;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\ORMSetup;

$paths = [__DIR__ . '/src/classes/model'];
$isDevMode = false;

// the connection configuration
$dbParams = [
    'driver'   => 'pdo_mysql',
    'user'     => 'sergio',
    'password' => 'sergio',
    'dbname'   => 'doctrine_PaniaguaSergio',
];

$config = ORMSetup::createAttributeMetadataConfiguration($paths, $isDevMode);
$connection = DriverManager::getConnection($dbParams, $config);
$entityManager = new EntityManager($connection, $config);

这是我编译前的实体,我使用了属性注释:

<?php

declare(strict_types=1);

namespace Thos;

use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\InverseJoinColumn;
use Doctrine\ORM\Mapping\JoinColumn;
use Doctrine\ORM\Mapping\JoinTable;
use Doctrine\ORM\Mapping\ManyToMany;
use Doctrine\ORM\Mapping\Table;


#[Entity]
#[Table(name: 'tbl_index')]
class Index
{
    #[Id]
    #[Column(type: 'string', length: 10)]
    private string $idx;

    #[Column(type: 'string', length: 50)]
    private string $des;

    #[ManyToMany(targetEntity: Accio::class)]
    #[JoinTable(name: 'rel_index_accions')]
    #[JoinColumn(name: 'index_id', referencedColumnName: 'idx')]
    #[InverseJoinColumn(name: 'accio_id', referencedColumnName: 'id')]
    private array $accions;
}

我需要这个问题的解决方案!

php doctrine-orm doctrine
1个回答
0
投票

我认为这主要是一个名称空间类型问题,也许是对 Doctrine 如何映射其实体的误解。自从我独立使用 Doctrine 以来已经有一段时间了,所以我做了一个简单的测试用例。强烈建议您使用 Symfony 框架来处理此类事情。它隐藏了很多细节。

#bootstrap.php
require_once __DIR__ . "/vendor/autoload.php";

use Doctrine\DBAL\DriverManager;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\ORMSetup;

$paths = [__DIR__ . '/src/Entity']; // This is very important
$isDevMode = false;

// the connection configuration
$dbParams = [
    'driver'   => 'pdo_mysql',
    'user'     => 'app',
    'password' => '!ChangeMe!',
    'dbname'   => 'app',
];

$config = ORMSetup::createAttributeMetadataConfiguration($paths, $isDevMode);
$connection = DriverManager::getConnection($dbParams, $config);
$entityManager = new EntityManager($connection, $config);
# test.php
require_once './bootstrap.php';

use Thos\Entity\Index;

//Objetos del tipo indice (idx:varchar(10), des:varchar(50))
$ibex35 = new Index('IBex35','description');

$entityManager->persist($ibex35);
$entityManager->flush();
# src/Entity/Index.php
namespace Thos\Entity; # Note the Entity namespace

use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\InverseJoinColumn;
use Doctrine\ORM\Mapping\JoinColumn;
use Doctrine\ORM\Mapping\JoinTable;
use Doctrine\ORM\Mapping\ManyToMany;
use Doctrine\ORM\Mapping\Table;


#[Entity]
#[Table(name: 'tbl_index')]
class Index
{
    #[Id]
    #[Column(type: 'string', length: 10)]
    private string $idx;

    #[Column(type: 'string', length: 50)]
    private string $des;

    public function __construct (string $idx, string $des)
    {
      $this->idx = $idx;
      $this->des = $des;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.