Home:ALL Converter>Symfony/Silex populate form with data from database

Symfony/Silex populate form with data from database

Ask Time:2015-03-06T21:48:20         Author:Vodokan

Json Formatter

I'm new to silex. Learning forms. I want to populate a form with data from database. I have this entity class

namespace NS\Entity\Admin;

    class Page
    {

    protected $id;
    protected $title;
    protected $keywords;
    protected $description;
    protected $content;

    public function getId()     
    {
        return $this->id;
    }

    public function setId($id)
    {
        $this->id = $id;
    }

    public function getTitle()
    {
        return $this->title;
    }

    public function setTitle($title)
    {
        $this->title = $title;
    }
    etc......

Then in my controller

$page = new Page();
$form = $app['form.factory']->create(new UserForm(), $page);
etc ........

When the form is rendered it's fields are empty (which is normal)

If I do

$paget->setTitle = 'Title'; 

before rendering the form then the field title in the form is populated with the word Title.

So my question is how to populate the form fields with data from database? I guess somehow the properties of Page class should be populated with the data from the database but have no idea how to do that.

Short example will be useful.

Author:Vodokan,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/28900169/symfony-silex-populate-form-with-data-from-database
yy