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 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题