数组集合:设置方法如何工作?

时间:2018-09-05 20:19:38

标签: doctrine arraycollection

这可能是一个愚蠢的问题,但我无法弄清楚...
我正在尝试为理论论坛建立一个基本的父母/子女实体。我已经定义了我的实体和关系ManyToOne。我在父级的侧面添加了一个带有数组集合的方法。我修改了addTopicarg()方法,以便按层次结构父级子级的顺序而不是按时间顺序排列一个主题的所有注释的数组。如果评论是对另一个评论的回应,则该评论应该紧接在其父项之后。然后,我只需要在视图中执行for即可显示所有评论。

在学说的官方文档中,设置了一种可以帮助我做到这一点的方法(键/索引,对象)。但是,每当我添加一个带有键/索引2的对象时,尽管我有6个元素,但最后都会添加它。

医生说

  

设置
  将集合中的元素设置为指定的键/索引。

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

我不知道键/索引是否只是识别添加的对象的一种方法,还是真的是对象的位置编号(如在普通数组中一样)。是否可以在数组集合中的其他两个对象之间的特定索引处添加对象?

如果有人可以帮助我,我会很高兴。我一直在努力弄清整个下午。

这是父实体:

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
}

这是子实体:

<?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
}

这是我重新定义的方法:

/**
 * 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;
}

0 个答案:

没有答案