phalcon model在插入或更新时自动验证非空字段怎么解决
在使用phalcon的insert和update功能时,因为数据库所有的字段设置的都是NOT NULL,而phalcon的model在插入或更新之前会自动判断字段是否需要必填,因此导致有空字段时无法存入。
开始遇到这问题时,想到两种解决方法:
一、改数据库字段,把NOT NULL改为可以为空。
但该数据库还得去找DBA,而且为了性能,DBA要求一般没有特殊情况,字段必须是NOT NULL,所以该方案否决。
二、给可以为空的字段设置默认值。
想过各种默认值,觉得空格最符合,但是赋值空格后,数据库里存的也会是空格,像一些empty和==''等判断会失效,很可能会影响一些业务逻辑,想想,还是放弃该方案。
最后还是上网各种搜,phalcon的资料太少,百度根本搜不出来,最后转战google,功夫不负有心人,终于给我找到些蛛丝马迹,最后再根据蛛丝马迹找出来真正的解决方案。同样有两种,如下:
一、给可以为空的字段单独设置规则
代码如下 | 复制代码 |
publicfunctionskipValidation($skipers=[])
{
foreach($skipersas$skiper) {
if(empty($this->$skiper)) {
$this->$skiper=new\Phalcon\Db\RawValue('""');
}
}
}
|
使用的时候:
代码如下 | 复制代码 |
publicfunctionbeforeValidation()
{
$this->skipValidation(['tag','source_url']);
}
|
这种方法可以完美解决问题,比较麻烦的是,需要设置每个可以为空的字段。
二、关闭phalcon对字段是否为空的判断
代码如下 | 复制代码 |
publicfunctioninitialize(){
$this->setup(
array('notNullValidations'=>false)
);
}
|
该方法直接把底层判断字段是否为空的逻辑关闭了,可以一劳永逸的解决这个问题,缺点就是,自己前后台得做好必填字段的判断。
这篇文章介绍了php的正则修正符,非常具有学习价值,有兴趣的同学可以参考一下本文实例讲述了php正则修正符用法。分享给大家供大家参考,具体如下:
代码如下 | 复制代码 |
<?php
//标记在整个模式之外;
// 例://$mode="/\bis\b/U",其中U在外面;
//修正符:i 不区分大小写的匹配;
//如:"/abc/i"可以与abc或aBC或ABc等匹配;
//修正符:m 将字符串视为多行,不管是那行都能匹配;
// 例://模式为:$mode="/abc/m";
//要匹配的字符串为:$str="bcefg5e\nabcdfe"
//注意其中\n,换行了;abc换到了下一行;
//$str和$mode仍可以匹配,修正符m使得多行也可匹配;
//修正符:s 将字符串视为单行,换行符作为普通字符;
// 例://模式为:$mode="/pr.y/";
//要匹配字符串为:$str="pr\ny";
//两者不可匹配; . 是除了换行以外的字符可匹配;
//修改下模式为:$mode="/pr.y/s";
//其中修正符s将\n视为普通字符,即不是换行;
//最后两者可以匹配;
//修正符:x 将模式中的空白忽略;
//修正符:A 强制从目标字符串开头匹配;
// 例://$mode="/abc/A";
//可以与$str="abcsdfi"匹配,
//不可以与$str2="sdsdabc"匹配;
//因为$str2不是以abc开头;
//修正符:D 如果使用$限制结尾字符,则不允许结尾有换行;
// 例://模式为:$mode="/abc$/";
//可以与最后有换行的$str="adshabc\n"匹配;
//元子符$会忽略最后的换行\n;
//如果模式为:$mode="/abc/D",
//则不能与$str="adshabc\n"匹配,
//修正符D限制其不可有换行;必需以abc结尾;
//修正符:U 只匹配最近的一个字符串;不重复匹配;
// 例:
// 如模式为:
$mode="/a.*c/";
$str="abcabbbcabbbbbc";
preg_match($mode,$str,$content);
echo$content[0];//输出:abcabbbcabbbbbc;
//如果$mode="/a.*c/";变成$mode="/a.*c/U";
// 则只匹配最近一个字符串,输出:abc;
//修正符:e 配合函数preg_replace()使用,
// 可以把匹配来的字符串当作正则表达式执行;
?>
|
修正符:
POSIX兼容正则没有修正符。
PERL兼容正则中可能使用的修正符(修正符中的空格和换行被忽略,其它字符会导致错误):
i(PCRE_CASELESS):
匹配时忽略大小写。
m(PCRE_MULTILINE):
当 设定了此修正符,行起始(^)和行结束($)除了匹配整个字符串开头和结束外,还分别匹配其中的换行符(\n)的之后和之前。
s(PCRE_DOTALL):
如 果设定了此修正符,模式中的圆点元字符(.)匹配所有的字符,包括换行符。没有此设定的话,则不包括换行符。
x(PCRE_EXTENDED):
如 果设定了此修正符,模式中的空白字符除了被转义的或在字符类中的以外完全被忽略。
e:
如果设 定了此修正符,preg_replace() 在替换字符串中对逆向引用作正常的替换,将其作为 PHP 代码求值,并用其结果来替换所搜索的字符串。 只有 preg_replace() 使用此修正符,其它 PCRE 函数将忽略之。
A(PCRE_ANCHORED):
如 果设定了此修正符,模式被强制为“anchored”,即强制仅从目标字符串的开头开始匹配。
D(PCRE_DOLLAR_ENDONLY):
如 果设定了此修正符,模式中的行结束($)仅匹配目标字符串的结尾。没有此选项时,如果最后一个字符是换行符的话,也会被匹配在里面。如果设定了 m 修正符则忽略此选项。
S:
当一个模式将被使用若干次时,为加速匹配起见值得先对其进行分析。 如果设定了此修正符则会进行额外的分析。目前,分析一个模式仅对没有单一固定起始字符的 non-anchored 模式有用。
U(PCRE_UNGREEDY):
使 “?”的默认匹配成为贪婪状态的。
X(PCRE_EXTRA):
模式中的任何反斜线后面跟上 一个没有特殊意义的字母导致一个错误,从而保留此组合以备将来扩充。默认情况下,一个反斜线后面跟一个没有特殊意义的字母被当成该字母本身。
u(PCRE_UTF8):
模 式字符串被当成UTF-8。
注意:
模式修正符(Pattern Modifiers)
i -可同时匹配大小写字母
M -将字符串视为多行
S -将字符串视为单行,换行符做普通字符看待,使“.”匹配任何字符
X -模式中的空白忽略不计
U -匹配到最近的字符串
e -将替换的字符串作为表达使用
本文实例讲述了PHP实现的文件操作类及文件下载功能。分享给大家供大家参考,具体如下:
文件操作类:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
<?php
// Copyright 2005, Lee Babin (lee@thecodeshoppe.com)
// This code may be used and redistributed without charge
// under the terms of the GNU General Public
// License version 2.0 or later -- www.gnu.org
// Subject to the retention of this copyright
// and GPL Notice in all copies or derived works
classcfile {
//The path to the file we wish to work with.
protected$thepath;
//Error messages in the form of constants for ease of use.
constFOUNDERROR ="Sorry, the file in question does not exist.";
constPERMERROR ="Sorry, you do not have the proper permissions on this file";
constOPENERROR ="Sorry, the file in question could not be opened.";
constCLOSEERROR ="Sorry, the file could not be closed.";
//The constructor function.
publicfunction__construct (){
$num_args= func_num_args();
if($num_args> 0){
$args= func_get_args();
$this->thepath =$args[0];
}
}
//A function to open the file.
privatefunctionopenfile ($readorwrite){
//First, ensure the file exists.
try{
if(file_exists($this->thepath)){
//Now, we need to see if we are reading or writing or both.
$proceed= false;
if($readorwrite=="r"){
if(is_readable($this->thepath)){
$proceed= true;
}
}elseif($readorwrite=="w"){
if(is_writable($this->thepath)){
$proceed= true;
}
}else{
if(is_readable($this->thepath) &&is_writable($this->thepath)){
$proceed= true;
}
}
try{
if($proceed){
//We can now attempt to open the file.
try{
if($filepointer=fopen($this->thepath,$readorwrite)){
return$filepointer;
}else{
thrownewexception (self::OPENERROR);
returnfalse;
}
}catch(exception$e) {
echo$e->getmessage();
}
}else{
thrownewexception (self::PERMERROR);
}
}catch(exception$e) {
echo$e->getmessage();
}
}else{
thrownewexception (self::FOUNDERROR);
}
}catch(exception$e) {
echo$e->getmessage();
}
}
//A function to close a file.
functionclosefile () {
try{
if(!fclose ($this->thepath)){
thrownewexception (self::CLOSEERROR);
}
}catch(exception$e) {
echo$e->getmessage();
}
}
//A function to read a file, then return the results of the read in a string.
publicfunctionread () {
//First, attempt to open the file.
$filepointer=$this->openfile ("r");
//Now, return a string with the read data.
if($filepointer!= false){
//Then we can read the file.
returnfgets($filepointer);
}
//Lastly, close the file.
$this->closefile ();
}
//A function to write to a file.
publicfunctionwrite ($towrite) {
//First, attempt to open the file.
$filepointer=$this->openfile ("w");
//Now, return a string with the read data.
if($filepointer!= false){
//Then we can read the file.
returnfwrite ($filepointer,$towrite);
}
//Lastly, close the file.
$this->closefile ();
}
//A function to append to a file.
publicfunctionappend ($toappend) {
//First, attempt to open the file.
$filepointer=$this->openfile ("a");
//Now, return a string with the read data.
if($filepointer!= false){
//Then we can read the file.
returnfwrite ($filepointer,$toappend);
}
//Lastly, close the file.
$this->closefile ();
}
//A function to set the path to a new file.
publicfunctionsetpath ($newpath) {
$this->thepath =$newpath;
}
}
?>
|
1
2
3
4
5
6
7
8
9
|
<?php
$myfile=newcfile ("test.txt");
//Now, let's try reading it.
echo$myfile->read();
//Then let's try writing to the file.
$myfile->write ("Hello World!");
//Then, let's try appending.
$myfile->append ("Hello Again!");
?>
|
文件下载:
1
2
3
4
5
6
7
8
9
10
11
12
|
<?php
$filename='file1.txt';
$file=fopen($filename,'r');
Header("Expires: 0");
Header("Pragma: public");
Header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
Header("Cache-Control: public");
Header("Content-Length: ".filesize($filename));
Header("Content-Type: application/octet-stream");
Header("Content-Disposition: attachment; filename=".$filename);
readfile($filename);
?>
|
本文实例讲述了PHP文件与目录操作。分享给大家供大家参考,具体如下:
文件目录相关函数
<?php
// 输出目录中的文件
functionoutputcurfiles ($allowedtypes,$thedir){
//首先,我们确保目录存在。
if(is_dir($thedir)){
//现在,我们使用scandir扫描目录中的文件。
$scanarray= scandir ($thedir);
//接着我们开始解析数组。
//scandir()用“.”和“..”统计文件导航列表
//因此作为文件,我们不应该列出他们。
for($i= 0;$i<count($scanarray);$i++){
if($scanarray[$i] !="."&&$scanarray[$i] !=".."){
//现在,进行检查,以确保这是一个文件,而不是一个目录。
if(is_file($thedir."/".$scanarray[$i])){
//现在,因为我们将允许客户端编辑这个文件,
//我们必须检查它是否是可读和可写。
if(is_writable($thedir."/".$scanarray[$i]) && is_readable($thedir."/".$scanarray[$i])){
//现在,我们检查文件类型是否存在于允许的类型数组中.
$thepath=pathinfo($thedir."/".$scanarray[$i]);
if(in_array ($thepath['extension'],$allowedtypes)){
//如果文件符合规定,我们可以继续输出.
echo$scanarray[$i] ."<br />";
}
}
}
}
}
}else{
echo"对不起,这个目录不存在.";
}
}
$allowedtypes=array("txt","html");
outputcurfiles ($allowedtypes,"testfolder");
///////////////////////////////////////////////////
functionrecurdir ($thedir) {
//First attempt to open the directory.
try{
if($adir= opendir ($thedir)){
//扫描目录。
while(false !== ($anitem= readdir ($adir))){
//不统计目录中包含“.”或“..”的情况
if($anitem!="."&&$anitem!=".."){
//此时如果是一个目录,则缩进一点
//再去递归
if(is_dir($thedir."/".$anitem)){
?><span style="font-weight: bold;"mce_style="font-weight: bold;"><?phpecho$anitem; ?></span><?php
?><div style="margin-left: 10px;"mce_style="margin-left:10px;"><?php
recurdir ($thedir."/".$anitem);
?></div><?php
}elseif(is_file($thedir."/".$anitem)){
//此时输出文件.
echo$anitem."<br />";
}
}
}
}else{
thrownewexception ("Sorry, directory could not be openend.");
}
}catch(exception$e) {
echo$e->getmessage();
}
}
echo"<br />/////////////////////////////////////<br /><br />";
recurdir("testfolder");
//////////////////////////////////////////////////////////////////
echo"<br />/////////////////////////////////////<br /><br />";
functionsortfilesbydate ($thedir){
//首先,需要确保目录存在。
if(is_dir($thedir)){
//接着,我们使用scandir扫描此目录中的文件.
$scanarray= scandir ($thedir);
$finalarray=array();
//然后开始解析数组
//scandir()用“.”和“..”统计文件导航列表
//因此作为文件,我们不应该列出他们.
for($i= 0;$i<count($scanarray);$i++){
if($scanarray[$i] !="."&&$scanarray[$i] !=".."){
//现在,我们检查,以确保这是一个文件,而不是一个目录.
if(is_file($thedir."/".$scanarray[$i])){
//现在需要做的是循环数据到一个关联数组.
$finalarray[$thedir."/".$scanarray[$i]] =filemtime($thedir."/".$scanarray[$i]);
}
}
}
//至此,我们已经遍历了整个数组,现在需要做的只是asort()它。
asort ($finalarray);
return($finalarray);
}else{
echo"对不起,这个目录不存在.";
}
}
//然后,我们将函数指向我们需要查看的目录.
$sortedarray= sortfilesbydate ("testfolder");
//至此,就可以按照如下形式输出:
while($element= each ($sortedarray)){
echo"File: ".$element['key'] ." was last modified: ".date("F j, Y h:i:s",$element['value']) ."<br />";
}
?>
|
相关文章
- Ajax提交表单时实现验证码自动验证,验证码先检测正确性,不正确则不提交表单,更新验证码,php后端验证码检测,感兴趣的小伙伴们可以参考一下...2016-07-29
MySQL查询空字段或非空字段(is null和not null)
本文介绍在MySQL中,面对值为空(NULL)的字段,应该如何查询。怎么判断一个字段的值为NULL或不为NULL,需要的朋友可以参考下...2016-04-18phalcon model在插入或更新时自动验证非空字段怎么解决
小编分享的这篇文章提供了phalcon model在插入或更新时自动验证非空字段的解决办法,工作学习中遇到问题的同学可以参考一下下文 在使用phalcon的insert和update功...2017-01-22- 这篇文章主要介绍了thinkPHP自动验证、自动添加及表单错误问题分析,结合案例形式分析了thinkPHP自动完成机制的相关使用技巧与注意事项,需要的朋友可以参考下...2016-10-20
JSON.toJSONString()空字段不忽略修改的问题
这篇文章主要介绍了JSON.toJSONString()空字段不忽略修改的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2022-02-07