5

コレクションの最後の要素のプロパティを取得しようとしています。私は試した

end($collection)->getProperty()

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

どれも動作しません

(ブール値で使用しようとしていると教えてくれますgetProperty())。

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

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

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

理由はありますか?

4

2 に答える 2

13

コレクションが空であるため、問題が発生しています。内部的には、last()メソッドは doc からのend()php 関数を使用します:

最後の要素の値、または空の配列の場合は FALSE を返します。

したがって、コードを次のように変更します。

$property = null

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

この助けを願っています

于 2015-09-10T16:49:12.207 に答える