bcosca / fatfree

A powerful yet easy-to-use PHP micro-framework designed to help you build dynamic and robust Web applications - fast!

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Database mapper cast method

FrankOssie opened this issue · comments

I would like to see something like this in the cast method, now i'm overriding the cast method like this

public function cast($obj=NULL, $notAllowed=[]){
        if (!$obj)
            $obj=$this;
        return array_map(
            function($row) {
                return $row['value'];
            },
            array_filter(
                $obj->fields+$obj->adhoc,
                function ($key){
                    return strpos($key, 'password') !== false || !in_array($key, $notAllowed);
                },
                ARRAY_FILTER_USE_KEY
            )
        );
    }

@FrankOssie it's been requested that we ask for support via the Google group or Slack:

https://groups.google.com/forum/#!forum/f3-framework
https://fatfreeframework-slack.herokuapp.com/

But you can check out the onload() handler and see if that would give you the support you're looking for:
https://fatfreeframework.com/3.7/cursor#onload

Or you could create a view and only return the data you want visible and then use a mapper to pull the data from there.

I cannot join the slack channel, it says user_disabled. But maybe add oncast as triggers

Hi. @FrankOssie you can limit the fields to work on with the $fields parameter on the constructor. Does that fix your problem? https://fatfreeframework.com/3.7/sql-mapper#Instantiation

It helps, but it would be easier to change the context, the not allowed fields are most of the time less fields then the allowed fields.
Just use cast to limit the output data instead of the mapper, becasue most of the time you check things on the models.

You could create a db view w/ only the fields you want, and then point the mapper to read from the view.

I get it, but it would be a nice feature to filter the cast method, because it is used as an output method

commented

imho this is something you can achieve easily with a separate function, as this is not something the fw must provide.
After all it's just a wrapped call: $foo = DBUtils::filter($bar->cast(), ['password','secretfield']).
Also it is not possible to include the hardcoded filter of the 'password' field ;)

Ok i'll fix it with something like that, thanks for the help