1
這是我第一次「findall」簡單查詢。目的是隨機選擇1條記錄,然後回顯「參考」(聖經箴言表中的一個字段)的值。我正在使用PHP的array_rand進行隨機選擇。我需要幫助的是如何從結果arrany_rand()數組中訪問'reference'的值。如何訪問Doctrine簡單查詢「findall」結果
$Bible_anagram_reference = NULL;
/**
* @var \AMDatabase\Entity\TheVerse\BibleAnagrams $result
*/
$result = $this->getDoctrineEntityManager()
->getRepository(get_class($this->getEntityBibleAnagrams()))
->findAll();
if (is_array($result) && count($result) > 0) {
/**
* @var \AMDatabase\Entity\TheVerse\BibleAnagrams $value
*/
$value = array_rand($result, 1);
echo "Bible Ananagram: " . $result[$value]['reference'] . "\r\n";
exit;
}
隨機選擇的記錄給出結果:
$result = Array
(
[0] => AMDatabase\Entity\TheVerse\BibleAnagrams Object
(
[reference:AMDatabase\Entity\TheVerse\BibleAnagrams:private] => 1
[languageIso:AMDatabase\Entity\TheVerse\BibleAnagrams:private] => en
[bibleAnagramWord:AMDatabase\Entity\TheVerse\BibleAnagrams:private] => Love
[word1:AMDatabase\Entity\TheVerse\BibleAnagrams:private] => Like
[word2:AMDatabase\Entity\TheVerse\BibleAnagrams:private] => Openness
[word3:AMDatabase\Entity\TheVerse\BibleAnagrams:private] => Venerate
[word4:AMDatabase\Entity\TheVerse\BibleAnagrams:private] => Enjoyment
)
);
當我使用
echo "Bible Ananagram: " . $result[$value]['reference'] . "\r\n";
我收到錯誤
PHP Fatal error: Cannot use object of type AMDatabase\Entity\TheVerse\BibleAnagrams as array
當我使用
echo "Bible Ananagram: " . $result[$value]->reference . "\r\n";
我收到錯誤
PHP Fatal error: Cannot access private property
如果有需要幫我請指教這是什麼意思的更多細節。非常感謝,Ron