Hunter
Hunter

Reputation: 41

Unable to save into postgresql using symfony and doctrine with its association objects or table?

I have to save a ticket with ticket_attachments,ticket_comments and ticket_status_history. I will persist only ticket object but want to save attachments,comments and history. I am using doctrine and symfony and annotations.

I have created 3 array collections in ticket with mappedBy and cascade remove and persist. And used inversedBy in attachments, comments and history. But after persisting and flush only ticket is being saved but other objects i.e. attachments,comments and historys are not saving. But shows No Error.

Ticket.php

/**
     * @ORM\OneToMany(targetEntity="AppBundle\Entity\TicketAttachments", mappedBy="ticket", cascade={"persist", "remove"})
     */
    private $attachments;

    /**
     * @ORM\OneToMany(targetEntity="AppBundle\Entity\TicketComments", mappedBy="ticket", cascade={"persist", "remove"})
     */
    private $comments;

    /**
     * @ORM\OneToMany(targetEntity="AppBundle\Entity\TicketStatusHistory", mappedBy="ticket", cascade={"persist", "remove"})
     */
    private $historys;

    public function __construct()
    {
        $this->attachments = new ArrayCollection();
        $this->comments = new ArrayCollection();
        $this->historys = new ArrayCollection();
    }

History, Comments and Attachments.php

/**
     * @var \AppBundle\Entity\Ticket
     *
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Ticket", inversedBy="historys")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="ticket_id", referencedColumnName="id")
     * })
     */
    private $ticket;

All have it's setter and getter. Where is the problem?

Upvotes: 1

Views: 212

Answers (1)

Ollie in PGH
Ollie in PGH

Reputation: 2629

Are you setting them to each other? As in:

public function addToHistories($history) {
   $this->historys[] = $history;
   $history->setTicket($this);
}

They have to be associated with each other within the setters (or some other way) for cascade persist to work.

Upvotes: 0

Related Questions