0

我正在使用 Symfony2。

这是我的对象播放器:

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

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

    protected $name;

    protected $urlAvatar;

    /**
     * @ORM\Column(type="integer")
     */
    protected $coins;

    /**
     * @ORM\Column(type="integer")
     */
    protected $money;

    /**
     * @ORM\OneToMany(targetEntity="Mercenary", mappedBy="player")
     */
    protected $mercenaries;

    public function __construct()
    {
        $this->mercenaries = new ArrayCollection();
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set coins
     *
     * @param integer $coins
     */
    public function setCoins($coins)
    {
        $this->coins = $coins;
    }

    /**
     * Get coins
     *
     * @return integer 
     */
    public function getCoins()
    {
        return $this->coins;
    }

    /**
     * Set money
     *
     * @param integer $money
     */
    public function setMoney($money)
    {
        $this->money = $money;
    }

    /**
     * Get money
     *
     * @return integer 
     */
    public function getMoney()
    {
        return $this->money;
    }

    /**
     * Add mercenaries
     *
     * @param IFZ\TowerofDimensionsBundle\Entity\Mercenary $mercenaries
     */
    public function addMercenary(\IFZ\TowerofDimensionsBundle\Entity\Mercenary $mercenaries)
    {
        $this->mercenaries[] = $mercenaries;
    }

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

我想阅读雇佣兵的信息,但我找不到方法。它返回一个 Doctrine 集合,但我无法继续并从集合中获取所有项目。我不想过滤它们,我只想获取所有项目。感谢阅读。

4

1 回答 1

0

ArrayCollection您可以像使用数组一样迭代a:

foreach ($player->getMercenaries() as $mercenary) {
    // do whatever you want with $mercenary
}

或在树枝中:

{% for mercenary in player.mercenaries %}
    {# do whatever you want with mercenary #}
{% endfor %}

您还可以从集合中获取数组:

$mercenariesArray = $player->getMercenaries()->toArray();
于 2012-09-28T06:15:14.683 回答