【问题标题】:Class does not exist Doctrine类不存在主义
【发布时间】:2021-01-28 17:10:04
【问题描述】:

好的,所以我似乎无法自己解决这个问题......

我正在使用 symfony 和教义构建一个应用程序。

出于某种原因,我需要在控制器中调用我的数据库,但出现错误。

每次我尝试调用这个函数时:

private function getAgency(string $userid)
    {
        $idAgency=$this->getDoctrine()->getRepository(User::class)->find($userid)->getidAgency();
        $Agency=$this->getDoctrine()->getRepository(Agency::class)->find($idAgency);
        return $Agency;
    
    }

我遇到了这个错误:Class App\Entity\Agency does not exist 女巫我不明白,因为代理实体肯定存在,代理.php 文件:

namespace App\Entity;


use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;


/**
 * @ORM\Entity(repositoryClass="App\Repository\AgencyRepository")
 * @UniqueEntity("nameAgency")
 */
class Agency
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="integer")
     * @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="idAgency")
     */
    private $user_id;

    /**
     * @ORM\Column(type="string", length=180, unique=true)
     */
    private $nameAgency;

    /**
     * @ORM\Column(type="integer")
     */
    private $idDefaultContact;

    /**
     * @ORM\Column(type="string", nullable=true)
     * @Assert\NotBlank
     */
    private $nameContact;
    /**
     * @ORM\Column(type="integer", nullable=true)
     */
    private $idContact;

    /**
     * @ORM\Column(type="string", nullable=true)
     */
    private $NameSpecialContact;
    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $commentary;

    // Contructors

    public function getnameAgency(): ?string
    {
        return $this->nameAgency;
    }

    public function setnameagency(string $nameAgency)
    {
        $this->nameAgency = $nameAgency;
        return $this;
    }

    public function getnameContact(): ?string
    {
        return $this->nameContact;
    }
    public function setnameContact(string $nameContact)
    {
        $this->nameContact = $nameContact;
        return $this;
    }

    public function getcommentary(): ?string
    {
        return $this->commentary;
    }

    public function setcommentary(string $commentary)
    {
        $this->commentary = $commentary;
        return $this;
    }

}

据我所知,我以与代理实体相同的方式调用用户实体,然后删除所有代理调用;使用用户实体的部分工作得很好......

我在这里缺少什么?

编辑:正如指出的那样;我使用了use App\Entity\User; use App\Entity\Agency;

但它不起作用

【问题讨论】:

  • 你应该使用关键字
  • 什么意思?
  • 代理对象无论您呼叫或显示的地方
  • private function getAgency(string $userid) { $idAgency=$this->getDoctrine()->getRepository(User::class)->find($userid)->getidAgency(); $Agency=$this->getDoctrine()->getRepository(Agency::class)->find($idAgency); return $Agency; } 可能没有use 关键字
  • 你是这个意思吗? ``` 使用应用\实体\用户;使用应用\实体\代理; ```我绝对用用;但它也不起作用......

标签: php symfony doctrine-orm


【解决方案1】:

类不存在可能是由 3 个不同点引起的:

  1. 错误地声明了类本身(命名空间或类名)
  2. 调用未使用正确的 use 语句
  3. 文件夹架构与声明的命名空间不匹配

正如您指出的那样,1 和 2 似乎不适用于您的情况。你能检查 3 吗?

【讨论】:

  • 它可能是这样有效的......这个文件夹组织并使其工作?
  • 从您的 src 文件夹中,如果您有 Entity/InfluenceurManagement/Agency.php,那么 Agency.php 的命名空间应该是 Entity/InfluenceurManagement 而对于 Entity/User.php 它应该只是 Entity跨度>
  • 更改命名空间似乎并没有解决我的问题,但将文件移动到实体文件夹(InfluenceurManagement 之外)似乎已经解决了它
【解决方案2】:

可能缺少使用关键字


namespace App\Bar;

use App\Entity\Agency;


class Foo
{

private function getAgency(string $userid)
    {
        $idAgency=$this->getDoctrine()->getRepository(User::class)->find($userid)->getidAgency();
        $Agency=$this->getDoctrine()->getRepository(Agency::class)->find($idAgency);
        return $Agency;
    
    }
}

【讨论】:

  • 不,他们在场;原帖里没说
  • stackoverflow.com/questions/10847600/… 与您的问题类似的链接可能会对您有所帮助
  • 好的,您的链接没有帮助;我试图找到一个我可以错误调用我的 Agency 对象但可以找到任何的地方......但是该类肯定存在并且 VS Code 检测到我的 getRepository(Agency::class) 作为对此类的调用
猜你喜欢
  • 2021-04-11
  • 1970-01-01
  • 2015-02-11
  • 1970-01-01
  • 1970-01-01
  • 2015-06-03
  • 1970-01-01
  • 2017-02-09
  • 1970-01-01
相关资源
最近更新 更多