2016-04-13 42 views
0

我需要跟蹤此Web應用程序的幫助。我對Yii非常陌生,我試圖剖析現有的應用程序以更好地理解它。我正在嘗試創建一個編輯功能,除了指定要覆蓋的ID(並且我很可能錯誤)之外,視頻教程讓我相信它具有與添加功能完全相同的過程[save()]。Yii:ID在加載時被覆蓋

近,我所知道的,下面的文件在起作用:

  • 的意見/論壇/ view.php
  • 的意見/論壇/ _commentform.php
  • 的意見/論壇/ _comments.php
  • 控制器/ ForumController.php
  • 模型/ Forum.php
  • 模型/ Comment.php

我不能真正改變現有的大部分,但我可以添加自己的。它始於view.php,其中顯示了大部分內容。在它的底部是這樣的:

<?php 
$this->widget('zii.widgets.CListView', 
    array('dataProvider'=>$dataProvider, 'itemView'=>'_comments', 'summaryText'=>'',)); 
?> 

_comments.php顯示註釋的所有常見的元素,如說,​​從Facebook。這裏有一個編輯按鈕,在這裏編碼:

<?php echo CHtml::link(CHtml::encode('Edit'),array('forum/editcomment','reply'=>$data->id,'topic'=>$data->content_id)); ?> 

編輯按鈕從數據庫中獲取當前註釋的ID。就在應用程序日誌可以告訴我的時候,這確實起作用。

這就要求在ForumController.php這個特殊的功能:

public function actionEditComment() { 
    if(isset($_GET['reply'])) { 
     $comment=Comment::model()->findByAttributes(array('id'=>$_GET['reply'])); 
     $topic=Forum::model()->findByAttributes(array('id'=>$comment->content_id)); 
     $this->renderPartial('_commentform', array('forum'=>$topic, 'model'=>$comment, 'view'=>'view',)); 
    } 
} 

接下來是_commentform.php。沒什麼,只是一個文本框,雖然它確實檢查ID是否存在;如果是,它正在編輯現有的評論,否則,它正在創建一個新的評論。提交按鈕也從Reply更改爲Update,具體取決於isNewRecord的值。

編輯:還有一個CActiveForm,以防任何幫助。可能與路由有關?

<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'comment-form', 
    'action'=>Yii::app()->createUrl('forum/view/id/'.$forum->id), 
    'enableAjaxValidation'=>false, 
)); ?> 

<?php 
if ($view == 'view') { 
    echo CHtml::submitButton($model->isNewRecord ? 'Reply' : 'Update', array('id'=>'comment'.$model->id)); 
}?> 

同樣,通過應用日誌證實,註釋的ID被穿過,儘管如id => comment<commentID>。那麼這就是事情變得朦朧的地方。我假設流程回到ForumController.php,在我的日誌中,ID丟失。

這裏是ForumController.php的部分,我認爲負責:

public function actionView() {  
    $post=$this->loadModel(); 
    $comment=$this->newComment($post); 
    $viewcount=$post->view_count+1; 
    $post->view_count=$viewcount; 
    $post->save(); 

    $this->render('view',array('model'=>$post, 'dataProvider'=>$dataProvider,)); 
} 

private $_model; 
public function loadModel() { 
    if($this->_model===null) { 
     if(isset($_GET['id'])) { 
      $this->_model=Forum::model()->findByPk($_GET['id'], $condition); 
     } 
     if($this->_model===null) 
      throw new CHttpException(404,'The requested page does not exist.'); 
    } 
    return $this->_model; 
} 

protected function newComment($post) { 
    $comment=new Comment; 
    if(isset($_POST['Comment'])) { 
     $comment->attributes=$_POST['Comment']; 
     $post->addComment($comment); 
    } 
    return $comment; 
} 

有趣的是,如果我寫了從newComment()$comment出來的日誌,它打印出編輯評論(即,它打印如果我編輯了現有的評論「誰是冠軍?」),但是$comment->id產生一個空值,我假設這是爲什麼而不是更新,編輯的評論被保存爲一個新的。

至於模型,Forum.phpComment.php奇怪地指向相同的數據庫表,因爲他們因爲某些原因決定將論壇和評論放在一張表中。 Forum.php也包含實際的addComment函數(我發現一個奇怪的位置),儘管當流量到達那裏時,雖然編輯的註釋本身在那裏,但註釋ID當然爲空。

我哪裏錯了?我錯過了什麼嗎?

編輯:下面是註釋模型的屬性和規則:

public function attributeLabels() { 
    return array(
     'id' => 'ID', 
     'node_type' => 'Node Type', 
     'party_id' => 'Party', 
     'category' => 'Category', 
     'title' => 'Title', 
     'content' => 'Content', 
     'date_created' => 'Create Time', 
     'date_modified' => 'Update Time', 
     'status' => 'Status',); 
} 

public function rules() 
{ 
    /* combine parent and own rules */ 
    $parentRules = parent::rules(); 

    $myRules = array(
     array('node_type_id', 'default', 'value'=>'7'), /* set type to Person */ 
     array('node_type_id', 'in', 'range'=>array('7')), /* allow only Person type */ 
     array('party_id, date_created, date_modified, status', 'numerical', 'integerOnly'=>true), 
     array('category, title, content', 'safe'), 
    ); 

/* you want to apply parent rules last, delete them here if necessary */ 
    return array_merge($myRules); 
} 

回答

0

你能不能發表評論類defenition這裏?

我認爲你沒有規則「評論::規則(012)」, 當規則屬性不定義屬性將是不安全的,你不能設置它的價值$ comment->屬性,或者你可以改變你的代碼,如:

if(isset($_POST['Comment']) && isset($_POST['Comment']['id'])) { 
    $comment = Comment::model()->findByPk($_POST['Comment']['id']); 
    $comment->attributes=$_POST['Comment']; 
    $post->addComment($comment); 
} 
+0

它大約有一百行。我應該只發布'attributeLabels()'?我還編輯了一部分'_commentform.php',與CActiveForm有關,可能會強制一些東西。 –

+0

不,在這裏發佈just rules()方法的評論 –

+0

我編輯了這個問題。 '規則()'在那裏。 –