duanpai1033 2016-02-25 20:06
浏览 10
已采纳

使用ZF2保存到多个表时检查空值

I'm using ZF2 and mysql, but the question is platform-independent. I have a data transfer object Organization that gets hydrated from an html form. OrganizationMapper has a save method that (1) gets Organization as an argument and (2) fills a couple of database tables one after another.

Suppose the 1st table gets filled ok, but the 2nd doesn't because one of the properties of Organization isn't set (not null constraint on a column). The user gets an error, but the 1st table is already filled. If he attempts to submit the form again, but this time with all html fields filled, all the tables get filled ok, but the 1st has a previous unused row.

How could I avoid this situation?

I thought of checking for empty values with if's in the mapper's save method, but it doesn't seem elegant. I know about the InputFilter validations in ZF2, but these check the user input in the form, they don't check things when the php code communicates with the database.

Any help?

  • 写回答

1条回答 默认 最新

  • douxi6903 2016-02-25 22:11
    关注

    The best way is to validate all the data before you start writing it to the database.

    I didn't use ZF2 and this solution is actually framework-dependent, so you need to check the ZF2 docs. For example, in Yii, you just define validation rules for every field of the model, so you can ensure that your Organization contains all the data before you start saving it to the database, probably something similar is possible in Zend.

    Note, that validation doesn't mean just to check for empty values, you may need to verify different things, like: "email is correct email like xxx@yyy.com", "name is not empty", "name length is more than 3 chars", "name length is less than 1000 chars" and so on.

    For Yii it roughly looks like this:

    class Organization extends ActiveRecord {
       ...
       // here we define the validation rules
       public function rules() {
           return [
              // name is required
              ['name', 'required'], 
              // check min / max length
              ['name', 'string', 'min' => 3, 'max' => 12],
              // check if email is valid
              ['email', 'email']
           ];
       }
    }
    

    Now you can do $organization->validate() to make sure everything is correct (also when you do $organization->save() the rules will be checked before saving to the database).

    And one more solution to protect from the inconsistent data is to use transactions. In the case you write to multiple tables, you anyway need them, even if you validated everything. Unexpected things happen, so it is better to protect your saving code like this (pseudo-code):

     $transaction->start();
     try {
         $table1->writeSomeData();
         $table2->writeMoreData();
         $transaction->commit();
     } (catch Exception $e) {
         $transaction->rollback();
     }
    

    Again, check your framework documentation, it probably supports this in some way.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同