如何使 Eloquent 模型属性只能通过 public 方法更新?
How to make Eloquent model attribute updatable only through public methods?
我想防止直接从外部源设置模型属性而不通过 setter 控制逻辑。
class Person extends Model
{
public function addMoney($amount)
{
if ($amount <= 0) {
throw new Exception('Invalid amount');
}
$this->money += $amount;
}
public function useMoney($amount)
{
if ($amount > $this->money) {
throw new Exception('Invalid funds');
}
$this->money -= $amount;
}
}
这是不允许的:
$person->money = -500;
您必须使用某种访问器或setter方法:
$person->useMoney(100);
但我不关心你是如何得到这个值的:
echo $person->money;
// or
echo $person->getMoney();
// whatever
我如何强制更新此属性的唯一方法是通过规定一些额外逻辑的特定方法?从某种意义上说,将模型属性设为私有或受保护。
我想在将模型数据持久保存到数据库之前单独执行此操作and/or。
你可以为每个你想保护的成员变量覆盖set..Attribute()函数,
或者您可以在 set..Attribute() 函数中执行验证,而不是使用单独的 public 方法。
class Person extends Model
{
public function addMoney($amount)
{
if ($amount <= 0) {
throw new Exception('Invalid amount');
}
if (!isset($this->attributes['money'])) {
$this->attributes['money'] = $amount;
} else {
$this->attributes['money'] += $amount;
}
}
public function useMoney($amount)
{
if ($amount > $this->money) {
throw new Exception('Invalid funds');
}
if (!isset($this->attributes['money'])) {
$this->attributes['money'] = -$amount;
} else {
$this->attributes['money'] -= $amount;
}
}
public function setMoneyAttribute($val) {
throw new \Exception('Do not access ->money directly, See addMoney()');
}
}
使用 mutator,您的代码应如下所示:
class Person extends Model
{
public function setMoneyAttribute($amount)
{
if ($amount < 0) {
throw new Exception('Invalid amount');
}
$this->attributes['money'] = $amount;
$this->save();
}
public function addMoney($amount)
{
if ($amount <= 0) {
throw new Exception('Invalid amount');
}
$this->money += $amount;
}
public function useMoney($amount)
{
if ($amount > $this->money) {
throw new Exception('Invalid funds');
}
$this->money -= $amount;
}
}
现在,您可以使用 $person->money = -500,它会抛出异常。希望这有帮助。
我想防止直接从外部源设置模型属性而不通过 setter 控制逻辑。
class Person extends Model
{
public function addMoney($amount)
{
if ($amount <= 0) {
throw new Exception('Invalid amount');
}
$this->money += $amount;
}
public function useMoney($amount)
{
if ($amount > $this->money) {
throw new Exception('Invalid funds');
}
$this->money -= $amount;
}
}
这是不允许的:
$person->money = -500;
您必须使用某种访问器或setter方法:
$person->useMoney(100);
但我不关心你是如何得到这个值的:
echo $person->money;
// or
echo $person->getMoney();
// whatever
我如何强制更新此属性的唯一方法是通过规定一些额外逻辑的特定方法?从某种意义上说,将模型属性设为私有或受保护。
我想在将模型数据持久保存到数据库之前单独执行此操作and/or。
你可以为每个你想保护的成员变量覆盖set..Attribute()函数, 或者您可以在 set..Attribute() 函数中执行验证,而不是使用单独的 public 方法。
class Person extends Model
{
public function addMoney($amount)
{
if ($amount <= 0) {
throw new Exception('Invalid amount');
}
if (!isset($this->attributes['money'])) {
$this->attributes['money'] = $amount;
} else {
$this->attributes['money'] += $amount;
}
}
public function useMoney($amount)
{
if ($amount > $this->money) {
throw new Exception('Invalid funds');
}
if (!isset($this->attributes['money'])) {
$this->attributes['money'] = -$amount;
} else {
$this->attributes['money'] -= $amount;
}
}
public function setMoneyAttribute($val) {
throw new \Exception('Do not access ->money directly, See addMoney()');
}
}
使用 mutator,您的代码应如下所示:
class Person extends Model
{
public function setMoneyAttribute($amount)
{
if ($amount < 0) {
throw new Exception('Invalid amount');
}
$this->attributes['money'] = $amount;
$this->save();
}
public function addMoney($amount)
{
if ($amount <= 0) {
throw new Exception('Invalid amount');
}
$this->money += $amount;
}
public function useMoney($amount)
{
if ($amount > $this->money) {
throw new Exception('Invalid funds');
}
$this->money -= $amount;
}
}
现在,您可以使用 $person->money = -500,它会抛出异常。希望这有帮助。