0

我正在用 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() 方法尝试选择新插入的数据库行时,似乎出现错误:

    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:好的问题是,“arrModified”包含一个空字符串作为 ID。有人知道这个数组从哪里得到它的元素吗?

4

1 回答 1

1

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

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

代替

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

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

更新:

此外,您对setDataand的使用mergeRow可能不是这种方式。你应该改用

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

例如。

于 2016-04-20T08:27:37.727 回答