0

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 Answer 1

2

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
    ));
}
Sign up to request clarification or add additional context in comments.

6 Comments

extending to CWidget does not show anything, and adding the init() method does nothing
what exactly returns null?
what im trying to retieve from model.. ive been making calls of method in other views and seems like the problem only comes from findRecentTasks()
yeah seems you need to add return before $this->findAll(), see the edit
well. i did not know in model methods should have returns.. now it did make the work.. i feel dumb for not realizing it
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.