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.

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

报告相同问题?