Scaffolding

Behind the CRUD

Scaffolding is used by our CRUD controllers. Thanks to this additional layer of abstraction you can switch between different databases adapter without changing your controller actions or attached events. Scaffolding should be registered as DI service, therefore it can be used outside the CRUD.

In the following example we will use scaffolding with event manager to extend basic CRUD functionality:

namespace Foo\Controllers\Foo;

use Phalcon\Forms\Element\Text;
use Vegas\Mvc\Controller\Crud\Events;
use Vegas\Mvc\Controller\CrudAbstract;

class FooController extends CrudAbstract
{
    protected $formName = 'Foo\Forms\Foo';
    protected $modelName = 'Foo\Models\Foo';

    protected function afterCreate()
    {
        parent::afterCreate();

        $this->changeFakeValue();
        $this->redirect();
    }

    protected function afterEdit()
    {
        parent::afterEdit();

        $this->addExtraField();
    }

    // ...

    private function changeFakeValue()
    {
        $record = $this->scaffolding->getRecord();
        $record->fake = 'Fixed value';
        $record->save();
    }

    private function addExtraField()
    {
        $form = $this->scaffolding->getForm($this->scaffolding->getRecord());

        $field = new Text('extra_field');
        $field->setLabel('Extra field');

        $form->add($field);
        $this->scaffolding->setForm($form);
    }

    public function redirect(){
        $this->response->redirect(array(
            'for' => 'foo/crud',
            'action' => 'index',
        ));
    }
}