Archiv

Artikel Tagged ‘Snippets’

CDetailView in Sidebar anzeigen

24. Januar 2012

Problem:

die th-Spalte im DetailView ist per CSS 160px breit, passt nicht so einfach in eine schmale Sidebar

Lösung:

class UserInfo extends CWidget
{
  public $user;
 
  public function run()
  {
    Yii::app()->clientScript->registerCss('sidebar', 'table.sidebar th {width: 100px;}');
    $this->widget('zii.widgets.CDetailView', array(
      'data' => $this->user,
      'htmlOptions' => array('class' => 'detail-view sidebar'),
      'attributes' => array(
        'firstname',
        'lastname'
      ),
    ));
  }
}
Author: Categories: Yii Tags:

renderPortlets

16. Januar 2012
//views/layouts.main.php
<div class="span-6 last">
  <div id="sidebar">
    <?php $this->renderPortlets('right'); ?>
  </div>
</div>
//components/Controller.php
class Controller extends CController
{   
  public $portlets = array();
 
  public function addPortlet($position, $portlet)
  {
    $this->portlets[$position][] = $portlet;
  }
 
  public function renderPortlets($position)
  {
    if (isset($this->portlets[$position])) {
      foreach ($this->portlets[$position] as $portlet) {
        $this->beginWidget('zii.widgets.CPortlet', array(
          'title' => $portlet['title'],
        ));
        $this->widget($portlet['class'], $portlet['params']);
        $this->endWidget();
      }
    }
  }
}
//views/article/view.php
$this->addPortlet('right', array(
  'class' => 'zii.widgets.CMenu',
  'title' => 'Operations',
  'params' => array( //Configuration CMenu
    'items' => array(
      array('label' => 'List Article', 'url' => array('index')),
      array('label' => 'Create Article', 'url' => array('create')),
      array('label' => 'Update Article', 'url' => array('update', 'id' => $model->id)),
      array('label' => 'Delete Article', 'url' => '#', 'linkOptions' => array('submit' =>
        array('delete', 'id' => $model->id), 'confirm' => 'Are you sure you want to delete this item?')),
      ),
    'htmlOptions' => array('class' => 'operations')
  )
));
 
$this->addPortlet('right', array(
  'class' => 'ArticleInfoWidget',
  'title' => 'ArticleInfo',
  'params' => array(
    'article' => $model
  )
));
Author: Categories: Yii Tags: ,

3-spaltiges Layout

7. Januar 2012
//css/main.css
#sidebar-left
{
    padding: 20px 0 20px 20px;
}
//protected/views/layouts/column3.php
<?php $this->beginContent('//layouts/main'); ?>
<div class="container">
 
    <div class="span-6">
        <div id="sidebar-left">
	    <?php $this->renderPortlets('left'); ?>
	</div>
    </div>
 
    <div class="span-12">
        <div id="content">
            <?php echo $content; ?>
	</div>
    </div>
 
    <div class="span-6 last">
        <div id="sidebar">
	    <?php $this->renderPortlets('right'); ?>
	</div>
    </div>
 
</div>
<?php $this->endContent(); ?>
//Controller.php
public $layout = '//layouts/column3';
Author: Categories: Yii Tags:

Liefert Posts einer Kategorie über Many_Many_Relation

12. September 2011
//table post (id, title)
//table category (id, name)
//table post_category (postId, categoryId)
 
//class Post extends CActiveRecord
public function relations()
{
    return array(
        'categories' => array(self::MANY_MANY, 'Category', 'tbl_post_category(postId, categoryId)')
    );
}
 
public function search($category)
{
    $criteria = new CDbCriteria;
 
    $criteria->with = array(
        'categories' => array(
            'condition' => 'categories.id = :categoryId',
            'params' => array(':categoryId' => $category->id),
            'together' => true
        )
    );
 
    return new CActiveDataProvider('Posts', array(
        'criteria' => $criteria,
    ));
}
Author: Categories: Yii Tags: