We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

skipAttributes not working

I am using Phalcon 1.1.0 (from yesterday). I have title and body fields and have set in the model

public function initialize()
    {
            $this->skipAttributes(array('body'));
    }

and still when I try to create new only with title it says: "body is required". Am I doing something wrong? (by the way skipAttributesOnCreate is working)



98.9k

Are you using column renaming? I can't reproduce your problem, actually skipAttributes calls skipAttributesOnCreate internally



7.9k

No I am not using column renaming. And the other strange thing is that I have other text field which Phalcon doesn't think is required :) I couldn't find why it acts like that but the simple solution is not to have the fields as required by default as in forms fields - they are not required unless you set them as such.



98.9k

I'm running this gist https://gist.github.com/phalcon/5527000 here: https://test.phalcon.io/skip-attributes.php

As you can see in the log, both "type" and "year" are ignored in the INSERT:

SELECT CASE WHEN COUNT(*) > 0 THEN 1 ELSE 0 END FROM sqlite_master WHERE type='table' AND tbl_name='robots'
SELECT CASE WHEN COUNT(*) > 0 THEN 1 ELSE 0 END FROM sqlite_master WHERE type='table' AND tbl_name='robots'
PRAGMA table_info('robots')
INSERT INTO "robots" ("name", "id") VALUES (?, null)
SELECT "robots"."id", "robots"."name", "robots"."type", "robots"."year" FROM "robots"
SELECT COUNT(*) FROM (SELECT "robots"."id", "robots"."name", "robots"."type", "robots"."year" FROM "robots")

Also, you can see how the robots are printed using its default values in the table (because of their skipping in the insert list).

If you want to check the database by yourself, you can download it from here: https://test.phalcon.io/robots.db



7.9k

Finally I found where was the problem. In fact Phalcon was doing it right all the time and the problem was not about the code of the controller or the model but with the db table.

The table:

CREATE TABLE news ( news_id INT(10) NOT NULL AUTO_INCREMENT, title VARCHAR(255) NOT NULL DEFAULT '', body TEXT NOT NULL, image VARCHAR(255) NULL DEFAULT NULL, )

The problem: I wanted the "body" field not to be required and used skipAttributes() in the model but it wasn't working. On the other side I didn't get any errors about the requirement of the "image" field about which I didn't specified anything in the skipAttributes().

The solution: I just had to set "body" as DEFAULT NULL - that's all :)