Symfony 3.4-扩展捆绑实体

时间:2018-11-29 16:08:39

标签: symfony entity bundle

我正在使用symfony 3.4和捆绑包l3-db-userbundle,尝试将属性添加到捆绑包的实体时遇到问题...

这是我在AppBundle \ Entity中的实体

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use L3\Bundle\DbUserBundle\Entity\User;
/**
 * XUser
 *
 * @ORM\Table(name="x_user")
 * @ORM\Entity
 */
 class XUser extends User
 {
 /**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer", nullable=false)
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="IDENTITY")
 */
private $id;

/**
 * @var string
 *
 * @ORM\Column(name="nombre", type="string", length=100, nullable=false)
 */
private $nombre;

/**
 * @var string
 *
 * @ORM\Column(name="apellido", type="string", length=100, nullable=false)
 */
private $apellido;

/**
 * @var string
 *
 * @ORM\Column(name="dni", type="string", length=20, nullable=false)
 */
private $dni;

/**
 * @var string
 *
 * @ORM\Column(name="uid", type="string", length=15, nullable=false)
 */
private $uid;

/**
 * @var \Doctrine\Common\Collections\Collection
 *
 * @ORM\ManyToMany(targetEntity="XRole", inversedBy="user")
 * @ORM\JoinTable(name="x_user_role",
 *   joinColumns={
 *     @ORM\JoinColumn(name="user_id", referencedColumnName="id")
 *   },
 *   inverseJoinColumns={
 *     @ORM\JoinColumn(name="role_id", referencedColumnName="id")
 *   }
 * )
 */
private $role;

/**
 * Constructor
 */
public function __construct()
{
    $this->role = new \Doctrine\Common\Collections\ArrayCollection();
}

/**
 * @return int
 */
public function getId()
{
    return $this->id;
}

/**
 * Set id
 *
 * @param string $id
 * @return User
 */
public function setId($id) {
    $this->id = $id;
    return $this;
}

/**
 * @return string
 */
public function getNombre()
{
    return $this->nombre;
}

/**
 * @param string $nombre
 */
public function setNombre(string $nombre)
{
    $this->nombre = $nombre;
}

/**
 * @return string
 */
public function getApellido()
{
    return $this->apellido;
}

/**
 * @param string $apellido
 */
public function setApellido(string $apellido)
{
    $this->apellido = $apellido;
}

/**
 * @return string
 */
public function getDni()
{
    return $this->dni;
}

/**
 * @param string $dni
 */
public function setDni(string $dni)
{
    $this->dni = $dni;
}

/**
 * @return string
 */
public function getUid()
{
    return $this->uid;
}

/**
 * Set uid
 *
 * @param string $uid
 * @return User
 */
public function setUid($uid) {
    $this->uid = $uid;
    return $this;
}


/**
 * @return \Doctrine\Common\Collections\Collection
 */
public function getRole()
{
    return $this->role;
}

/**
 * @param \Doctrine\Common\Collections\Collection $role
 */
public function setRole(\Doctrine\Common\Collections\Collection $role)
{
    $this->role = $role;
}

}

这是供应商的类别

<?php

namespace L3\Bundle\DbUserBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
use Doctrine\Common\Collections\ArrayCollection;
/**
 * @ORM\Table(name="x_user")
 * @ORM\Entity(repositoryClass="L3\Bundle\DbUserBundle\Repository    \UserRepository")
 */
class User implements UserInterface, \Serializable {

/**
 * @var integer $id
 * @ORM\Id
 * @ORM\Column(name="id", type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @ORM\Column(name="uid", type="string", length=15)
 */
private $uid;

/**
 * @ORM\ManyToMany(targetEntity="Role", cascade={"persist"})
 * @ORM\JoinTable(name="x_user_role",
 *     joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
 *     inverseJoinColumns={@ORM\JoinColumn(name="role_id", referencedColumnName="id")}
 * )
 * @var ArrayCollection $roles
 */
private $roles;


public function __construct() {

}

/**
 * Get id
 *
 * @return integer 
 */
public function getId() {
    return $this->id;
}

/**
 * Set id
 *
 * @param string $id
 * @return User
 */
public function setId($id) {
    $this->id = $id;
    return $this;
}

/**
 * Get uid
 *
 * @return string 
 */
public function getUid() {
    return $this->uid;
}

/**
 * Set uid
 *
 * @param string $uid
 * @return User
 */
public function setUid($uid) {
    $this->uid = $uid;
    return $this;
}

//Source : http://www.metod.si/symfony2-error-usernamepasswordtokenserialize-must-return-a-string-or-null/
public function serialize() {
    return serialize(array(
      $this->id,
      $this->uid
    ));
}

public function unserialize($serialize) {
    list(
      $this->id,
      $this->uid
      ) = unserialize($serialize);
}

public function eraseCredentials() {
    //pas d'implémentation nécessaire
}

public function getPassword() {
    //pas d'implémentation nécessaire
}

public function getRoles() {
    $roles = array();

    foreach ($this->roles as $role) {
        if (!in_array($role->getRole(), $roles)) {
            $roles[] = $role->getRole();
        }
    }

    $roles[] = "ROLE_USER";

    return $roles;
}

public function getSalt() {
    //pas d'implémentation nécessaire
}

public function getUsername() {
    return $this->getUid();
}

public function __toString() {
    return $this->getUid();
}

}

当我尝试从数据库中获取数据时,它显示了一个错误:尝试调用名为“ L3 \ Bundle \ DbUserBundle \ Entity \ User”类的未定义方法“ getNombre”。

这是我遇到问题的方法:

<?php

namespace AppBundle\Controller;

use AppBundle\Entity\XUser;
use AppBundle\Entity\XUserOffice;
use AppBundle\Form\UserType;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;


/**
 * Class UserController
 * @Route("/users")
 */
class UserController extends Controller
{


/**
 * @Route("/", name="users_list")
 * @Template("@App/user/index.html.twig")
 */
public function indexAction(Request $request)
{
    $users = $this->getDoctrine()->getRepository('AppBundle:XUser')->findAll();

    $list = [];
    foreach ($users as $user) {
        $role = $this->getRole($user->getId());
        $dat['id'] = $user->getId();
        $dat['name'] = $user->getNombre() . ' ' . $user->getApellido();
        $dat['dni'] = $user->getDni();
        $dat['role'] = $role['role'];
        $list[] = $dat;
    }

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

0 个答案:

没有答案