Symfony4映射问题

时间:2018-08-15 11:07:04

标签: symfony doctrine-orm orm symfony4

我遇到了类似的错误:

Undefined index: id

我知道在Stack Overflow上已经出现了类似的问题,但是这些解决方案对我不起作用。您能不能查看一下我的实体,以便弄清到底是什么引起了这个问题?

用户

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 \ Utility \ IdentifierFlattener-> flattenIdentifier(Object(Doctrine \ ORM \ Mapping \ ClassMetadata),   数组)   /vendor/doctrine/orm/lib/Doctrine/ORM/Internal/Hydration/SimpleObjectHydrator.php(146):   原则\ ORM \ UnitOfWork-> createEntity('App \ Entity \ User',Array,   数组)   /vendor/doctrine/orm/lib/Doctrine/ORM/Internal/Hydration/SimpleObjectHydrator.php(68):   主义\ ORM \内部\水化\ SimpleObjectHydrator-> hydrateRowData(Array,   数组)   /vendor/doctrine/orm/lib/Doctrine/ORM/Internal/Hydration/AbstractHydrator.php(152):   主义\ ORM \ Internal \ Hydration \ SimpleObjectHydrator-> hydrateAllData()   /vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php(741):   Doctrine \ ORM \ Internal \ Hydration \ AbstractHydrator-> hydratesAll(Object(Doctrine \ DBAL \ Driver \ PDOStatement),   对象(Doctrine \ ORM \ Query \ ResultSetMapping),数组   /vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php(751):   原则\ ORM \ Persisters \ Entity \ BasicEntityPersister-> load(Array,NULL)   /vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php(460):   原则\ ORM \ Persisters \ Entity \ BasicEntityPersister-> loadById(数组)   /vendor/doctrine/orm/lib/Doctrine/ORM/EntityRepository.php(154):   Doctrine \ ORM \ EntityManager-> find('App \ Entity \ User',Array,NULL,   NULL)/src/Controller/UserController.php(61):   原则\ ORM \ EntityRepository-> find('1')   /vendor/symfony/http-kernel/HttpKernel.php(149):   App \ Controller \ UserController-> get('1')   /vendor/symfony/http-kernel/HttpKernel.php(66):   Symfony \ Component \ HttpKernel \ HttpKernel-> handleRaw(Object(Symfony \ Component \ HttpFoundation \ Request),   1)/vendor/symfony/http-kernel/Kernel.php(188):   Symfony \ Component \ HttpKernel \ HttpKernel->句柄(Object(Symfony \ Component \ HttpFoundation \ Request),   1,true)/public/index.php(37):   Symfony \ Component \ HttpKernel \ Kernel-> handle(Object(Symfony \ Component \ HttpFoundation \ Request))   {main}

已解决!

该问题是由重新定义的QuoteStrategy::getColumnAlias()方法引起的 因为在查询中添加了“`”。

1 个答案:

答案 0 :(得分:1)

此问题是由于重新定义的QuoteStrategy :: getColumnAlias()方法引起的,因为在查询中添加了“`”。