php - contao - 保存我的模型给我一个空模型

时间:2016-04-18 15:39:33

标签: php contao

我正在为php编写一个Contao模块。 我正在使用函数" Model :: save()",它将我的数据保存到数据库中。 但是当我在保存后尝试使用模型时,它只是空的。我不知道这是怎么发生的。 代码段:

            $report->tstamp = time();
            $report->machine_id = $machine_data['type_of_machine'];
            var_dump($report);
            echo "<br/>";
            $report->save();
            var_dump($report);
            echo "<br/>";

所以在我保存之前的var_dump中,一切都很好,但第二个没有显示任何数据! 有没有人有想法?

EDIT2: 好的,这里是模块的完整代码:

<?php


    use Contao\Date;
    use Contao\FilesModel;
    use Contao\Input;
    use Contao\Module;
    use Contao\PageModel;
    use Contao\RequestToken;
    use Contao\Validator;

    class ModuleReportData extends Module

{

protected $strTemplate = 'mod__reportdata';

public function generate()
{

    if (TL_MODE == 'BE')
    {
        /** @var \BackendTemplate|object $objTemplate */
        $objTemplate = new \BackendTemplate('be_wildcard');

        $objTemplate->wildcard = '### ReportData ###';
        $objTemplate->href = 'contao/main.php?do=themes&amp;table=tl_module&amp;act=edit&amp;id=' . $this->id;

        return $objTemplate->parse();
    }



    return parent::generate();
}

public function compile()
{

    $report_id = Input::get('r');

    if($report_id){
        $report = ReportModel::findByPk($report_id);
        $project = ProjectModel::findBy('report_id', $report_id);
    }else{
        $report = new ReportModel();
         $project = new ProjectModel();
    }
    $machine = new MachineModel();

    $machines = [];
    $next_step = false;

    //get data for selectbox machines
    $result = $this->Database->prepare("SELECT * FROM tl_sa_machines")->execute();

    while($result->next())
    {
        $id = $result->id;
        $machines[$id] = $result->type;
    }
    //Check if form was submitted
    if(Input::post('submit_data')){
        $report_data = Input::post('report_data');
        $project_data = Input::post('project_data');
        $machine_data = Input::post('machine_data');

        $errors = [];
        $next_step = true;
        foreach($report_data as $key => $data)
        {
            if(empty($data)) continue;
            switch ($key) {
                case 'document_date':
                    if (preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/", $data)) //###andere Formate hinzufügen
                    {
                        break;
                    }
                    else {
                    $next_step = false;
                    $errors[$key] ="Error";
                    break;
                    }
                case 'customer':
                    if(Validator::isAlphanumeric($data)) break;
                    else {
                        $next_step = false;
                        $errors[$key] ="Error";
                        break;
                    }
                case 'city':
                    if(Validator::isAlphanumeric($data)) break;
                    else {
                        $next_step = false;
                        $errors[$key] ="Error";
                        break;
                    }
                case 'country':
                    if(Validator::isAlphanumeric($data)) break;
                    else {
                        $next_step = false;
                        $errors[$key] ="Error";
                        break;
                    }
                case 'document_version':
                    if(Validator::isNumeric($data)) break;
                    else {
                        $next_step = false;
                        $errors[$key] ="Error";
                        break;
                    }
                case 'author':
                    if(Validator::isAlphanumeric($data)) break;
                    else {
                        $next_step = false;
                        $errors[$key] ="Error";
                        break;
                    }
                case 'max_speed':
                    if(Validator::isNumeric($data)) break;
                    else {
                        $next_step = false;
                        $errors[$key] ="Error";
                        break;
                    }
            }

        }
        $report->setRow($report_data);

        foreach($project_data as $key => $data)
        {
            if(empty($data)) continue;
            if(Validator::isAlphanumeric($data)) continue;
            else {
                $next_step = false;
                $errors[$key] = "Error";
            }
        }
        $project->setRow($project_data);

        if($next_step)
        {
            $project->date_of_evaluation = strtotime($project->date_of_evaluation);
            $report->document_date = strtotime($report->document_date);

            //save and set report_data
            $report->tstamp = time();
            $report->machine_id = $machine_data['type_of_machine'];
            var_dump($report);
            echo "<br/>";
            $report->save();
            var_dump($report);
            echo "<br/>";
            $report = ReportModel::findByPK($report_id);
            var_dump($report);

            //save and set project_data
            $project->report_id = $report->id;
            $project->tstamp = time();
            $project->save();

            //session for transfering report_id to the next page
           /* var_dump($report->id);
            var_dump($report_id);
            var_dump($project->report_id);
            if($report_id) {
                $_SESSION['report_id'] = $report_id;
            }
            else
            {//var_dump($report_id);
                //var_dump($report->id);
                $report_id = $report->id;
                $_SESSION['report_id'] = $report_id;
            }

            $jumpTo = PageModel::findByPk($this->jumpTo);
            $url = $this->generateFrontendUrl($jumpTo->row());

            $this->redirect($url);*/
        }
    }

    $this->Template->report = $report;
    $this->Template->project = $project;
    $this->Template->machine = $machine;
    $this->Template->machines = $machines;
    $this->Template->errors = $errors;

    $this->Template->request_token = RequestToken::get();


}

}

我有一个表单,用于保存新数据或编辑现有数据。我试图填充数据的数据库中有两个不同的表。第二个我需要在此代码中生成的新行的新ID。但它不起作用,因为保存后模型是空的。

EDIT3: ProjectModel就是这么简单:

    use Contao\Model;

class ProjectModel extends Model{
    protected static $strTable = "tl_sa_projects";
    }

我刚刚发现,只有当我在$ report上使用save方法时才会发生这种情况。它与$ project一起工作正常!

更新: 当refresh()方法尝试使用:

选择新插入的databaserow时,看起来我收到错误
    public function refresh()
    {
        $intPk = $this->{static::$strPk};

        // Track primary key changes
        if (isset($this->arrModified[static::$strPk]))
        {
            $intPk = $this->arrModified[static::$strPk];
        }

        // Reload the database record
        $res = \Database::getInstance()->prepare("SELECT * FROM " . static::$strTable . " WHERE " . static::$strPk . "=?")
                                       ->execute($intPk);

var_dump($res);

        $this->setRow($res->row());
    }

更新2: 好的问题是,&#34; arrModified&#34;包含一个空字符串作为ID。有人知道这个数组的元素在哪里吗?

1 个答案:

答案 0 :(得分:1)

不是您原来问题的答案,但您应该使用

ProjectModel::findOneBy('report_id', $report_id);

而不是

ProjectModel::findBy('report_id', $report_id);

因为您只想找到一个特定项目。 findBy会返回Contao\Model\Collection(即可能有多个结果),而findOneBy会返回Contao\Model

<强>更新

此外,您对setDatamergeRow的使用可能不是这样的。你应该改为使用

foreach ($project_data as $key => $val)
{
    $project->$key = $val;
}
例如