保存不为空的模型值
Save Model Values Without Null
每次我尝试更新一行时,我都会收到一条错误消息 "something is required"。在 codeigniter 中,您可以更新行,而无需在 mysql 表格设置中将所有内容都设置为 null。
我只想更新一个值而不是整行。
这可能吗?
if ($users->save() == false) {
echo "Umh, We can't update the user right now: \n";
foreach ($users->getMessages() as $message) {
echo $message, "<br>";
}
$this->flash->error("Error in updating information.");
$this->response->redirect('user/profile');
} else {
echo "Great, a new robot was saved successfully!";
$this->flash->success("Member has been updaed successfully.");
//$this->response->redirect('user/profile');
}
你正在做的事情叫做插入。在预先存在的行中将列设置为不同的值称为更新。
后者灵活,前者不灵活
我强烈建议不要像 this is what i feel like
那样对待数据库
把所有的数据都放进去,Null是你的敌人
您的问题发生是因为您已经填充 table 但尚未正确定义模型。 Phalcon 在尝试保存之前验证所有模型数据。如果您使用所有默认值、跳过等正确定义模型,将根据需要在单个列上触发更新。
如果您有不允许空值的定义,但无论如何您都需要一个空值或默认值,您可能希望在模型实现中使用 'beforeCreate' 操作。此外,如果在第一次插入时设置了默认值,您可能想使用 skipAttributes
方法。
文档中有更多信息:Working with Models。到目前为止我在互联网上找到的最好的一点。
此外,下面是我的工作代码中可空电子邮件列和 NOT NULL DEFAULT '0'
'skipped' 列的示例:
public function initialize() {
$this->skipAttributesOnCreate(['skipped']);
}
public function validation()
{
if($this->email !== null) {
$this->validate(
new Email(
array(
'field' => 'email',
'required' => true,
)
)
);
if ($this->validationHasFailed() == true) {
return false;
}
}
}
您确实想要 "something is required" 的错误。您所缺少的只是模型默认值的正确实现。一旦你习惯了这些机制,你就会发现它们很容易处理,而且利大于弊。
每次我尝试更新一行时,我都会收到一条错误消息 "something is required"。在 codeigniter 中,您可以更新行,而无需在 mysql 表格设置中将所有内容都设置为 null。
我只想更新一个值而不是整行。
这可能吗?
if ($users->save() == false) {
echo "Umh, We can't update the user right now: \n";
foreach ($users->getMessages() as $message) {
echo $message, "<br>";
}
$this->flash->error("Error in updating information.");
$this->response->redirect('user/profile');
} else {
echo "Great, a new robot was saved successfully!";
$this->flash->success("Member has been updaed successfully.");
//$this->response->redirect('user/profile');
}
你正在做的事情叫做插入。在预先存在的行中将列设置为不同的值称为更新。
后者灵活,前者不灵活
我强烈建议不要像 this is what i feel like
把所有的数据都放进去,Null是你的敌人
您的问题发生是因为您已经填充 table 但尚未正确定义模型。 Phalcon 在尝试保存之前验证所有模型数据。如果您使用所有默认值、跳过等正确定义模型,将根据需要在单个列上触发更新。
如果您有不允许空值的定义,但无论如何您都需要一个空值或默认值,您可能希望在模型实现中使用 'beforeCreate' 操作。此外,如果在第一次插入时设置了默认值,您可能想使用 skipAttributes
方法。
文档中有更多信息:Working with Models。到目前为止我在互联网上找到的最好的一点。
此外,下面是我的工作代码中可空电子邮件列和 NOT NULL DEFAULT '0'
'skipped' 列的示例:
public function initialize() {
$this->skipAttributesOnCreate(['skipped']);
}
public function validation()
{
if($this->email !== null) {
$this->validate(
new Email(
array(
'field' => 'email',
'required' => true,
)
)
);
if ($this->validationHasFailed() == true) {
return false;
}
}
}
您确实想要 "something is required" 的错误。您所缺少的只是模型默认值的正确实现。一旦你习惯了这些机制,你就会发现它们很容易处理,而且利大于弊。