Quantcast
Channel: Active questions tagged symfony4 - Stack Overflow
Viewing all articles
Browse latest Browse all 3924

Could not determine access type

$
0
0

I need to create a master entity named Users and another one named 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

"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.**

A new table is created named: users_specialties that contains: users_id/specialties_id


Viewing all articles
Browse latest Browse all 3924

Trending Articles