开发者

What is the best way to enter hidden field data in CakePHP?

I am using CakePHP and want to pass the author's name (stored in a se开发者_如何学Cssion) to an article that is being saved to the database. Is a hidden field the only way to do this or is there a better way?


If you already have it in the session, I would not spend the extra code/time to add it to a hidden field. I would update the method to add the session variable to the $this->data so it will add it when you save the record. So the method in the controller would look something like:

function add() {
   if($this->data) {
      $this->data['Article']['author'] = $this->Session->read('User.name');
      $this->Article->create();
      if ($this->Article->save($this->data)) {
    ...
}

This way you are not dealing with all the extra work and you can still achieve the results you are looking for.


There are other ways, but a hidden field is as convenient as any other and probably the most transparent (i.e. most detectable by other developers who may pick up the code later). You could also insert the value into the $this->data structure before save and be sure that your model knows what to do with it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜