php - How do I avoid using memory, or clear memory, when retrieving only the first item from an ArrayCollection? -


/** * @onetomany(targetentity="documentdata", mappedby="document") **/ private $records; 

i have above. using below retrieve first record.

function getfirstrecord() {   if (isset($this->records[0])) {     return $this->records[0]->getdata();   }   else {     return false;   } } 

it's using far memory (3mb 128mb after looping through 100 documents). expect because on line:

if (isset($this->records[0])) { 

the entire $records arraycollection loaded memory, need $records[0]. how can avoid or clear memory once have first record?

edit:

i have:

  /**   * @onetomany(targetentity="documentdata", mappedby="document", fetch="extra_lazy")   **/   private $records; 

and

  function getfirstrecord() {     $header;     if ($this->records->containskey(0)) {       $header = $this->records->get(0);       return $header->getdata();     }     else {       return false;     }   } 

unfortunately, both , containskey method still load entire arraycollection. able spot i'm doing wrong?

my final solution use raw sql query.

select column documentdata document_id = ". $docid ." limit 1 

i don't need specific record record. not manage expected output out of fetch="extra_lazy".


Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -