bcosca / fatfree

A powerful yet easy-to-use PHP micro-framework designed to help you build dynamic and robust Web applications - fast!
2.66k stars 446 forks source link

Database mapper cast method #1195

Closed FrankOssie closed 4 years ago

FrankOssie commented 4 years ago

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
            )
        );
    }
geniuswebtools commented 4 years ago

@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.

FrankOssie commented 4 years ago

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

ikkez commented 4 years ago

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

FrankOssie commented 4 years ago

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.

geniuswebtools commented 4 years ago

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

FrankOssie commented 4 years ago

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

KOTRET commented 4 years ago

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 ;)

FrankOssie commented 4 years ago

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