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.

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

报告相同问题?

悬赏问题

  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 angular开发过程中,想要读取模型文件,即图1的335行,会报404错误(如图2)。但我的springboot里配置了静态资源文件,如图3。且在该地址下我有模型文件如图4,请问该问题该如何解决呢?
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解
  • ¥15 有偿请教计算电磁学的问题涉及到空间中时域UTD和FDTD算法结合的
  • ¥15 vite打包后,页面出现h.createElement is not a function,但本地运行正常
  • ¥15 Java,消息推送配置