属性 或方法未在实体中定义

Property or method not defined in entity

我有一个 Usuario 实体定义如下:

namespace Checkengine\DashboardBundle\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;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use JMS\Serializer\Annotation as Serializer;

/**
 * Usuario.
 *
 * @ORM\Table(name="usuarios")
 * @ORM\Entity(repositoryClass="Checkengine\DashboardBundle\Repository\UsuarioRepository")
 * @ORM\HasLifecycleCallbacks()
 * @UniqueEntity("email")
 *
 * @Serializer\ExclusionPolicy("all")
 */
class Usuario implements UserInterface, \Serializable
{
    ...

    /**
     * @var integer
     *
     * @todo No recibir ofertas del usuario.
     *
     * @ORM\ManyToMany(targetEntity="Checkengine\DashboardBundle\Entity\Empresa")
     * @ORM\JoinTable(name="no_ofertas",
     *      joinColumns={@ORM\JoinColumn(name="usuario_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="empresa_id", referencedColumnName="id")}
     * )
     *
     * @Serializer\Expose
     * @Serializer\Type("ArrayCollection<Checkengine\DashboardBundle\Entity\Empresa>")
     * @Serializer\MaxDepth(1)
     */
    private $noOfertas;

    ...

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

    ...

    /**
     * Add noOfertas.
     * @param \Checkengine\DashboardBundle\Entity\Empresa $noOfertas
     * @return Usuario
     */
    public function addNoOfertas(Empresa $noOfertas)
    {
        $this->noOfertas[] = $noOfertas;

        return $this;
    }

    /**
     * Remove noOfertas.
     * @param \Checkengine\DashboardBundle\Entity\Empresa $noOfertas
     */
    public function removeNoOfertas(Empresa $noOfertas)
    {
        $this->noOfertas->removeElement($noOfertas);
    }

    /**
     * Get noOfertas.
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getNoOfertas()
    {
        return $this->noOfertas;
    }
}

每当我尝试更新 Usuario 时,我都会遇到此错误:

Neither the property "noOfertas" nor one of the methods "addNoOferta()"/"removeNoOferta()", "setNoOfertas()", "noOfertas()", "__set()" or "__call()" exist and have public access in class "Checkengine\DashboardBundle\Entity\Usuario".

这是updateAction()方法:

public function updateAction(Request $request, $id)
{
    $em = $this->getDoctrine()->getManager();
    $entity = $em->getRepository('DashboardBundle:Usuario')->find($id);

    if (!$entity) {
        throw $this->createNotFoundException('Unable to find Usuario entity.');
    }

    $deleteForm = $this->createDeleteForm($id);
    $editForm = $this->createEditForm($entity);

    $current_pass = $entity->getPassword();
    $editForm->handleRequest($request);

    if ($editForm->isValid()) {
        if (null == $entity->getPassword()) {
            $entity->setPassword($current_pass);
        } else {
            $this->setSecurePassword($entity);
        }

        $em->flush();

        return $this->redirect($this->generateUrl('usuarios_edit', array('id' => $id)));
    }

    return array(
        'entity'      => $entity,
        'form'        => $editForm->createView(),
        'delete_form' => $deleteForm->createView(),
    );
}

哪里出了问题?我缺少什么?我已经多次清除缓存并重新启动网络服务器(以防万一),但仍然没有任何问题,这让我发疯,有什么建议吗?线索?

如错误消息所示,您需要 add/remove 一个 $noOferta 对象而不是整个集合 $noOfertas

你需要像这样重写方法:

public function addNoOferta(Empresa $noOferta)
{
    $this->noOfertas[] = $noOferta;

    return $this;
}

public function removeNoOferta(Empresa $noOferta)
{
    $this->noOfertas->removeElement($noOferta);
}