Hi,

I have these entities below. There is a 1:m relationship between them.

"MetodoPago" is "PayingMethod", already saved in the database: "credit 
card", "cash", etc.  And Pedido is Order.

Now I want to to associate a paying method to an order, so I have this code:

    $pedido = new Pedido();
    $metodoPago = new MetodoPago();

    $pedido = new Pedido();
    $repositoryMetodoPago = $this->getDoctrine()->getRepository(
'ProjectBackendBundle:MetodoPago');
    $metodoPago = $repositoryMetodoPago->find(1);
    
    $pedido->setMetodoPago($metodoPago);

    $em = $this->getDoctrine()->getManager();
    $em->persist($pedido);
    $em->flush();



The problem with this code: in the same time the new order is saved, a new 
MetodoPago is also saved!! Im interested in that since I have already saved 
all the availablel paying methods.

My try: I have tried this setter: 

    public function setMetodoPago(\Project\BackendBundle\Entity\MetodoPago 
$metodoPago = null)
    {
        $this->metodoPago = $metodoPago->getId();

        return $this;
    }



but in this case when I try to run the code above, I get 

Warning: spl_object_hash() expects parameter 1 to be object, integer given 
in /vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php line 1389 
Sooo.. what should I do?? These are the entites:

class Pedido
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\ManyToOne(targetEntity="MetodoPago", cascade={"persist", "remove"})
     * @ORM\JoinColumn(name="metodo_pago_id", referencedColumnName="id")
     **/
    private $metodoPago;

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

    /**
     * Set metodoPago
     *
     * @param \Project\BackendBundle\Entity\MetodoPago $metodoPago
     * @return Pedido
     */
    public function setMetodoPago(\Project\BackendBundle\Entity\MetodoPago 
$metodoPago = null)
    {
      var_dump($metodoPago);
        $this->metodoPago = $metodoPago;

        return $this;
    }

    /**
     * Get metodoPago
     *
     * @return \Project\BackendBundle\Entity\MetodoPago 
     */
    public function getMetodoPago()
    {
        return $this->metodoPago;
    }
}




class MetodoPago
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="string", name="nombre")
     */
    protected $nombre;

    /**
     * @ORM\Column(type="float", name="precio")
     */
    protected $precio;


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

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

        return $this;
    }

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

    /**
     * Set precio
     *
     * @param float $precio
     * @return MetodoPago
     */
    public function setPrecio($precio)
    {
        $this->precio = $precio;

        return $this;
    }

    /**
     * Get precio
     *
     * @return float 
     */
    public function getPrecio()
    {
        return $this->precio;
    }
}



-- 
You received this message because you are subscribed to the Google Groups 
"doctrine-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/doctrine-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to