Yii Widgets CGridView显示数据

时间:2014-11-27 12:13:49

标签: yii widget where cgridview dataprovider

我的数据库中有2个表: t_product t_user

T_USER

“************* *************“

'* cod_arm *descrição* 用户名 *密码* salt * tipo *'

“************* *************“

'* 000 * 000 - ADMIN * admin * 123 * asdfgh * A *'

'* 001 * 001 - MEDICINA 1 * P01 * 123 * asdf * U *'

'* 021 * 021 - UROLOGIA * P21 * 123 * asdfg * U *'

*************** *************“

t_product

“************* ***************'

'* id_prod * cod_art * designacao * unidade * data_validade * cod_loc *'

“************* ***************'

'* 1 * 210110300 * ADESIVO COMUM(...)* ROL * 2014-11-30 * P010101 *'

'* 2 * 210110320 * ADESIVO COMUM(...)* ROL * NULL * P01 *'

'* 3 * 210110302 * ADESIVO COMUM(...)* ROL * 2016-12-30 * P210110 *'

'* 4 * 210110301 * ADESIVO COMUM(...)* ROL * 2014-11-30 * P010101 *'

'* 1 * 210110300 * ADESIVO COMUM(...)* ROL * 2014-11-30 * P01EXT *'

'* 1 * 210110300 * ADESIVO COMUM(...)* ROL * 2014-11-30 * P210101 *'

“************* *******************”

我想,当用户点击“管理产品”时。表格显示所有产品其中cod_loc类似'username%'

豁免:在这种情况下,用户是“P01”。在这个页面 - “管理Produtos”我想要显示所有产品,其中cod_loc以“P01”开头。

我尝试使用dataProvider。在模型 - >产品..但我不能显示我想要的!我可以显示cod_loc与用户名完全匹配的所有产品(例如“P01”),我可以显示cod_loc有“P”,“0”和“1”的所有产品,这种情况几乎都是产品。


嗯,在我的模型中,我有代码:

<?php

/**
 * This is the model class for table "produto".
 *
 * The followings are the available columns in table 'produto':
 * @property integer $idProduto
 * @property integer $cod_art
 * @property string $designacao
 * @property string $unidades
 * @property string $data_validade
 * @property string $cod_loc
 * @property string $username
 *
 * The followings are the available model relations:
 * @property User $username0
 */
class Produto extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'produto';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('cod_art, designacao, unidades, cod_loc', 'required'),
			array('cod_art', 'numerical', 'integerOnly'=>true),
			array('designacao', 'length', 'max'=>128),
			array('unidades', 'length', 'max'=>6),
			array('cod_loc', 'length', 'max'=>12),
			array('username', 'length', 'max'=>3),
			array('data_validade', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('idProduto, cod_art, designacao, unidades, data_validade, cod_loc, username', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	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(
			'username0' => array(self::BELONGS_TO, 'User', 'username'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			//'idProduto' => 'Id Produto',
			'cod_art' => 'Cod Art',
			'designacao' => 'Designacao',
			'unidades' => 'Unidades',
			'data_validade' => 'Data Validade',
			'cod_loc' => 'Cod Loc',
			//'username' => 'Username',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		//$criteria=new CDbCriteria;

		/*$criteria->compare('cod_arm',$this->cod_arm);
		$criteria->compare('descricao',$this->descricao,true);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('salt',$this->salt,true);
		$criteria->compare('tipo',$this->tipo,true);*/
                $username = Yii::app()->user->name;
		return new CActiveDataProvider('Produto', array(
                    'criteria'=>array(
                    'condition'=>'cod_loc LIKE :username%',
                    'params' => array(
                        ':username'=>$username
                    )
                ),
            ));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Produto the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}


在我的观点中&gt; Produto&gt; admin.php我有这段代码:

<?php
/* @var $this ProdutoController */
/* @var $model Produto */

$this->breadcrumbs=array(
	'Produtos'=>array('index'),
	'Manage',
);

$this->menu=array(
	array('label'=>'List Produto', 'url'=>array('index')),
	array('label'=>'Create Produto', 'url'=>array('create')),
);

Yii::app()->clientScript->registerScript('search', "
$('.search-button').click(function(){
	$('.search-form').toggle();
	return false;
});
$('.search-form form').submit(function(){
	$('#produto-grid').yiiGridView('update', {
		data: $(this).serialize()
	});
	return false;
});
");
?>

<h1>Manage Produtos</h1>

<p>
You may optionally enter a comparison operator (<b>&lt;</b>, <b>&lt;=</b>, <b>&gt;</b>, <b>&gt;=</b>, <b>&lt;&gt;</b>
or <b>=</b>) at the beginning of each of your search values to specify how the comparison should be done.
</p>

<?php echo CHtml::link('Advanced Search','#',array('class'=>'search-button')); ?>
<div class="search-form" style="display:none">
<?php $this->renderPartial('_search',array(
	'model'=>$model,
)); ?>
</div><!-- search-form -->
<?php echo $AP = Yii::app()->User->name; ?>
<?php $this->widget('zii.widgets.grid.CGridView', array(
	'id'=>'produto-grid',
	'dataProvider'=>$model->search(),
	'filter'=>$model,
	'columns'=>array(
		//'idProduto',
		'cod_art',
		'designacao',
		'unidades',
		'data_validade',
		'cod_loc',
		/*
		'username',
		*/
		array(
			'class'=>'CButtonColumn',
		),
	),
)); ?>

在mysql中,我可以做我想要的 - &gt; SELECT * FROM produto WHERE cod_loc LIKE'username%';

1 个答案:

答案 0 :(得分:0)

在代码中没有深入解释可能在这个代码示例中:

$username = 'P01' ;
Product::model()->findAll('cod_loc LIKE :username', array(
    ':username'=>"{$username}%"
));

或在数据提供者中:

$dataProvider = new CActiveDataProvider('Product', array(
    'criteria'=>array(
        'condition'=>'cod_loc LIKE :username',
        'params' => array(
            ':username'=>"{$username}%"
        )
    ),
));

Product我的意思是t_product表的模型类。