使用Phalcon框架无法将POST项目保存到数据库

时间:2014-06-18 13:28:17

标签: php html phalcon

控制器:

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{


}

我已尝试将getSource()方法实现到模型中,因为phalcon文档建议但仍未获得将POST项目保存到数据库的所需输出

1 个答案:

答案 0 :(得分:0)

试试这个:

<?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->handle()之前):

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

通过这种方式,您将获得更好的错误消息,因此您可以检查数据库设置是否正常。还要记住,Phalcon只能被动地使用数据库模式,也就是说,所有表格和字段应该已经存在于模型中存储,Phalcon只是使用表格而永远不会创建它们