我想创建一个自定义小部件,就像yii带来的一个示例(博客),我想制作的自定义小部件是一个名为“RecentPosts”的小部件,但对于我的页面,我将称之为“ RecentTasks“,所以我只想在我的数据库SQLite
上获得第一个任务(像”recentPost“这样的almos)。
在我的第2栏:
<?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');
}
}
我的模特:
<?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);
}
}
在小部件的视图中我只是制作var_dump($ this-&gt; getRecentTasks());
我还没有弄清楚问题,但现在它只是返回NULL。我几乎按照yii
中示例页面上的相同步骤进行操作答案 0 :(得分: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()
方法,请在return
之前添加findAll()
。我还添加了应用限制的代码,以及将来可能需要的条件。
public function findRecentTasks($limit=10)
{
return $this->findAll(array(
// 'condition'=>'id = :id',
// 'params' => array('id'=>$id),
'limit'=>$limit
));
}