We build. You grow.

Get best community software here

Start a social network, a fan-site, an education project with oxwall - free opensource community software

Proper/Safer DB Updates through OW PDO Models | Forum

AppXprt
AppXprt Sep 22 '18
I was able to find information on selecting and saving data through the OW ByExample Methods, but it wasn't exactly obvious how to update single or multiple rows through these methods.


I've also seen custom queries in plugins that could be prevented through fully utilizing the DB models. 


Didn't really know where else to put this, but wanted to share for other devs, a proper way for simple updates. This doesn't entirely eliminate the need for specific queries in all cases, but can dramatically simplify some DB updates while being quicker and safer by utilizing the DB models and PDO that OW utilizes:
 
    public function update(CLASS_BOL_Name $data )

    {
        $ex->andFieldEqual('column-index', $search);

        $dto = $this->findObjectByExample($ex);
        if ( empty($dto) )

        {

            return;

       }
        $dto->column=$update;

        $dto->column=$object->value;

        $this->save($dto);

        return $dto->id;

    }


Working Example:

    public function updateSubscription(OXPWAPRO_BOL_Subscriptions $subscription )

    {

        $ex = new OW_Example();

        $owuserid = OW::getUser()->getId();


        $ex->andFieldEqual('owuserid', $owuserid);

        $dto = $this->findObjectByExample($ex);


        if ( empty($dto) )

        {

            return;

        }


        $dto->owuserid=$owuserid;

        $dto->endpoint=$subscription->endpoint;

        $this->save($dto);

        return $dto->id;

    }

The Forum post is edited by AppXprt Sep 22 '18