K
K
Karina2015-09-23 19:19:53
symfony
Karina, 2015-09-23 19:19:53

How to display results from linked table?

I have a table:

/**
 * Fcsnotificationea
 *
 * @ORM\Table(name="fcsNotificationEA")
 * @ORM\Entity
 */
class Fcsnotificationea
{
...
    /**
     * @ORM\OneToMany(targetEntity="Attachmentstoea", mappedBy="Fcsnotificationea")
     **/
    private $attachments;
...
/**
     * Constructor
     */
    public function __construct()
    {
        $this->attachments = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * Add attachment
     *
     * @param \Custom\ScanFilesBundle\Entity\Attachmentstoea $attachment
     *
     * @return Fcsnotificationea
     */
    public function addAttachment(\Custom\ScanFilesBundle\Entity\Attachmentstoea $attachment)
    {
        $this->attachments[] = $attachment;

        return $this;
    }

    /**
     * Remove attachment
     *
     * @param \Custom\ScanFilesBundle\Entity\Attachmentstoea $attachment
     */
    public function removeAttachment(\Custom\ScanFilesBundle\Entity\Attachmentstoea $attachment)
    {
        $this->attachments->removeElement($attachment);
    }

    /**
     * Get attachments
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getAttachments()
    {
        return $this->attachments;
    }

and there is another one:
/**
 * Attachmentstoea
 *
 * @ORM\Table(name="attachmentsToEA", indexes={@ORM\Index(name="notificationEAid", columns={"notificationEAid", "id"}), @ORM\Index(name="IDX_5637D788CEB3C986", columns={"notificationEAid"})})
 * @ORM\Entity
 */
class Attachmentstoea
{
...
    /**
     * @var \Fcsnotificationea
     *
     * @ORM\ManyToOne(targetEntity="Fcsnotificationea")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="notificationEAid", referencedColumnName="id")
     * })
     */
    private $notificationeaid;
...

I have generated methods like
/**
     * Add attachment
     *
     * @param \Custom\ScanFilesBundle\Entity\Attachmentstoea $attachment
     *
     * @return Fcsnotificationea
     */
    public function addAttachment(\Custom\ScanFilesBundle\Entity\Attachmentstoea $attachment)
    {
        $this->attachments[] = $attachment;

        return $this;
    }

first I make setters for the Fcsnotificationea object. I do persist for the Fcsnotificationea object. But I don't flush.
Then I make setters for the Attachmentstoea object. And only then do I call the setter for the Fcsnotificationea object:
$eaNotification->addAttachment($eaAttachments);
and only then do I flush.
In general, the base is filled normally.
but I need to display information in twig and I can't call the getAttachments method for Fcsnotificationea. Although entity has this method:
/**
     * Get attachments
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getAttachments()
    {
        return $this->attachments;
    }

I try to call this method in twig
{% for notification in notificationsEA %}
  {% for attach in notification.getAttachments %}

they write to me: Undefined index: Fcsnotificationea, where the line {% for attach in notification.getAttachments %}
tried in different ways to select the result with and without join - to no avail. I can't reach attachemts in any way:
I make a request in general like this:
$twig_params["notificationsEA"] = $em->createQueryBuilder()
                                           ->select('n')
                                           ->from('CustomScanFilesBundle:Fcsnotificationea', 'n')
                                           ->setFirstResult($page)
                                           ->setMaxResults(10)
                                           ->join('CustomScanFilesBundle:Attachmentstoea', 'a',
                                           \Doctrine\ORM\Query\Expr\Join::WITH, 'n.id = a.notificationeaid')
                                           ->getQuery()
                                           ->getResult();

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis, 2015-09-23
@iKapex

class Fcsnotificationea
{
    /**
     * @ORM\OneToMany(targetEntity="Attachmentstoea", mappedBy="Fcsnotificationea")
     **/
    private $attachments;

mappedBy="Fcsnotificationea"
Is Fcsnotificationea a field in Attachmentstoea? Where?
See doc...
doctrine-orm.readthedocs.org/en/latest/reference/a...
Check
app/console mapping doctrine:mapping:info

S
Sergey, 2015-09-23
Protko @Fesor

I have a table:

Not a table, but an entity. Doctrine professes the principle of persistence ignorance.
Why is it so difficult? We set up a cascading persistence for communication and our code is simplified to:
$notification = new FcsNotificationEA();
// добавляем 3 аттачмента
$notification->add(new Attachment());
$notification->add(new Attachment());
$notification->add(new Attachment());

$this->notificationsRepository->add($notification); // внутри add происходит persist

$this->get('doctrine.orm.entity_manager')->flush(); // коммитим изменения в базу

return $this->render('some_template.twig', compact('notification'));

ps Still, think about how to properly name objects ... because it's not clear what it is.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question