When I try to save a NULL value on postgresql, it does not save;
$model->getMessages() "return field1,field2,field3.... is required
I've get model from database and try to save it but got required errors on NULL values.
It's similar to issue https://github.com/phalcon/cphalcon/issues/14722
I will try to help you, but you can tell me your postgres, phalcon and php version, I'm not having this issue. and if possible some code example and the table.
Thanks
@Gamblt Can you give more specific example with code?
I confirm on Postgresql 12
PHP: 7.4
Phalcon (build from source):
Version => 4.0.4
Build Date => Feb 16 2020 16:40:11
Powered by Zephir => Version 0.12.16-$Id$
Model:
class User extends \Phalcon\Mvc\Model
{
public ?int $id = null;
public ?string $login = null;
public ?string $password = null;
public ?string $updated_at = null;
public function initialize()
{
$this->setSource('users');
$this->useDynamicUpdate(true);
}
}
Schema
CREATE TABLE "users" (
"id" SERIAL PRIMARY KEY,
"login" CHARACTER VARYING(255) NOT NULL,
"password" CHARACTER VARYING(255) NULL,
"updated_at" timestamp NULL
);
To reproduce:
$user = new User();
$user->login = 'kek';
$user->save();
foreach ($user->getMessages() as $message) {
echo $message->getMessage(); // password is required, updated_at is required
}
Yes. It's on Phalcon 4.0.4 from binary, PHP7.4 and Postgresql12.1
The problem is in the Adapter/Pdo/Postgresql class.
On line 490 we have
if field [5] == "NO" {
let definition["notNull"] = true;
}
and that is correct
Then it feeds the "Db/Column" class but by default notNull is already at "true" (since commit of February 03: 4cb58878b23e14016a63fe7373de44cde463499c)
line 258: protected notNull = true;
If we replace Adapter/Pdo/Postgresql with
if field [5] == "YES" {
let definition["notNull"] = false;
}
that fixes the problem
The fix already exists for Mysql: https://github.com/phalcon/cphalcon/commit/8038065559bc0dca85ce165622143adba04c0ac8
Same for Sqlite https://github.com/phalcon/cphalcon/commit/31a6fd018bdda6b18a0dbe5f3797e70bcc4833f8
Fixed in #14876
Most helpful comment
The problem is in the Adapter/Pdo/Postgresql class.
On line 490 we have
and that is correct
Then it feeds the "Db/Column" class but by default notNull is already at "true" (since commit of February 03: 4cb58878b23e14016a63fe7373de44cde463499c)
line 258: protected notNull = true;If we replace Adapter/Pdo/Postgresql with
that fixes the problem
The fix already exists for Mysql: https://github.com/phalcon/cphalcon/commit/8038065559bc0dca85ce165622143adba04c0ac8
Same for Sqlite https://github.com/phalcon/cphalcon/commit/31a6fd018bdda6b18a0dbe5f3797e70bcc4833f8