ReynierPM
ReynierPM

Reputation: 18660

Property or method not defined in entity

I have a Usuario entity defined as follow:

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;
    }
}

Any time I try to update a Usuario I got this error:

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

This is the updateAction() method:

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(),
    );
}

What could be wrong there? What I'm missing? I have clear the cache several times also restart the webserver (just in case) and nothing issue still and it's driving me crazy, any advice? clue?

Upvotes: 0

Views: 176

Answers (1)

gp_sflover
gp_sflover

Reputation: 3500

As showed in the error message you need to add/remove a $noOferta object and not the entire collection $noOfertas.

You need to rewrite the methods like this:

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

    return $this;
}

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

Upvotes: 1

Related Questions