Phalcon model automatically verifies non-empty fields when inserting or updating phalcon model.
When using the insert and update functions of phalcon, because all fields in the database are set to not null, the phalcon model will automatically determine whether the fields need to be filled before being inserted or updated, therefore, empty fields cannot be saved.
There are two solutions to this problem:
1. Change the database field and change not null to NULL.
However, the database still needs to find the DBA, and for the sake of performance, DBA requirements generally do NOT have special circumstances, the field must be not null, so this solution is rejected.
2. Set the default value for fields that can be empty.
After thinking about the default values, I think the spaces are the most suitable. However, after the values are assigned with spaces, the space stored in the database will also be space. For example, some empty and = ''will be invalid, it may affect some business logic. Think about it and give up the solution.
In the end, there were a variety of searches on the Internet. There were too few phalcon materials and Baidu could not find them at all. In the end, I had to fight for google, and I finally found some clues for me, finally, find the real solution based on the clues. There are two types:
1. Set separate rules for fields that can be empty
public function skipValidation($skipers=[]) { foreach ($skipers as $skiper) { if (empty($this->$skiper)) { $this->$skiper = new \Phalcon\Db\RawValue('""'); } } }
When using:
public function beforeValidation(){ $this->skipValidation(['tag','source_url']);}
This method can solve the problem perfectly. It is troublesome to set each field that can be empty.
2. Disable phalcon to judge whether the field is empty
public function initialize(){ $this->setup( array('notNullValidations'=>false) ); }
This method directly closes the logic for determining whether the field is null at the underlying layer. This problem can be solved once and for all. The disadvantage is that you must make a proper judgment on required fields in the front and backend.