我正在尝试获取集合中最后一个元素的属性。我试过了

end($collection)->getProperty()


$collection->last()->getProperty()

没有办法

(告诉我,我正在尝试在 bool 值上使用getProperty())。
/**
 * Get legs
 *
 * @return \Doctrine\Common\Collections\Collection
 */
public function getLegs()
{
    return $this->aLegs;
}

public function getLastlegdate()
{
    $legs = $this->aLegs;

    return $legs->last()->getStartDate();
}

知道为什么吗?

最佳答案

您有问题是由于集合为空。
在内部last()方法使用from the docend() php函数:



因此,如下更改代码:

$property = null

if (!$collection->isEmpty())
{
$property =  $collection->last()->getProperty();
}

希望这个帮助

关于php - 获取集合中的最后一个元素,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32506828/

10-10 14:10
查看更多