SQL mapper->find() all records returned?

729 views
Skip to first unread message

Nuwanda

unread,
Aug 21, 2015, 2:48:07 AM8/21/15
to Fat-Free Framework
Here:

http://www.willis-owen.co.uk/2013/02/blog-tutorial-with-fat-free-framework-v3/

I'm seeing:

$article=new DB\SQL\Mapper($f3->get('DB'),'article');
$articles
=$article->find();
$f3
->set('articles',$articles)


article being the table containing article records.

Is the $article->find() returning ALL articles which are then being assigned by the set method to an articles hive variable, or is that simply a reference to the list of articles?

Given huge numbers of records that's a big assignment. Perhaps I'm missing something basic about active record. Only the first record is actually loaded and others referenced with iteration?

Anatol Buchholz

unread,
Aug 21, 2015, 5:28:37 AM8/21/15
to f3-fra...@googlegroups.com
Well, first I I would have said it´s just the reference. But If you check

 $article=new DB\SQL\Mapper($f3->get('DB'),'article');
 $articles
=$article->find();

 echo
'<pre>',print_r($articles,1);

You´ll see that you get everything from "article". So from my basic understanding of the Mapper (just learning Cortex)
It´s like this:

new DB\SQL\Mapper($f3->get('DB'),'article');
The Mapper now nows the underlaying structure of your table

$articles=$article->find();
You force the mapper to return all records as long as you do not narrow the result with a given criteria.

If you look at find method from /sql/mapper.php you´ll see that if you do not filter your results, the default limit is NULL

 $options+=array(
 
'group'=>NULL,
 
'order'=>NULL,
 
'limit'=>0,
 
'offset'=>0
 
);

Find method than return $this->select:

 $sql='SELECT '.$fields.' FROM '.$this->table;

Which queries your table.


If you use "load" method instead of find() only the first matched result seems to be returned. You then can navigate with next() prev() …

 $article=new DB\SQL\Mapper($f3->get('DB'),'article');

 $article
->load();
 echo $article
->text; // text of first article
 $article
->next();
 echo $article
->text;  // text of second article



But I´m sure other board member can tell you more …

cheers,

– anatol

xfra35

unread,
Aug 21, 2015, 5:51:19 AM8/21/15
to f3-fra...@googlegroups.com
$article->find() returns an array of Mapper objects:

$articles[0] contains a new DB\SQL\Mapper, mapped to the first record
$articles[1] contains a new DB\SQL\Mapper, mapped to the second record
etc...

In PHP, objects are always passed as references, so when you pass this array to the view, you pass an array of references to the newly created Mapper objects.

NB: when calling load(), the mechanism is similar but additionally, the current mapper is synchronized with the first record:
$article->load(); // call $article->find() behind the hood and map $article to $articles[0]
$article->next(); // map $article to $articles[1]

Message has been deleted

Nuwanda

unread,
Aug 21, 2015, 11:59:21 PM8/21/15
to Fat-Free Framework
Ok. So by assigning the mapper array I'm not passing around huge amounts of data? Or should the find operation always be limited?

Rayne

unread,
Aug 22, 2015, 5:56:00 AM8/22/15
to Fat-Free Framework
You should limit the amount of fetched rows, otherwise you will run out of memory some day. This is also true for Mapper->load(), etc.
Reply all
Reply to author
Forward
0 new messages