当前位置: 首页 > 知识库问答 >
问题:

Symfony4映射问题

弓玉书
2023-03-14

我犯了一个错误,比如:

Undefined index: id

我知道在堆栈溢出中已经出现了一些类似的问题,但解决方案对我来说并不适用。请您检查一下我的实体,以便找出到底是什么导致了这个问题?

使用者

namespace App\Entity;

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

/**
 * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
 * @ORM\HasLifecycleCallbacks()
 * @UniqueEntity("email")
 */
class User
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     * @Assert\NotBlank()
     */
    private $user_name;

    /**
     * @ORM\Column(type="string", length=100)
     */
    private $first_name;

    /**
     * @ORM\Column(type="string", length=100)
     */
    private $last_name;

    /**
     * @ORM\Column(type="string", length=255, unique=true)
     * @Assert\NotBlank()
     * @Assert\Email()
     */
    private $email;

    /**
     * @ORM\Column(type="string", length=20, nullable=true)
     */
    private $phone_number;

    /**
     * @ORM\Column(type="string", length=20, nullable=true)
     */
    private $cell_phone_number;

    /**
     * @ORM\Column(type="boolean")
     */
    private $enabled;

    /**
     * @ORM\Column(type="string", length=100)
     * @Assert\NotBlank()
     */
    private $timezone;

    /**
     * @ORM\Column(type="string", length=40)
     */
    private $password;

    /**
     * @ORM\Column(type="datetime", nullable=true)
     */
    private $last_login;

    /**
     * @var \DateTime $created
     *
     * @ORM\Column(type="datetime")
     */
    private $created;

    /**
     * @var \DateTime $updated
     *
     * @ORM\Column(type="datetime", nullable=true)
     */
    private $updated;

    /**
     * Many Users have Many Groups.
     * @ORM\ManyToMany(targetEntity="Group")
     * @ORM\JoinTable(name="user_group",
     *     joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
     *     inverseJoinColumns={@ORM\JoinColumn(name="group_id", referencedColumnName="id")}
     * )
     */
    private $group;

    /**
     * Many Users have Many Roles.
     * @ORM\ManyToMany(targetEntity="Role", cascade={"persist"})
     * @ORM\JoinTable(name="user_role",
     *     joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
     *     inverseJoinColumns={@ORM\JoinColumn(name="role_id", referencedColumnName="id")}
     * )
     */
    private $role;


    public function __construct() {
        $this->enabled = 0;
        $this->group = new ArrayCollection();
        $this->role = new ArrayCollection();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getUsername(): ?string
    {
        return $this->user_name;
    }

    public function setUsername(string $user_name): self
    {
        $this->user_name = $user_name;

        return $this;
    }

    public function getEmail(): ?string
    {
        return $this->email;
    }

    public function setEmail(?string $email): self
    {
        $this->email = $email;

        return $this;
    }

    public function getEnabled(): ?bool
    {
        return $this->enabled;
    }

    public function setEnabled(bool $enabled): self
    {
        $this->enabled = $enabled;

        return $this;
    }

    public function getTimezone(): ?string
    {
        return $this->timezone;
    }

    public function setTimezone(string $timezone): self
    {
        $this->timezone = $timezone;

        return $this;
    }

    public function getPassword(): ?string
    {
        return $this->password;
    }

    public function setPassword(string $password): self
    {
        $this->password = $password;

        return $this;
    }

    public function getLastLogin(): ?\DateTimeInterface
    {
        return $this->last_login;
    }

    public function setLastLogin(?\DateTimeInterface $last_login): self
    {
        $this->last_login = $last_login;

        return $this;
    }

    public function getPhoneNumber(): ?string
    {
        return $this->phone_number;
    }

    public function setPhoneNumber(?string $phone_number): self
    {
        $this->phone_number = $phone_number;

        return $this;
    }

    /**
     * @Assert\IsTrue(message="The password cannot match your first name")
     */
    public function isPasswordSafe()
    {
        return $this->user_name !== $this->password;
    }

    /**
     * @ORM\PrePersist
     */
    public function setCreated()
    {
        $this->created = new \DateTime();
    }

    public function getCreated()
    {
        return $this->created;
    }

    /**
     * @ORM\PreUpdate
     */
    public function setUpdated()
    {
        $this->updated = new \DateTime();
    }

    public function getUpdated()
    {
        return $this->updated;
    }

    public function getGroups()
    {
        return $this->group;
    }

    public function getRoles()
    {
        return $this->role;
    }

    public function addRoles(Role $role)
    {
        // updating inverse side
        $role->addUser($this);
        $this->role[] = $role;
    }

    public function getFirstName(): ?string
    {
        return $this->first_name;
    }

    public function setFirstName(string $first_name): self
    {
        $this->first_name = $first_name;

        return $this;
    }

    public function getLastName(): ?string
    {
        return $this->last_name;
    }

    public function setLastName(string $last_name): self
    {
        $this->last_name = $last_name;

        return $this;
    }

    public function getCellPhoneNumber(): ?string
    {
        return $this->cell_phone_number;
    }

    public function setCellPhoneNumber(?string $cell_phone_number): self
    {
        $this->cell_phone_number = $cell_phone_number;

        return $this;
    }
}

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

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

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $group_name;

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getGroupName(): ?string
    {
        return $this->group_name;
    }

    public function setGroupName(string $group_name): self
    {
        $this->group_name = $group_name;

        return $this;
    }
}

角色

namespace App\Entity;

use App\Entity\User;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

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

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

    /**
     * Many Roles have Many Permissions.
     * @ORM\ManyToMany(targetEntity="Permission")
     * @ORM\JoinTable(name="role_permission",
     *     joinColumns={@ORM\JoinColumn(name="role_id", referencedColumnName="id")},
     *     inverseJoinColumns={@ORM\JoinColumn(name="permission_id", referencedColumnName="id")}
     * )
     */
    private $permission;

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

    private $user;

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

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getRolename(): ?string
    {
        return $this->role_name;
    }

    public function setRolename(string $role_name): self
    {
        $this->role_name = $role_name;

        return $this;
    }

    public function getDescription(): ?string
    {
        return $this->description;
    }

    public function setDescription(?string $description): self
    {
        $this->description = $description;

        return $this;
    }

    public function addUser(User $user)
    {
        $this->user[] = $user;
    }
}

控制器方法

/**
 * @Route("/user/{id}", methods={"GET","HEAD"})
 * @SWG\Response(
 *     response=200,
 *     description="Returns a user or users",
 *     @SWG\Schema(
 *         type="array",
 *         @SWG\Items(ref=@Model(type=User::class, groups={"full"}))
 *     )
 * )
 * @SWG\Tag(name="user")
 */
public function get(string $id)
{
    $repo = $this->userRepository;
    $user = $repo->find($id);

    return [
        'user' => $user
    ];
}

痕迹

/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php(2607):教义\ORM\实用程序\标识符Flattener-

解决了的!

该问题是由重新定义的QuoteStrategy::get哥伦布别名()方法引起的,因为查询中添加了'。

共有1个答案

关浩壤
2023-03-14

该问题是由重新定义的QuoteStrategy::getPolinAlias()方法引起的,因为查询中添加了'。

 类似资料:
  • 我在Spring绘制地图时遇到了麻烦。我有一个Spring web项目,有用户和数据库。我得到了这个网站。xml: 今年Spring: 这是我的控制器: 我想把所有的请求映射到网址“http://localhost:8080/CRUDWebAppMavenized/users”将打开users.jsp.但我总是只看到错误页面。 IDE告诉我: 警告:org.springframework.web.

  • 我一直试图通过一个名为Guardian的中间类映射两个用户之间的一些“OneToOne”关系。当我试图检索一个用户(和他的监护人)时,从Glassfish(Open edition V4.0)返回一个内部服务器错误。但是,日志中没有显示任何类型的堆栈跟踪或任何错误。我怀疑问题是我在JPA类中的映射。 启动服务器时,我得到两个与Guardian类有关的警告,但我并不真正理解: 警告:映射到元素[me

  • 我使用Angular2β与angular2材料alpha.3他们是兼容的,但我有映射问题。 在我的公开/索引中。我有这个系统设置 但是在我的一个组件中,当我尝试导入并将其添加到指令中以在模板中使用时,如下所示: 我得到这个错误: angular2-polyfills.js:127GEThttp://localhost:8080/node_modules/@angular2-材质/输入404(未找到

  • 我使用DTO和modelMapper是为了不让某些字段可见。我有一个类别实体,可以有子类别 创建类别时,我使用模型: 在此模型中,我希望parentCategoryKeyId与父对象的categoryKeyId匹配。 例如,如果我创建了一个“顶级”类别: 它返回给我: 当我这样做时: 在我的控制器中,我将其余对象传递给DTO层,该层调用服务: 我的类别是基本的POJO: 为我服务: 我的问题是,我

  • 我目前正在使用Laravel v7。2.安装babenkoivan/scout elasticsearch驱动程序(4.2)并使用AWS elasticsearch 7.1调幅。在我的应用程序中映射了几个表,这些表工作正常,但嵌套映射存在问题,以前该映射工作正常,现在已损坏。 我将数据保存到一个表中,并将该表数据复制到AWS Elasticsearch中。我使用MySQL 5.6,所以我使用TEX