Jump to content

Recommended design pattern for updating name/values


NotionCommotion

Recommended Posts

I have a bunch of sub classes which extend a base class.  In the base class, I was planning on including a method updateValue($name, $value, $primaryKey) which would update the column specified by $name with the value $value for the primary key $primaryKey.

 

Some column names are allowed to be changed for all sub classes, yet others are only allowed to be changed for some of the sub classes.  Also, some column names might have unique requirements for all of the sub classes or even just some of them.  I will definitely need to whitelist them.

 

Any recommended design patterns to implement this?

Link to comment
Share on other sites

Off the wall suggestion, since I'm not a class-y kind of guy.

 

How about using the get_class() function to get the current object's class name and use that as a key to a 2D array of allowable column names? If the column name is in the array under that classname key, it's a go.

Link to comment
Share on other sites

Thanks ginerjm,

 

This is what I ended up doing.  See any concerns?

    public function updateParam($params)
    {
        $rs=$this->sanitize($params,['name','value']);
        if($rs->error) {
            $rsp=\MyApp\ErrorResponse::missingValue($rs->error);
        }
        elseif ($method=$this->getUpdateMethod($rs->data['name'])) {
            try {
                $sql="UPDATE $method[table] SET $method[column]=? WHERE id=? AND accounts_id=?";
                $stmt = $this->pdo->prepare($sql);
                $stmt->execute([$rs->data['value'],$this->id,$this->account->id]);
                $rsp=[null,204];
            }
            catch(\PDOException $e){
                switch($e->getCode()) {
                    case 23000:
                        $rsp=\MyApp\ErrorResponse::duplicatedName();
                        break;
                    default:
                        $rsp=\MyApp\ErrorResponse::customError($e->getMessage());
                }
            }
        }
        else {
            $rsp=\MyApp\ErrorResponse::customError('Invalid column '.$rs->data['name']);
        }
        return $rsp;
    }
    protected function getUpdateMethod($name)
    {
        return [
            'name'=>['column'=>'name','table'=>'items'],
            'price'=>['column'=>'unit_price','table'=>'items'],
            'code'=>['column'=>'code','table'=>'codes'],
            ][$name];
    }
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.