dongxietao0263 2014-06-18 13:28
浏览 25
已采纳

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

Controller:

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/>";
        }
    }


    }
}

Model:

<?php

class People extends \Phalcon\Mvc\Model{


}

I have tried implementing the getSource() method into the model as the phalcon docs suggest that but still not getting the desired output of saving the POST items to the database

  • 写回答

1条回答 默认 最新

  • drvxnivoqf17568697 2014-06-19 04:07
    关注

    Try this:

    <?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!";  
            }
        }
    }
    

    Also, add this code to your main index.php (just before Phalcon\Mvc\Application->handle()):

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

    With this you'll get better error messages, so you can check if your DB settings are OK. Also remember that Phalcon only works with the database schema passively, that means, all tables and fields should already exist to the Model get stored, Phalcon just use the tables and never create them.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效