不保存php-redbean到数据库的属性 [英] Not saving property of a php-redbean to database

查看:148
本文介绍了不保存php-redbean到数据库的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Redbean作为我的php应用程序的ORM。



每个用户(在这种情况下的员工),必须有一个密码才能登录,我想我会为他们生成一个,所以他们不必在表单中键入它。



显然,这意味着他们都必须有一个salt,并且唯一存储的密码应该是实际密码的哈希值。因为我必须发送密码给用户(否则他们不会知道它:D),因此必须有它作为对象的属性,而不保存它的数据库。



一个聪明的地方生成密码将在模型中,所以它基本上是由每个新员工(用户)本身,因此我创建了这个模型:

  class Model_employee extends RedBean_SimpleModel 
{
public function dispense()
{
$ this-> salt = cypher :: getIV(32);
$ this-> tempPassword = cypher :: getIV(8);
$ this-> password = md5($ this-> salt。$ this-> password);
}

public function update()
{
unset($ this-> tempPassword);
}
}

()工作正常。 update()应该在bean保存之前运行,因此它是(如果我将该属性保存为null),但是,tempPassword仍然保存,即使我取消设置它(列也创建,即使我存储为null)。



基本上,问题归结为:如何摆脱 tempPassword 属性,因此它不会保存到数据库?

解决方案

原来,有人只是问了确切的问题几天前,在 readBean论坛。 p>

基本上,redbean不会存储任何类扩展的私有属性。



解决方案很简单: / p>

  class Model_employee extends RedBean_SimpleModel 
{
private $ tempPassword;
public function dispense()
{
$ this-> salt = cypher :: getIV(32);
$ this-> tempPassword = cypher :: getIV(8);
$ this-> password = md5($ this-> salt。$ this-> password);
}
}

这将不会在数据库中存储密码,也不创建列。
当然,如果我想读取密码,我必须添加一个getter(),但上面解决了直接的问题:)


I'm using Redbean as an ORM for my php-application.

Every user (employee in this situation), has to have a password in order to login, and I thought i'd generate one for them, so they don't have to type it in the form.

Obviously, this means they all have to have a salt, and the only password stored should be the hash value of the actual password. Because of that I have to send the password to the user (otherwise they won't know it :D), and thus have to have it as a property of the object, without saving it for the database.

A clever place to generate passwords would be in the model, so that it basically does it by itself for every new employee (user), therefor I created this model:

class Model_employee extends RedBean_SimpleModel
{
  public function dispense()
  {
    $this->salt = cypher::getIV(32);
    $this->tempPassword = cypher::getIV(8);
    $this->password = md5($this->salt . $this->password);
  }

  public function update()
  {
    unset($this->tempPassword);
  }
}

The generating password in dispense() works fine. The update() is supposed to be run right before the bean is saved, and so it is (if I null the property it is saved as null), however, the tempPassword is still saved, even if I unset it (the column is also created, even if I store it as null).

Basically, the question boils down to this: How do I get rid of the tempPassword property, so that it is not saved to the database?

解决方案

It turns out that someone else just asked that exact question a couple of days ago, in the readBean forum.

Basically, redbean won't store private properties of any class extension.

The solution was then quite simple:

class Model_employee extends RedBean_SimpleModel
{
  private $tempPassword;
  public function dispense()
  {
    $this->salt = cypher::getIV(32);
    $this->tempPassword = cypher::getIV(8);
    $this->password = md5($this->salt . $this->password);
  }
}

This won't store the password in the database, nor create the column. Of course, I have to add a getter() if I want to read the password, but the above solves the immediate problem :)

这篇关于不保存php-redbean到数据库的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆