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 mysql , 用自己创建的本地主机和用户名 登录不上
  • ¥15 关于#web项目#的问题,请各位专家解答!
  • ¥15 vmtools环境不正常
  • ¥15 请问如何在Ubuntu系统中安装使用microsoft R open?
  • ¥15 jupyter notebook
  • ¥30 informer时间序列预测
  • ¥20 SSR引物多态性分析
  • ¥15 大漠插件在Win11易语言注册调用和免注册灵异事件,VS上注册调用完全没问题
  • ¥15 Addressable缓存机制做热更新的问题
  • ¥15 微信开发者工具vant组件