phalcon моя форма создания пользователя не работает с моей базой данных - PullRequest
0 голосов
/ 05 ноября 2018

Я новичок в phalcon, и у меня действительно странная проблема, вот лучшее объяснение

  • У меня есть форма для создания пользователя
  • Мой контроллер получает все из моей формы
  • В моей базе данных создан пользователь, но зарегистрирован только адрес электронной почты

форма проверки изображения с дампом var

изображение базы данных

вот мой контроллер:

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

вот класс Users:

<?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);
}
}

Ответы [ 2 ]

0 голосов
/ 09 ноября 2018

Вы можете использовать columnMap (), чтобы установить соответствие между именами в таблице и моделью

 public function columnMap()
    {
        return array(
            'LastName' => 'lastName', 
        );
    }
0 голосов
/ 05 ноября 2018

Проблема заключается в том, что в БД у вас есть, например, «LastName» в качестве имени поля БД, но «lastName» в качестве имени поля PHP. Кажется, ваш фреймворк использует соглашение об именах для сопоставления имен полей PHP с именами полей базы данных, и это соглашение учитывает регистр.

Поэтому вам необходимо переименовать либо имя поля вашей базы данных, либо имя поля PHP, чтобы они полностью совпадали.

...