How to loop through mapper array and add new field to each record?

672 views
Skip to first unread message

John Doe

unread,
Jan 8, 2016, 5:37:47 PM1/8/16
to Fat-Free Framework
I have:

$subset = $this->cp->paginate($page-1, $limit, $filter, $option);
$this->cpList['cpList'] = $this->cp->paginate($page-1, $limit, $filter, $option);


How to run through this complex arrays and add one field to each record?
Something like:

foreach ($this->cpList['cpList'] as &$value) {
    $this->cpList['cpList']['SomeNewField'] = some_calculated_value;
}

J.D.


Anatol Buchholz

unread,
Jan 9, 2016, 3:22:37 PM1/9/16
to f3-fra...@googlegroups.com
Hi John Doe,

I´m unsure if I do correctly understand what you´re looking for. The mapper virtual field can be used to calculate new fields from
Here Unityprice and quantity would be fields in your table. After that you can use this virtual field for what ever you want.

Anyway it seems you are more likely looking for iterating over your results and add a new array element?
This would work as always in php. Find here a little example for you: http://hastebin.com/iqetigavom.php

Which you can reproduce easily (if you use composer) with:

$ git clone https://github.com/F3Community/fatfree-composer-app.git  
$ cd fatfree
-composer-app  
$ composer install  

$ composer
require ikkez/f3-schema-builder:dev-master  


and then visit

http://localhost/fatfree-composer-app/build
http
://localhost/fatfree-composer-app/fill
http
://localhost/fatfree-composer-app/showarray

You can use the new Array in your views.

Hope this helps,

– anatol

Anatol Buchholz

unread,
Jan 10, 2016, 8:27:29 AM1/10/16
to Fat-Free Framework
Hi John Doe,

I looked at your question again. I´ve overseen that you using pagination. Most likely sugar.
Most likely there is a cleaner way but it would work as in the previous ecample. You could
just fill another array and set the fields you need:

class blog{
 
function listAllArticles($f3){
 $f3
= \Base::instance();
 $products
= new DB\SQL\Mapper($f3->get('db'),'products');

 
// paginate
 $limit
= 5;
 $page
= \Pagination::findCurrentPage();
 $filter
= NULL;
 $option
= NULL;
 $subset
= $products->paginate($page-1, $limit, $filter, $option);


 
// some random strings …
 $length
= 10;
 $randomString
= substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, $length);


 
// fill a new Array with your "Virtual Fields"
 $arr
= array();
 $i
= 0;


 
foreach ($subset['subset'] as $obj) {
   $arr
[$i]['title']= $obj->title;
   $arr
[$i]['random']= $randomString;
  $i
++;
 
}


 
// render output
 $f3
->set('newArray',$arr);
 echo
\Template::instance()->render('output.html');
 
}
}


$f3
->route(array(
   
'GET /list',
   
'GET /list/@page'
   
), 'Blog->listAllArticles');

and in your template:

<F3:repeat group="{{ @newArray }}" value="{{ @article }}">
 
<h2>{{ @article.title }}</h2>
 
<h2>{{ @article.random }}</h2>
</F3:repeat>


cheers,

– anatol

Gosforth

unread,
Jan 10, 2016, 9:52:02 AM1/10/16
to f3-fra...@googlegroups.com
Thanks a lot Anatol for your posts.
With your last example I will receive array that will be useless for pagination (as far as I know it will not work). Moreover I do not want to create new set of fields. I just want to add one key and value to existing array keeping records I got from DB.

If I print what mapper gives me I have (expand text below to see all):

Array
(
   
[cpList] => Array
       
(
           
[subset] => DB\CortexCollection Object
               
(
                   
[relSets:protected] => Array
                       
(
                       
)


                   
[pointer:protected] => 0
                   
[changed:protected] =>
                   
[cid:protected] => cortex_collection_56926bffd8e96
                   
[storage:ArrayIterator:private] => Array
                       
(
                           
[0] => Models\Cp Object
                               
(
                                   
[table:protected] => ....
                                   
[db:protected] => DB\SQL Object
                                       
(
                                           
[uuid:protected] => 37ymo35cp94ws
                                           
[pdo:protected] => PDO Object
                                               
(
                                               
)


                                           
[dsn:protected] => mysql....
                                           
[engine:protected] => mysql
                                           
[dbname:protected] => ....
                                           
[trans:protected] =>
                                           
[rows:protected] => 5
                                           
[log:protected] =>  blah, blah, blah......
                                       
)


                                   
[fluid:protected] =>
                                   
[fieldConf:protected] =>
                                   
[ttl:protected] => 60
                                   
[rel_ttl:protected] => 0
                                   
[primary:protected] => id
                                   
[smartLoading:protected] => 1
                                   
[standardiseID:protected] => 1
                                   
[dbsType:protected] => sql
                                   
[fieldsCache:protected] => Array
                                       
(
                                       
)


                                   
[saveCsd:protected] => Array
                                       
(
                                       
)


                                   
[collection:protected] => DB\CortexCollection Object
 
*RECURSION*
                                   
[relFilter:protected] => Array
                                       
(
                                       
)


                                   
[hasCond:protected] =>
                                   
[whitelist:protected] =>
                                   
[relWhitelist:protected] =>
                                   
[grp_stack:protected] =>
                                   
[countFields:protected] => Array
                                       
(
                                       
)


                                   
[preBinds:protected] => Array
                                       
(
                                       
)


                                   
[vFields:protected] =>
                                   
[_ttl:protected] => 0
                                   
[mapper:protected] => DB\SQL\Mapper Object
                                       
(
                                           
[db:protected] => DB\SQL Object
                                               
(
                                                   
[uuid:protected] => 37ymo35cp94ws
                                                   
[pdo:protected] => PDO Object
                                                       
(
                                                       
)


                                                   
[dsn:protected] => mysql....
                                                   
[engine:protected] => mysql
                                                   
[dbname:protected] => ....
                                                   
[trans:protected] =>
                                                   
[rows:protected] => 5
                                                   
[log:protected] => blah, blah, blah......
                                               
)


                                           
[engine:protected] => mysql
                                           
[source:protected] => ...
                                           
[table:protected] => ....
                                           
[_id:protected] =>
                                           
[fields:protected] => Array
                                               
(
                                                   
[id] => Array
                                                       
(
                                                           
[type] => int(11)
                                                           
[pdo_type] => 1
                                                           
[default] => 0
                                                           
[nullable] =>
                                                           
[pkey] =>
                                                           
[value] => 2
                                                           
[changed] =>
                                                       
)


                                                   
[field1] => Array
                                                       
(
                                                           
[type] => varchar(2)
                                                           
[pdo_type] => 2
                                                           
[default] =>
                                                           
[nullable] =>
                                                           
[pkey] =>
                                                           
[value] => MNJ
                                                           
[changed] =>
                                                       
)


I need to iterate through cpList->subset->storage... whatever it is and add new key and value to fields array (imagine new field 'field2' with some calculated value). I'm getting from DB some source values and I process them in quite long function so I cannot do it in template. The question is how?

ikkez

unread,
Jan 10, 2016, 4:07:40 PM1/10/16
to Fat-Free Framework
you could convert that object to an array first:

foreach ($subset['subset'] as &$obj) {
   $obj = $obj->cast();
   $
obj['field2'] = $randomString;
   unset($
obj);

John Doe

unread,
Jan 13, 2016, 6:20:50 AM1/13/16
to Fat-Free Framework
Thank you very much Ikkez - this is what I needed. 
Thank you Anatol as well.

Reply all
Reply to author
Forward
0 new messages