【问题标题】:Yii Widgets CGridView show dataYii Widgets CGridView 显示数据
【发布时间】:2014-11-27 12:13:48
【问题描述】:

我的数据库中有 2 个表:t_productt_user

t_user

'********************************************** *****************************'

'* cod_arm * 描述 * 用户名 * 密码 * salt * tipo *'

'********************************************** *****************************'

'* 000 * 000 - 管理员 * 管理员 * 123 * asdfgh * A *'

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

'* 021 * 021 - 泌尿科 * 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”。在此页面-“管理产品”中,我想显示 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);
	}
}

在我看来>Produto>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 中,我可以做我想做的事 -> SELECT * FROM produto WHERE cod_loc LIKE 'username%';

【问题讨论】:

  • 请在您尝试方法的地方附上相关源代码

标签: yii widget where cgridview dataprovider


【解决方案1】:

没有深入了解您的代码,答案可能在此代码示例中:

$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 表的模型类。

【讨论】:

  • 对不起,但我是新手..我编辑我的问题..如果你能帮助我
  • 我编辑了我的问题..我认为我包含了相关的源代码..谢谢! @MauricioGracia
  • 现在我有这个 CDbexception:CDbCommand 未能执行 SQL 语句:SQLSTATE[42000]:语法错误或访问冲突:1064 Erreur de syntaxe près de '%' à la ligne 1. SQL 语句执行的是:SELECT COUNT(*) FROM produto t WHERE cod_loc LIKE :username%
  • 我通过将通配符 % 从条件移动到参数边界来更正了我的代码。现在试试。这只是一个可能的解决方案,我的代码没有在实际应用中测试。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-03-08
  • 1970-01-01
  • 1970-01-01
  • 2013-12-15
  • 1970-01-01
  • 1970-01-01
  • 2012-11-01
相关资源
最近更新 更多