douhong4452 2014-07-03 18:04
浏览 44
已采纳

Yii,自定义小部件返回null

I want to make a custom widget like one of the examples that yii brings (blog), and the custom widget i want to make is the one called "RecentPosts", but for my page i'm going to call it "RecentTasks", so i just want to get the firsts 4 tasks on my database SQLite(almos like "recentPost" does).

in my column2:

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

in my custom widget inside components:

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

my model:

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

in the view of the widget im just making var_dump($this->getRecentTasks());

i haven't figure out the problem but for now its just returning NULL. I followed almost the exact same steps made on the example page in yii

  • 写回答

1条回答 默认 最新

  • du5591 2014-07-03 21:18
    关注

    Try this:

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

    Call the widget like so in your view or layout file (with a capital):

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

    Then you can use $models in the view to show the tasks.

    Also see: http://www.yiiframework.com/doc/guide/1.1/en/basics.view#widget

    EDIT:

    Seems the problem is in your findRecentTasks() method, add return before the findAll(). I also added the code to apply the limit and for conditions may you need that in the future.

    public function findRecentTasks($limit=10)
    {
        return $this->findAll(array(
            // 'condition'=>'id = :id',
            // 'params' => array('id'=>$id),
            'limit'=>$limit
        ));
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记