1

This is maybe a stupid question but I can't figure it out...
I am trying to build a basic parent/child entity for a forum in doctrine. I have defined my entities and the relationship ManyToOne. I added a method with an array collection on the side of the parent. I modified the addTopicarg() method so as to have an array of all the comments on one topic in the order of the hierarchy parent child instead of a chronological order. If a comment is a response to another comment, it should be right after his parent in the array. Then I just have to do a for in the view to display all the comments.

In the official doc of doctrine a method that could help me do that is set(key/indice, object). But each time I add an object with the key/indice 2 for instance, it is added at the end, although I have 6 elements.

The doc says

set
Sets an element in the collection at the specified key/index.

  $collection = new ArrayCollection();  
  $collection->set('name', 'jwage');  

I don't know if key/index is just a way to identify the object added or if it really is the place's number of the object, like in a normal array. Is there a way to add an object at a specific index, between two other objects for instance, in an Array Collection ?

I would be glad if someone could help me on that. I have been trying to figure it out the whole afternoon..

Here is the parent entity:

namespace Shaker\JRQBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Shaker\JRQBundle\Entity\Contribution;

/**
 * Topic
 *
 * @ORM\Table(name="topic")
 * @ORM\Entity(repositoryClass="Shaker\JRQBundle\Repository\TopicRepository")
 */
class Topic extends Contribution
{
  // Other attributes
    /**
    * @ORM\OneToMany(targetEntity="Shaker\JRQBundle\Entity\TopicArg", mappedBy="topic")
    */
    private $topicargs;

// Other methods

    /**
     * Add topicarg
     *
     * @param \Shaker\JRQBundle\Entity\TopicArg $topicarg
     *
     * @return Topic
     */
    public function addTopicarg(\Shaker\JRQBundle\Entity\TopicArg $topicarg, $topicargparent)
    {

        if ($topicargparent===null) {
            // if no parent then add it at the end of the array
            $this->topicargs[] = $topicarg;
        } else {
            // otherwise get indice parent and put it after

            $key=indexOf($topicargparent);
            $this->topicargs->set($key+1, $topicarg);
            // indexOf(mixed $element)
            //Gets the index/key of a given element. The comparison of two elements is strict, that means not only the value but also the type must match.

//         ArrayCollection  set(string|int $key, mixed $value) 
        }

        return $this;
    }

    /**
     * Remove topicarg
     *
     * @param \Shaker\JRQBundle\Entity\TopicArg $topicarg
     */
    public function removeTopicarg(\Shaker\JRQBundle\Entity\TopicArg $topicarg)
    {
        $this->topicargs->removeElement($topicarg);

    /**
     * Get topicargs
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getTopicargs()
    {
        return $this->topicargs;
    }
// Other methods
}

And here is the child entity:

<?php

namespace Shaker\JRQBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Shaker\JRQBundle\Entity\Argumentation;

/**
 * TopicArg
 *
 * @ORM\Table(name="topic_arg")
 * @ORM\Entity(repositoryClass="Shaker\JRQBundle\Repository\TopicArgRepository")
 */
class TopicArg extends Argumentation
{
// other attributes

    /**
    * @ORM\ManyToOne(targetEntity="Shaker\JRQBundle\Entity\Topic", inversedBy="topicargs")
    */
    protected $topic;

//other methods

    /**
     * Set topic
     *
     * @param \Shaker\JRQBundle\Entity\Topic $topic
     *
     * @return TopicArg
     */
    public function setTopic(\Shaker\JRQBundle\Entity\Topic $topic = null)
    {
        $this->topic = $topic;
        return $this;
    }

    /**
     * Get topic
     *
     * @return \Shaker\JRQBundle\Entity\Topic
     */
    public function getTopic()
    {
        return $this->topic;
    }
// Other methods
}

And this is the method I redefined:

/**
 * Add topicarg
 *
 * @param \Shaker\JRQBundle\Entity\TopicArg $topicarg
 *
 * @return Topic
 */
public function addTopicarg(\Shaker\JRQBundle\Entity\TopicArg $topicarg, $topicargparent)
{
    if ($topicargparent===null) {
        // if no parent then add it at the end of the array
        $this->topicargs[] = $topicarg;
    } else {
        // otherwise get indice parent and put it after
        $key=indexOf($topicargparent);
        $this->topicargs->set($key+1, $topicarg);
    }

    return $this;
}
Shaker81
  • 47
  • 1
  • 12

0 Answers0