蛋糕PHP模型的元表关系

时间:2014-04-18 06:38:13

标签: php database cakephp cakephp-2.3 cakephp-appmodel

我有一个名为“帖子”的表格,其中包含id, title, owner, content.等帖子详细信息 我有另一个表来存储帖子表字段的更改历史记录(post_changes)。

post_id, created, field, old_value, new_value;

我有模型post.php

class Post extends AppModel {
   public $hasMany = array('PostChanges');
}

class PostChanges extends AppModel {
   public $belongsTo = array('Post');

  public function beforeSave($options = array()) {

    /*
    foreach($this->data['PostChanges'] as $field => $value) {
        //if($field !== 'user_id') {
            $this->data['PostChanges']['field'] = $field;
            $this->data['PostChanges']['value'] = $value;
        //}
    }
    */

    $db = ConnectionManager::getDataSource('default');          
    $this->data['PostChanges']['created'] = $db->expression('NOW()');
    var_dump($this->data['PostChanges']);
    return true;
}
}

//来自beforeSave的Var Dump

// Trace from Post beforeSave//
array(2) {
  ["PostChanges"]=>
  array(2) {
    ["comment"]=>
    array(1) {
      ["PostChanges"]=>
      array(1) {
        ["this is a test comment from comment box 1"]=>
        NULL
      }
    }
    ["comment1"]=>
    array(1) {
      ["PostChanges"]=>
      array(1) {
        ["this is a another test content from text area box 2"]=>
        NULL
      }
    }
  }
  ["Post"]=>
  array(8) {
    ["type"]=>
    string(1) "3"
    ["priority"]=>
    string(1) "4"
    ["component"]=>
    string(1) "2"
    ["severity"]=>
    string(1) "4"
    ["milestone"]=>
    string(1) "4"
    ["version"]=>
    string(1) "4"
    ["id"]=>
    string(2) "12"
    ["modified"]=>
    object(stdClass)#49 (2) {
      ["type"]=>
      string(10) "expression"
      ["value"]=>
      string(5) "NOW()"
    }
  }
}
// Trace from PostChanges beforeSave//
array(3) {
  ["this is a test comment from comment box 1"]=>
  NULL
  ["post_id"]=>
  string(2) "12"
  ["created"]=>
  object(stdClass)#50 (2) {
    ["type"]=>
    string(10) "expression"
    ["value"]=>
    string(5) "NOW()"
  }
}
// Trace from PostChanges beforeSave//
array(3) {
  ["this is a another test content from text area box 2"]=>
  NULL
  ["post_id"]=>
  string(2) "12"
  ["created"]=>
  object(stdClass)#47 (2) {
    ["type"]=>
    string(10) "expression"
    ["value"]=>
    string(5) "NOW()"
  }
}

在我的控制器中使用

if ($this->Post->saveAll($this->request->data)) {

}

和我的观点

<div class="form-group">
        <?php echo $this->Form->input('PostChanges.comment', array('rows' => '8', 'class' => 'form-control text_editor', 'placeholder' => '')); ?>
        <?php echo $this->Form->input('PostChanges.comment1', array('rows' => '8', 'class' => 'form-control text_editor', 'placeholder' => '')); ?>
</div>

我的问题是我的beforeSave函数没有PostChanges.comment, PostChanges.comment1。所以我不能在那里使用我的foreach循环。

使用cakephp 2.9

0 个答案:

没有答案