如何在cakePHP BelongsTo中保存到多个表?

时间:2011-07-12 21:51:33

标签: php cakephp

表单(newreports.php)在提交时需要保存到表中以及将隐藏数据(每个包含7列的27条记录)保存到BelongsTo表中。 7列是:id,user_id,reports_id,count,area,area_id,comments。区域需要预填充为0-26,并且reports_id需要对所有(100)相同。 User_id应该从表单条目中预填充。另外,id应该自动填充。我认为这可以在我的控制器中为函数newreports()完成。

我需要像这样编写数组还是有简化方法?

$this->Report->saveAll(
    Array
(
    [Report] => Array
        (
            [0] => Array
                (
                    [id] => //leave blank because it will auto-fill?
            [user_id] => //dynamically from form input
                    [reports_id] => //dynamically from form input
                    [area_id] => //dynamically from form input
                    [area] => 0
    [count] => // this should be blank as there are no counts yet
    [comments] => // this should be blank as there are no comments yet
                )
        [1] => Array
                (
                    [id] => //leave blank because it will auto-fill?
            [user_id] => //dynamically from form input
                    [reports_id] => //dynamically from form input
                    [area_id] => //dynamically from form input
                    [area] => 1
    [count] => // this should be blank as there are no counts yet
    [comments] => // this should be blank as there are no comments yet
                )
        )
) 

2 个答案:

答案 0 :(得分:1)

这就是为我做的事情

$count = 27;
$v = 0;
  do {
      ######### save report rows to database
      $this->Report->create();
      $this->Report->set(array('reports_id' => $id , 'area' => $v));
              $this->Report->save();
      $v++;
  } while ($v < $count);

答案 1 :(得分:0)

如果您的表单/视图代码以正确的方式编写,您应该可以使用$this->Report->saveAll($this->data['Report']);

查看表单的HTML源代码。输入标记的名称属性值应类似于data[Report][0][id]data[Report][0][user_id]等。第二条记录应包含data[Report][1][id]data[Report][2][user_id]等字段。

当它获得POST时,它将自动导入$ this-&gt;数据中的正确数组结构。

如果此表单始终会创建新记录,请忽略data[Report][][id]字段(或将其留空),saveAll将创建新记录。


更新以处理第一条评论。

在控制器保存所有隐藏数据之后或之前,您是否希望对$ data进行分配尚不清楚。让我们处理这两个部分;首先,保存。

因此,your own answer几乎可以正确创建数据。

$count = 27;
$v = 0;
$data = array('Report' => array());
do {
    $data['Report'][] = array('reports_id' => $id, 'area' => $v);
  $v++;
} while ($v < $count);

// save report rows to database
$this->Report->saveAll($data['Report']);

因此,准备一个数据结构,就像你在原始问题和你的答案中所做的那样。但是,我们使用saveAll()一次创建它们。

下一部分是检索已保存的数据,然后输入$ this-&gt;数据,这样您就可以按照用户在重定向后看到的表单(根据您的评论)使用它。在控制器中你需要这样的东西。

$reports = $this->Report->find('all', array(
    'conditions' => array(
        'reports_id' => $id
    )
));
// merge this in with existing data
$this->data = Set::merge($reports, $this->data);

假设$ id作为重定向的一部分在URL上。这就是您将这些数据分配给$ this-&gt;数据的方式,因此您可以在表单中使用它。

在您的表单中,您可以引用多个字段,如下所示:

$this->Form->create('Report');

foreach ($data['Report'] as $i => $report) {
    $this->Form->input('Report.'.$i.'.id');
    $this->Form->input('Report.'.$i.'.user_id');
    $this->Form->input('Report.'.$i.'.reports_id');
    $this->Form->input('Report.'.$i.'.area_id');
    $this->Form->input('Report.'.$i.'.area');
    $this->Form->input('Report.'.$i.'.count');
    $this->Form->input('Report.'.$i.'.comment');
}

$this->Form->end('Submit);

你可以read more about forms for multiple records at the CakePHP manual