Some code:
// Controller
public function editAction($id)
{
$id = (int) $id;
$number = Number::findById($id);
$form = new NumberForm($number);
if ($this->request->isPost()) {
$form->bind($_POST, $number);
if ($form->isValid()) {
$number->save();
$this->uploadImage($id);
$this->redirect('/newspaper/number/edit/' . $id);
}
} else {
$form->setEntity($number);
}
$this->view->setVar('form', $form);
$this->view->setVar('number', $number);
}
// NumberForm
public function initialize()
{
$pub = new \Phalcon\Forms\Element\Check('pub');
$pub->setLabel('Pub');
$this->add($pub);
}
class Number extends \Phalcon\Mvc\Collection // also tested with \Phalcon\Mvc\Model ... same trouble
{
public $pub = 0;
public function getPub()
{
return $this->pub;
}
public function setPub($pub)
{
$this->pub = $pub;
}
}
At version 1.1 was problem with checkbox ... it was solved.
But, not to the end.
What problem?
When I create a record with form, checkbox is checked, I get in my DB the "pub" value of this record = "" (string value).
When I create a record and checkbox is not checked, I get in my DB the "pub" value of this record = 0 (integer value).
Then ... I try edit my records, I call editAction ... and I recieve a CHECKED checkbox at 1 and 2 records.
I try resave records with different checkbox values ... but theirs values has not been changed.
// Mongo console
> db.number.find()
{ "_id" : 1, "pub" : "" }
{ "_id" : 2, "pub" : "0" }
To solve that problem I modified my Collection:
// Number Collection. Added beforeSave() method and modified getPub()
public function beforeSave()
{
if (is_int($this->pub)) {
$this->setPub(0);
} else {
$this->setPub(1);
}
}
public function getPub()
{
return (string) $this->pub;
}
public function setPub($pub)
{
$this->pub = $pub;
}
Then all begins work correctly.
Records in DB:
// Mongo console
> db.number.find()
{ "_id" : 1, "pub" : 0 } // good staff
{ "_id" : 2, "pub" : 1 } // good staff