简体   繁体   English

无法确定访问类型

[英]Could not determine access type

I need to create a master entity named Users and another one named Specialties.我需要创建一个名为 Users 的主实体和另一个名为 Specialties 的主实体。 A user can have many specialties.一个用户可以有很多专长。

There is my users entity:有我的用户实体:

<?php

namespace App\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\AdvancedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;

/**
 * @ORM\Entity(repositoryClass="App\Repository\UsersRepository")
 * @UniqueEntity(
 *     fields={"email"},
 *     errorPath="email",
 *     message="It appears you have already registered with this email."
 * )
 */
class Users implements UserInterface
{

    /**
     * Users constructor.
     */
    public function __construct()
    {
        $this->created_at = new \DateTime();
        $this->specialtyId = new ArrayCollection();
    }

    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     * @Groups({"public"})
     */
    private $id;

    //Other properties there...

    /**
     * @ORM\ManyToMany(targetEntity="App\Entity\Specialties", inversedBy="users")
     */
    private $specialtyId;

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

    /**
     * @return Collection|Specialties[]
     */
    public function getSpecialtyId(): Collection
    {
        return $this->specialtyId;
    }

    public function addSpecialtyId(Specialties $specialtyId): self
    {
        if (!$this->specialtyId->contains($specialtyId)) {
            $this->specialtyId[] = $specialtyId;
        }
        return $this;
    }

    public function removeSpecialtyId(Specialties $specialtyId): self
    {
        if ($this->specialtyId->contains($specialtyId)) {
            $this->specialtyId->removeElement($specialtyId);
        }
        return $this;
    }

}

There is my specialties entity:有我的专业实体:

<?php

namespace App\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

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

    //Other properties there...

    /**
     * @ORM\ManyToMany(targetEntity="App\Entity\Users", mappedBy="specialtyId")
     */
    private $users;

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

    /**
     * @return Collection|Users[]
     */
    public function getUsers(): Collection
    {
        return $this->users;
    }

    public function addUser(Users $user): self
    {
        if (!$this->users->contains($user)) {
            $this->users[] = $user;
            $user->addSpecialtyId($this);
        }

        return $this;
    }

    public function removeUser(Users $user): self
    {
        if ($this->users->contains($user)) {
            $this->users->removeElement($user);
            $user->removeSpecialtyId($this);
        }

        return $this;
    }
}

When I try to save the registration form, I got this exception:当我尝试保存注册表时,出现以下异常:

**> Could not determine access type for property "specialtyId" in class **> 无法确定类中属性“specialtyId”的访问类型

"App\\Entity\\Users": The property "specialtyId" in class "App\\Entity\\Users" can be defined with the methods "addSpecialtyId()", "removeSpecialtyId()" but the new value must be an array or an instance of \\Traversable, "App\\Entity\\Specialties" given.** “App\\Entity\\Users”:类“App\\Entity\\Users”中的属性“specialtyId”可以使用“addSpecialtyId()”、“removeSpecialtyId()”方法定义,但新值必须是数组或实例\\Traversable,给出了“App\\Entity\\Specialties”。**

A new table is created named: users_specialties that contains: users_id/specialties_id创建一个名为:users_specialties 的新表,其中包含:users_id/specialties_id

Tried this on Users Entity but still same error:在用户实体上试过这个,但仍然是同样的错误:

public function addSpecialtyId(Specialties $specialtyId): self
    {
        if (!$this->specialtyId->contains($specialtyId)) {
            $this->specialtyId[] = $specialtyId;
        }
        return $this;
    }

    public function removeSpecialtyId(Specialties $specialtyId): self
    {
        if ($this->specialtyId->contains($specialtyId)) {
            $this->specialtyId->removeElement($specialtyId);
        }
        return $this;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Symfony - 无法确定属性“id”的访问类型 - Symfony - Could not determine access type for property “id” 在类“ App \\ Entity \\ GameGenre”中无法确定属性“游戏”的访问类型: - Could not determine access type for property “games” in class “App\Entity\GameGenre”: Symfony 4 | ManyToMany关系-无法确定属性的访问类型 - Symfony 4 | ManyToMany Relation - Could not determine access type for property 使用EntityType表单构建器时,Symfony无法确定访问类型 - Symfony Could not determine access type when using EntityType form builder 将&#39;by reference&#39;设置为false,在symfony表单上抛出&#39;无法确定访问类型&#39; - setting 'by reference' to false, on symfony form throws 'Could not determine access type' 对于某些,但不是全部,表单提交得到“无法确定属性的访问类型”id“。” - for some, but not all, form submission got “Could not determine access type for property ”id“.” 无法确定 class “App\Entity\XXXX”中属性“image”的访问类型。 Symfony 4 - EasyAdmin 3.2 - VichUploader - Could not determine access type for property “image” in class “App\Entity\XXXX”. Symfony 4 - EasyAdmin 3.2 - VichUploader PHP PDO PostgreSQL 无法确定数据类型 - PHP PDO PostgreSQL Could Not Determine Data Type PHP准备的postgres查询失败,并显示:“无法确定参数$ 1的数据类型” - PHP prepared postgres query fails with:“could not determine data type of parameter $1” PostgreSQL无法使用PHP PDO确定参数$ 1的数据类型 - Postgresql could not determine data type of parameter $1 using php pdo
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM