Doubts in the creation of the relationship

Asked

Viewed 187 times

1

Personal I have 2 classes, a call Venda and another ItensVenda. I need to create a relationship to:

  1. when I open a sale can see all the items of this sale.

  2. when I access an item of a sale I can see the Sale data.

How I Create These Relationships in the Doctrine?

Class Venda: id, nome, data

Class ItensVenda: id, nome, valor

Thank you.

  • managed to create the relationship?

1 answer

0


Make a relationship OneToMany.

Class Venda:

/**
 * @ORM\Entity
 * @ORM\Table(name="Venda")
 */
class Venda
{
    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue
     */
    protected $id;

    /**
     * @ORM\OneToMany(targetEntity="ItensVenda", mappedBy="venda", cascade={"remove"})
     */
    protected $itensVenda;

    /** demais atributos**/
}

Class ItensVenda:

/**
 * @ORM\Entity
 * @ORM\Table(name="ItensVenda")
 */
class ItensVenda
{
    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue
     */
    protected $id;

    /**
     * @ORM\ManyToOne(targetEntity="Venda", inversedBy="itensVenda")
     * @ORM\JoinColumn(name="venda_id", referencedColumnName="id", nullable=FALSE)
     */
    protected $venda;

    /** demais atributos**/
}

Source

  • opa gave right, but take me some small doubts. First the Cascade={"remove"} for what it serves and has other parameters? inversedBy what is the purpose? Thank you

  • Cascade indicates that when a sale is removed, itensVenda will be removed as well. Further information in the Documentation http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/basic-mapping.html

  • can mark as correct the answer?

  • understood and as for inversedBy for what it serves?

  • basically it represents the field in which it will be presented by the other class, so the name inversed . this link has a great documentation about relationship. http://doctrine-orm.readthedocs.org/en/latest/reference/unitofwork-associations.html

  • Thank you for your help.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.