I have a problem with update records in database. Here are errors:
Phalcon\Mvc\Model\Message Object
(
[_type:protected] => PresenceOf
[_message:protected] => field1 is required
[_field:protected] => private
[_model:protected] =>
[_code:protected] => 0
)
Phalcon\Mvc\Model\Message Object
(
[_type:protected] => PresenceOf
[_message:protected] => field2 is required
[_field:protected] => online
[_model:protected] =>
[_code:protected] => 0
)
This is the database schema:
CREATE TABLE `data`
(
`id` BINARY(16) NOT NULL,
`id_second` BINARY(16) NOT NULL,
`field1` TINYINT(1) NOT NULL DEFAULT 1,
`field2` TINYINT(1) NOT NULL DEFAULT 1,
`field3` INTEGER NOT NULL DEFAULT 0,
`name` VARCHAR(255) NOT NULL DEFAULT "",
PRIMARY KEY (`id`, `id_second`),
FOREIGN KEY (`id_second`) REFERENCES `data2` (`id`)
);
I can create new record, but I can't update it. After record find I'm trying to replace values and change it:
$record->name = $save['name'];
$record->field1 = $save['field1'];
$record->field2 = $save['field2'];
$record->update();
But this displays errors given above. I don't know why, becouse when I display $record->field1
and $record->field2
they are not null but true or false (that's why it's TINYINT
in database).
There are no validation rules in model and id and id_second fields are setted propertly.
I was trying to do this in this way:
$result = Di::getDefault()->getModelsManager()->executeQuery('
UPDATE \Pulsar\Model\Data SET
name = :name:,
field1 = :field1:,
field2 = :field2:
WHERE
id = :id: AND id_second = :id_d2:
', [
'id' => $this->id,
'id_d2' => $this->id_second,
'name' => $this->name,
'field1' => $this->field1,
'field2' => $this->field2
], [
'id' => \PDO::PARAM_STR,
'id_d2' => \PDO::PARAM_STR,
'name' => \PDO::PARAM_STR,
'field1' => \PDO::PARAM_INT,
'field2' => \PDO::PARAM_INT
]
);
But result is the same. However, this works:
$v1 = $record->field1 ? 1 : 0;
$v2 = $record->field2 ? 1 : 0;
$record->getReadConnection()->execute("
UPDATE data SET
name = '{$record->name}',
field1 = {$v1},
field2 = {$v2}
WHERE
id = '{$record->id}' AND id_second = '{$record->id_second}'
");
How can I achieve the same with update()
or save()
function? I remind that create()
and delete()
functions are working correctly.
Ok... I see why it's not working.
I'm posting true
/ false
values, but it requires integer values.
So, passing 0
/ 1
instead of true
/ false
works correctly.