php - phalcon 我的用户创建表单不适用于我的数据库
问题描述
我是 phalcon 的新手,我遇到了一个非常奇怪的问题,这里有一个更好的解释
- 我有一个应该创建用户的表单
- 我的控制器从我的表单中接收所有内容
- 在我的数据库中创建了一个用户,但只注册了电子邮件
这是我的控制器:
/**
* Creates a new user
*/
public function createAction()
{
if (!$this->request->isPost()) {
$this->dispatcher->forward([
'controller' => "users",
'action' => 'index'
]);
return;
}
$user = new Users();
$user->setlastName($this->request->getPost("LastName"));
$user->setfirstName($this->request->getPost("FirstName"));
$user->setlogin($this->request->getPost("Login"));
$user->setpassword($this->request->getPost("password"));
$user->seteMail($this->request->getPost("eMail"));
var_dump($this->request->getPost("LastName"));
var_dump($this->request->getPost("FirstName"));
var_dump($this->request->getPost("Login"));
var_dump($this->request->getPost("Password"));
var_dump($this->request->getPost("eMail"));
var_dump($user);
if (!$user->save()) {
foreach ($user->getMessages() as $message) {
$this->flash->error($message);
var_dump($message);
}
$this->dispatcher->forward([
'controller' => "users",
'action' => 'new'
]);
return;
}
$this->flash->success("user was created successfully");
$this->dispatcher->forward([
'controller' => "users",
'action' => 'index'
]);
}
和我的表格:
<?php
echo $this->tag->form(
[
"users/create",
"class" => "form-horizontal"
]
);
?>
<div class="form-group">
<label for="fieldLastname" class="col-sm-2 control-label">LastName</label>
<div class="col-sm-10">
<?php echo $this->tag->textField(["LastName", "cols" => 30, "rows" => 4, "class" => "form-control", "id" => "fieldLastname"]) ?>
</div>
</div>
<div class="form-group">
<label for="fieldFirstname" class="col-sm-2 control-label">FirstName</label>
<div class="col-sm-10">
<?php echo $this->tag->textField(["FirstName", "cols" => 30, "rows" => 4, "class" => "form-control", "id" => "fieldFirstname"]) ?>
</div>
</div>
<div class="form-group">
<label for="fieldLogin" class="col-sm-2 control-label">Login</label>
<div class="col-sm-10">
<?php echo $this->tag->textField(["Login", "size" => 30, "class" => "form-control", "id" => "fieldLogin"]) ?>
</div>
</div>
<div class="form-group">
<label for="fieldPassword" class="col-sm-2 control-label">Password</label>
<div class="col-sm-10">
<?php echo $this->tag->passwordField(["Password", "size" => 30, "class" => "form-control", "id" => "fieldPassword"]) ?>
</div>
</div>
<div class="form-group">
<label for="fieldEmail" class="col-sm-2 control-label">EMail</label>
<div class="col-sm-10">
<?php echo $this->tag->textField(["eMail", "size" => 30, "class" => "form-control", "id" => "fieldEmail"]) ?>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<?php echo $this->tag->submitButton(["Save", "class" => "btn btn-default"]) ?>
</div>
</div>
<?php echo $this->tag->endForm(); ?>
这是用户类:
<?php
namespace Phalcon_Punch;
class Users extends \Phalcon\Mvc\Model
{
/**
*
* @var integer
*/
protected $iD;
/**
*
* @var string
*/
protected $lastName;
/**
*
* @var string
*/
protected $firstName;
/**
*
* @var string
*/
protected $login;
/**
*
* @var string
*/
protected $password;
/**
*
* @var string
*/
protected $grants;
/**
*
* @var string
*/
protected $status;
/**
*
* @var string
*/
protected $lastUpdateDate;
/**
*
* @var string
*/
protected $eMail;
/**
*
* @var string
*/
protected $tag;
/**
* Method to set the value of field ID
*
* @param integer $iD
* @return $this
*/
public function setID($iD)
{
$this->iD = $iD;
return $this;
}
/**
* Method to set the value of field LastName
*
* @param string $lastName
* @return $this
*/
public function setLastName($lastName)
{
$this->lastName = $lastName;
return $this;
}
/**
* Method to set the value of field FirstName
*
* @param string $firstName
* @return $this
*/
public function setFirstName($firstName)
{
$this->firstName = $firstName;
return $this;
}
/**
* Method to set the value of field Login
*
* @param string $login
* @return $this
*/
public function setLogin($login)
{
$this->login = $login;
return $this;
}
/**
* Method to set the value of field Password
*
* @param string $password
* @return $this
*/
public function setPassword($password)
{
$this->password = $password;
return $this;
}
/**
* Method to set the value of field Grants
*
* @param string $grants
* @return $this
*/
public function setGrants($grants)
{
$this->grants = $grants;
return $this;
}
/**
* Method to set the value of field Status
*
* @param string $status
* @return $this
*/
public function setStatus($status)
{
$this->status = $status;
return $this;
}
/**
* Method to set the value of field LastUpdateDate
*
* @param string $lastUpdateDate
* @return $this
*/
public function setLastUpdateDate($lastUpdateDate)
{
$this->lastUpdateDate = $lastUpdateDate;
return $this;
}
/**
* Method to set the value of field eMail
*
* @param string $eMail
* @return $this
*/
public function setEMail($eMail)
{
$this->eMail = $eMail;
return $this;
}
/**
* Method to set the value of field tag
*
* @param string $tag
* @return $this
*/
public function setTag($tag)
{
$this->tag = $tag;
return $this;
}
/**
* Returns the value of field iD
*
* @return integer
*/
public function getID()
{
return $this->iD;
}
/**
* Returns the value of field lastName
*
* @return string
*/
public function getLastName()
{
return $this->lastName;
}
/**
* Returns the value of field firstName
*
* @return string
*/
public function getFirstName()
{
return $this->firstName;
}
/**
* Returns the value of field login
*
* @return string
*/
public function getLogin()
{
return $this->login;
}
/**
* Returns the value of field password
*
* @return string
*/
public function getPassword()
{
return $this->password;
}
/**
* Returns the value of field grants
*
* @return string
*/
public function getGrants()
{
return $this->grants;
}
/**
* Returns the value of field status
*
* @return string
*/
public function getStatus()
{
return $this->status;
}
/**
* Returns the value of field lastUpdateDate
*
* @return string
*/
public function getLastUpdateDate()
{
return $this->lastUpdateDate;
}
/**
* Returns the value of field eMail
*
* @return string
*/
public function getEMail()
{
return $this->eMail;
}
/**
* Returns the value of field tag
*
* @return string
*/
public function getTag()
{
return $this->tag;
}
/**
* Initialize method for model.
*/
public function initialize()
{
$this->setSchema("basephalcon");
$this->setSource("users");
}
/**
* Returns table name mapped in the model.
*
* @return string
*/
public function getSource()
{
return 'users';
}
/**
* Allows to query a set of records that match the specified conditions
*
* @param mixed $parameters
* @return Users[]|Users|\Phalcon\Mvc\Model\ResultSetInterface
*/
public static function find($parameters = null)
{
return parent::find($parameters);
}
/**
* Allows to query the first record that match the specified conditions
*
* @param mixed $parameters
* @return Users|\Phalcon\Mvc\Model\ResultInterface
*/
public static function findFirst($parameters = null)
{
return parent::findFirst($parameters);
}
}
解决方案
问题似乎是在数据库中,您将“LastName”作为数据库字段名称,但将“lastName”作为 PHP 字段名称。您的框架似乎使用命名约定将 PHP 字段名称映射到数据库字段名称,并且此约定区分大小写。
因此,您需要重命名数据库字段名称或 PHP 字段名称,以便它们完全匹配。
推荐阅读
- assembly - 如何使用标签和寄存器作为偏移量从内存复制到寄存器中?
- .net - 如何设置自定义 GraphQL 端点?
- javascript - ScrollView 属性“onScroll”只触发一次
- kubernetes - Kubernetes 自动缩放策略不起作用
- java - 如何从 Arraylist 中删除重复项
? (爪哇) - java - 尝试使用 Webviewer 加载网页 - JavaFx - 错误
- java - 如何在 Java 中执行 while 循环和 for 循环条件?
- windows - Windows任务计划程序最小化正在运行的程序并最大化另一个程序
- c# - 如何从字符串中获取数组
- r - 如何在ggplot2中做一个并排的条形图?