Phalcon model automatically verifies non-empty fields when inserting or updating phalcon model.

Source: Internet
Author: User

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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.