【问题标题】:Cannot save POST items to database with Phalcon framework无法使用 Phalcon 框架将 POST 项目保存到数据库
【发布时间】:2014-08-08 19:50:09
【问题描述】:

控制器:

class PeopleController extends \Phalcon\Mvc\Controller{

public function indexAction(){

}
public function CreatePersonAction(){
        $person = new people();
        $person->firstName=$this->request->getPost("firstName");                
        $person->surname=$this->request->getPost("surname");
        $person->telephone=$this->request->getPost("telephone");
        $person->email=$this->request->getPost("email");
        $person->city=$this->request->getPost("city");
        $person->country=$this->request->getPost("country");
        $person->save();
    if ($person) {
        echo"Successfully Registered User!";


    } else {
        echo "Sorry, the following problems were generated: ";
        foreach ($person->getMessages() as $message) {
            echo $message->getMessage(), "<br/>";
        }
    }


    }
}

型号:

<?php

class People extends \Phalcon\Mvc\Model{


}

我已经尝试按照 phalcon 文档的建议在模型中实现 getSource() 方法,但仍然没有获得将 POST 项目保存到数据库的所需输出

【问题讨论】:

  • 您的数据库字段名称是否类似于firstNamesurname、...?
  • 来自文档:如果您使用的是 PHP 5.4/5.5,建议您声明构成模型一部分的每一列,以节省内存并减少内存分配。 i>(只是一个提示,不是解决您问题的方法)
  • getSource() 仅当您的表未命名为 people 时才需要。

标签: php html phalcon


【解决方案1】:

试试这个:

<?php

use Phalcon\Mvc\Controller as PhController;

class PeopleController extends PhController
{
    public function IndexAction()
    {
        //When no view(aka template) is used you should disable the view rendering
        //Otherwise the output buffer can get overwritten and your echoes won't display
        $this->view->disable(); 

        echo "<h1>Index Action!</h1>";
    }

    public function CreatePersonAction()
    {
        $this->view->disable();

        if($this->request->isPost()) {
            $dataSent = $this->request->getPost();

            $person = new People();
            $person->firstName  = $dataSent["firstName"]
            $person->surname    = $dataSent["surname"];
            $person->telephone  = $dataSent["telephone"];
            $person->email      = $dataSent["email"];
            $person->city       = $dataSent["city"];
            $person->country    = $dataSent["country"];

            $savedSuccessfully = $person->save();

            if($savedSuccessfully) {
                echo "Successfully Registered User!";
            } else {
                $messages = $person->getMessages();

                echo "Sorry, the following problems were generated: ";
                foreach ($messages as $message) {
                    echo "$message <br/>";
                }
            }
        } else {
            echo "The request method should be POST!";  
        }
    }
}

另外,将此代码添加到您的主 index.php(就在 Phalcon\Mvc\Application-&gt;handle() 之前):

$debug = new \Phalcon\Debug();
$debug->listen();

这样您将收到更好的错误消息,因此您可以检查您的数据库设置是否正常。还要记住,Phalcon 只能被动地使用数据库模式,这意味着,所有表和字段都应该已经存在到模型存储中,Phalcon 只是使用表并且从不创建它们.

【讨论】:

  • 哦,这似乎让我更接近我需要的地方,因为它抱怨请求没有发布。我的 HTML 在视图中可能有问题。我有以下内容:&lt;form method="POST" action="createPerson"&gt; &lt;div class="input-group"&gt; &lt;label for="firstName"&gt;First Name&lt;/label&gt;&lt;br&gt; &lt;input type="text" class="form-control" placeholder="First Name" name="firstName"&gt;
  • @JoshBoiskin 请将你所有的 HTML 发给我pastebin
  • @JoshBoiskin 在深入研究 PHP 之前,您需要先学习一些 HTML 基础知识。尝试先修复您的 HTML。
  • 哦,谢谢,现在至少我知道我的 HTML 有问题,祝您有愉快的一天
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-01
  • 2017-02-09
  • 1970-01-01
相关资源
最近更新 更多