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

时间:2015-03-01 23:21:48

标签: php symfony caching doctrine-orm

我有一个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时,我都会收到此错误:

  

属性“noOfertas”也不是其中一种方法   “addNoOferta()”/“removeNoOferta()”,“setNoOfertas()”,“noOfertas()”,   “__set()”或“__call()”存在并且在课堂上具有公共访问权限   “CHECKENGINE \ DashboardBundle \实体\ 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(),
    );
}

那里有什么问题?我错过了什么?我已经清除缓存几次也重新启动网络服务器(以防万一)并没有任何问题仍然和它让我发疯,任何建议?线索?

1 个答案:

答案 0 :(得分:1)

如错误消息所示,您需要添加/删除$noOferta对象,而不是整个集合$noOfertas

你需要重写这样的方法:

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

    return $this;
}

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