2014-07-03 96 views
0

我想製作一個自定義小部件,就像yii帶來的一個例子(博客),而我想製作的自定義小部件是一個名爲「RecentPosts」的自定義小部件,但對於我的頁面, m會稱之爲「RecentTasks」,所以我只想在我的數據庫SQLite(像「recentPost」那樣的almos)上獲得第一個4個任務。Yii,自定義小部件返回null

<?php /* @var $this Controller */ ?> 
<?php $this->beginContent('//layouts/main'); ?> 
<div class="col-xs-12 col-sm-9"> 
    <div id="content"> 
     <?php echo $content; ?> 
    </div><!-- content --> 
</div> 
<div id="sidebar" class="col-xs-6 col-sm-3 sidebar-offcanvas" role="navigation"> 
    <div class="list-group"> 
    <?php 
     // $this->beginWidget('zii.widgets.CPortlet', array(
     // 'title'=>'Operations', 
     //)); 
     $this->widget('zii.widgets.CMenu', array(
      'items'=>$this->menu, 
      'htmlOptions'=>array('class'=>'nav nav-pills nav-stacked'), 
     )); 
     // $this->endWidget(); 
    ?> 

    <?php 
    $this->widget('recentTasks', array(
     'maxTasks'=>10 
    )); 
    ?> 
    </div> 
</div> 
<?php $this->endContent(); ?> 

在我的自定義窗口小部件內的部件:

<?php 

Yii::import('zii.widgets.CPortlet'); 

class RecentTasks extends CPortlet 
{ 
    public $title = 'Recent Tasks'; 
    public $maxTasks = 10; 

    public function getRecentTasks() 
    { 
     return Task::model()->findRecentTasks($this->maxTasks); 
    } 

    protected function renderContent() 
    { 
     $this->render('recentTasks'); 
    } 
} 

我的模型:

我COLUMN2

<?php 

class Task extends CActiveRecord 
{ 
    /** 
    * @return string the associated database table name 
    */ 
    public function tableName() 
    { 
     return 'task'; 
    } 

    public function rules() 
    { 
     // NOTE: you should only define rules for those attributes that 
     // will receive user inputs. 
     return array(
      array('Name, Status, Project_id, User_id', 'required'), 
      array('Status, Project_id, User_id', 'numerical', 'integerOnly'=>true), 
      array('Name, Create_time, Update_time, Assigned', 'length', 'max'=>45), 
      array('Description, Tags', 'safe'), 
      // The following rule is used by search(). 
      // @todo Please remove those attributes that should not be searched. 
      array('id, Name, Description, Status, Create_time, Update_time, Tags, Project_id, User_id, Assigned', 'safe', 'on'=>'search'), 
     ); 
    } 

    public function relations() 
    { 
     // NOTE: you may need to adjust the relation name and the related 
     // class name for the relations automatically generated below. 
     return array(
      'comments' => array(self::HAS_MANY, 'Comment', 'Task_id'), 
      'project' => array(self::BELONGS_TO, 'Project', 'Project_id'), 
      'user' => array(self::BELONGS_TO, 'User', 'User_id'), 
     ); 
    } 

    public function attributeLabels() 
    { 
     return array(
      'id' => 'ID', 
      'Name' => 'Name', 
      'Description' => 'Description', 
      'Status' => 'Status', 
      'Create_time' => 'Create Time', 
      'Update_time' => 'Update Time', 
      'Tags' => 'Tags', 
      'Project_id' => 'Project', 
      'User_id' => 'User', 
      'Assigned' => 'Assigned', 
     ); 
    } 

    public function findRecentTasks($limit=10) 
    { 
     $this->findAll(); 
    } 

    public function search() 
    { 
     // @todo Please modify the following code to remove attributes that should not be searched. 

     $criteria=new CDbCriteria; 

     $criteria->compare('id',$this->id); 
     $criteria->compare('Name',$this->Name,true); 
     $criteria->compare('Description',$this->Description,true); 
     $criteria->compare('Status',$this->Status); 
     $criteria->compare('Create_time',$this->Create_time,true); 
     $criteria->compare('Update_time',$this->Update_time,true); 
     $criteria->compare('Tags',$this->Tags,true); 
     $criteria->compare('Project_id',$this->Project_id); 
     $criteria->compare('User_id',$this->User_id); 
     $criteria->compare('Assigned',$this->Assigned,true); 

     return new CActiveDataProvider($this, array(
      'criteria'=>$criteria, 
     )); 
    } 

    public static function model($className=__CLASS__) 
    { 
     return parent::model($className); 
    } 
} 
在widget IM的觀點

只是讓的var_dump( $這 - > getRecentTasks());

我還沒弄清楚問題,但現在它只是返回NULL。我跟着幾乎相同的警予

回答

1

示例頁面上進行嘗試此步驟:

// location: /protected/components/RecentTasks.php 
class RecentTasks extends CWidget 
{ 
    public $title = 'Recent Tasks'; 
    public $maxTasks = 10; 

    /** 
    * Is called when $this->beginWidget() is called 
    */ 
    public function init() 
    { 

    } 

    /** 
    * Is called when $this->endWidget() is called 
    */ 
    public function run() 
    { 
     // render /protected/components/views/recentTasks.php 
     $this->render('recentTasks', array(
      'models'=>$this->getRecentTasks($this->maxTasks) 
     )); 
    } 

    public function getRecentTasks() 
    { 
     return Task::model()->findRecentTasks($this->maxTasks); 
    } 
} 

呼叫小部件,像這樣在您的視圖或佈局文件(大寫):

$this->widget('RecentTasks', array(
    'maxTasks'=>10 
)); 

然後,您可以在視圖中使用$models來顯示任務。

另見:http://www.yiiframework.com/doc/guide/1.1/en/basics.view#widget

編輯:

看來問題是在你的findRecentTasks()法,findAll()前加return。我還添加了應用限制的代碼,並且可能在將來需要這些條件。

public function findRecentTasks($limit=10) 
{ 
    return $this->findAll(array(
     // 'condition'=>'id = :id', 
     // 'params' => array('id'=>$id), 
     'limit'=>$limit 
    )); 
} 
+0

延伸到CWidget不顯示任何東西,並添加init()方法不執行任何操作 – nosthertus

+0

答案編輯:) – deacs

+0

仍然返回null – nosthertus