Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem that phalcon model automatically validates non-empty fields when inserting or updating?

2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "how to solve the problem that phalcon model will automatically verify non-empty fields when inserting or updating". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

When using the insert and update functions of phalcon, because all the fields in the database are set to NOT NULL, and the model of phalcon automatically determines whether the fields need to be required before inserting or updating them, it cannot be saved when there are empty fields.

When you start to encounter this problem, you can think of two solutions:

First, change the database field so that NOT NULL can be empty.

But the database still has to find DBA, and for performance, DBA requires that there is no special case, and the field must be NOT NULL, so the scheme is rejected.

Second, set the default value for fields that can be empty.

After thinking about various default values, I think spaces are the most suitable, but after assigning spaces, spaces will also be stored in the database, such as some empty and =''judgments will be invalid, which is likely to affect some business logic. Think about it or abandon the solution.

Finally, there are all kinds of searches on the Internet, the information of phalcon is too little, Baidu can not find it at all, and finally switch to google, and finally find some clues for me, and finally find out the real solution according to the clues. There are also two, as follows:

1. Set rules separately 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 in use:

Public function beforeValidation () {$this- > skipValidation (['tag','source_url']);}

This method can solve the problem perfectly, but the trouble is that you need to set each field that can be empty.

2. Disable phalcon's judgment on whether the field is empty or not

Public function initialize () {$this- > setup (array ('notNullValidations'= > false));}

This method directly closes the underlying logic to determine whether the field is empty, and can solve this problem once and for all, but the disadvantage is that the front and rear have to make a good judgment of the required fields.

This is the end of the content of "how to solve the problem that phalcon model automatically validates non-empty fields when inserting or updating". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report