【问题标题】:phalcon my user creation form not working well with my databasephalcon 我的用户创建表单不适用于我的数据库
【发布时间】:2019-04-08 16:51:51
【问题描述】:

我是 phalcon 的新手,我遇到了一个非常奇怪的问题,这里有一个更好的解释

  • 我有一个应该创建用户的表单
  • 我的控制器从我的表单中接收所有内容
  • 在我的数据库中创建了一个用户,但只注册了电子邮件

form validation picture with var dump

database picture

这是我的控制器:

/**
 * 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);
}
}

【问题讨论】:

  • 您已经向我们展示了您已经证明有效的代码(通过执行 var_dump())。因此,问题似乎出在用户类的“保存”方法中。不幸的是,您没有向我们展示这一点,因此很难为您提供帮助。请使用更多信息编辑您的问题。 (HTML表单实际上在很大程度上无关紧要,因为您可以证明数据正在发送成功)
  • @ADyson 已完成,但我认为 save() 方法应该来自 Phalcon\Mvc\Model\Criteria
  • 好的,但不要假设我们都知道这个特定的框架或代码的作用。您可以轻松找到它的源代码。看来您正在扩展 \Phalcon\Mvc\Model 类,所以我希望该方法在其中(或在该类本身继承的类中)。
  • 由于您将其扩展为包含一些自定义字段,我不太清楚您是否一定希望它知道如何在正确的位置自动将这些写入数据库?它是否使用某种命名约定?如果是这样,那么您的问题可能出在数据库中,例如“LastName”作为数据库字段名称,但“lastName”作为 PHP 字段名称,所以它可能区分大小写。只是猜测,没有看到实际代码。
  • 你是对的,这是因为我的数据库中的大写字母非常感谢

标签: php database forms model-view-controller phalcon


【解决方案1】:

可以使用columnMap()建立表中名称与模型的对应关系

 public function columnMap()
    {
        return array(
            'LastName' => 'lastName', 
        );
    }

【讨论】:

    【解决方案2】:

    问题似乎是在数据库中,您有例如“LastName”作为数据库字段名称,但“lastName”作为 PHP 字段名称。您的框架似乎使用命名约定将 PHP 字段名称映射到数据库字段名称,并且此约定区分大小写。

    因此,您需要重命名数据库字段名称或 PHP 字段名称,以便它们完全匹配。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-04
      • 1970-01-01
      • 2017-07-15
      • 2023-03-15
      • 2020-11-21
      • 1970-01-01
      • 1970-01-01
      • 2021-05-21
      相关资源
      最近更新 更多