Modify content records in Bolt CMS

206 Views Asked by At

I've added a 'product' content type to my Bolt installation. I want to track and update inventory when a purchase is made, so I added an integer field 'available'. I set up a test controller to modify the record. Everything appears to work, but the update never happens. What am I missing?

<?php
namespace Bundle\Site;
class CartController extends \Bolt\Controller\Base
{
    public function addRoutes(\Silex\ControllerCollection $c)
    {
        $c->match('/test-save', [$this,'testSave']);
        return $c;
    }
    public function testSave()
    {
        $result=false;
        $repo = $this->app['storage']->getRepository('products');
        $content = $repo->find(1);
        //Error log output confirms that this is the correct record
        error_log(get_class($this).'::'.__FUNCTION__.': '.json_encode($content));
        $content->set('available',15);
        $content->setDatechanged('now');
        $result=$repo->save($content); //returns 1
        return new \Symfony\Component\HttpFoundation\Response(json_encode($result), \Symfony\Component\HttpFoundation\Response::HTTP_OK);
    }
}
2

There are 2 best solutions below

0
Jon Hulka On

It turns out that my code works. I was checking the result by reloading the back-end edit form, which had apparently cached its data, so I didn't see the value changing.

The same thing happens if I load the edit form in two browser tabs and update one, then refresh the other, so it looks like this is the best I can do.

0
Chandraarnav On

I have faced a similar problem. Either the updated codes didn't show up or the result in the output is unchanged. Figured though this happened due to setting debug: false in the config.yml file. When debug is true your in bolt-data/app/ all cache files increases but this thing get sorted. In production it is suggested that to keep debug: false but this problem checks-in too, hence the way you are dealing with it comes out to be the easiest way out without changing the configuration much.