2014-07-03 1 views
0

Je veux créer un widget personnalisé comme l'un des exemples que yii apporte (blog), et le widget personnalisé que je veux faire est celui appelé "RecentPosts", mais pour ma page i ' Je vais l'appeler "RecentTasks", donc je veux juste obtenir les premières 4 tâches sur ma base de données SQLite (almos comme "recentPost" fait).Yii, widget personnalisé renvoie null

dans mon colonne2:

<?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(); ?> 

dans mon widget personnalisé à l'intérieur des composants:

<?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'); 
    } 
} 

mon modèle:

<?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); 
    } 
} 

dans la vue du widget im juste faire var_dump ($ this-> getRecentTasks());

Je n'ai pas compris le problème, mais pour l'instant, il vient de retourner NULL. J'ai suivi presque exactement les mêmes étapes faites sur la page d'exemple dans yu

Répondre

1

Essayez ceci:

// 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); 
    } 
} 

Appelez le widget comme si dans votre fichier de vue ou la mise en page (avec une majuscule):

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

Ensuite, vous pouvez utiliser $models dans la vue pour afficher les tâches.

Voir aussi: http://www.yiiframework.com/doc/guide/1.1/en/basics.view#widget

EDIT:

semble que le problème est dans votre méthode findRecentTasks(), ajoutez return avant la findAll(). J'ai également ajouté le code pour appliquer la limite et pour les conditions vous pouvez en avoir besoin dans le futur.

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

extension à cwidget ne montre rien, et en ajoutant la méthode init() ne fait rien – nosthertus

+0

:) réponse modifiée – deacs

+0

retourne toujours null – nosthertus