2017-05-31 07:10:40 4755次浏览 3条回答 0 悬赏 10 金钱

对数据库查询操作时如果只用find,那么代码没问题,如果使用了where语句后报错Array to string conversion
代码如下:

public function actionMajor()
    {
        $this->layout = "equipment";

        $entname = YII::$app->request->get('name');
        **$query = EquipmentArchives::find()->where(['entname' => $entname]);**
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'pagination' => [
                // 每个分页15条记录
                'pagesize' => 15
            ]
        ]);
        $data = array('dataProvider' => $dataProvider);

        return $this->render('major', $data);
    }

EquipmentArchives是一个对应数据库表的类,字段entname没有问题。

补充于 2017-05-31 12:40

这个是EquipmentArchives类的代码:

<?php

namespace app\models;


use yii\db\ActiveRecord;
use yii\helpers\HtmlPurifier;

/**
 * This is the model class for table "ledger".
 *
 * @property string $id
 * @property integer $eid
 * @property string $city
 * @property string $entname
 * @property string $ent_type
 * @property string $name
 * @property string $type
 * @property string $io
 * @property string $model
 * @property string $_level
 * @property string $_range
 * @property string $vendor
 * @property string $serial
 * @property string $managenum
 * @property string $place
 * @property string $note
 * @property string $rec_time
 * @property string $status_time
 * @property string $jdtime
 * @property integer $jdcycle
 */
class EquipmentArchives extends ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'ledger';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['eid', 'city', 'entname', 'ent_type', 'name', 'type', 'io', 'model', '_level', '_range', 'vendor', 'serial', 'managenum', 'place', 'rec_time', 'status_time', 'jdtime', 'jdcycle'], 'required'],
            [['eid', 'jdcycle'], 'integer'],
            [['rec_time', 'status_time', 'jdtime'], 'safe'],
            [['city', 'ent_type', 'name', 'type', 'io', 'model', '_level', '_range', 'vendor', 'serial', 'managenum', 'place'], 'string', 'max' => 50],
            [['entname'], 'string', 'max' => 200],
            [['note'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => '编号',
            'eid' => 'Eid',
            'city' => '所在城市',
            'entname' => '企业名称',
            'ent_type' => '组织规模',
            'name' => '计量器具名称',
            'type' => '器具类别',
            'io' => '输入输出用能',
            'model' => '型号规格',
            '_level' => '准确度等级',
            '_range' => '测量范围',
            'vendor' => '生产厂家',
            'serial' => '出厂编号',
            'managenum' => '用能单位管理编号',
            'place' => '安装使用地点',
            'note' => '备注',
            'rec_time' => '录入时间',
            'status_time' => '状态发生时间',
            'jdtime' => '检定时间',
            'jdcycle' => '检定周期',
        ];
    }

    public static function findById($id)
    {
        return static::findOne(['id'=>$id]);
    }

    public function add()
    {
        $record = new EquipmentArchives();

        $record->eid = HtmlPurifier::process($this->eid);
        $record->city = HtmlPurifier::process($this->city);
        $record->entname = HtmlPurifier::process($this->entname);
        $record->ent_type = HtmlPurifier::process($this->ent_type);
        $record->name = HtmlPurifier::process($this->name);
        $record->type = HtmlPurifier::process($this->type);
        $record->io = HtmlPurifier::process($this->io);
        $record->model = HtmlPurifier::process($this->model);
        $record->_level = HtmlPurifier::process($this->_level);
        $record->_range = HtmlPurifier::process($this->_range);
        $record->vendor = HtmlPurifier::process($this->vendor);
        $record->serial = HtmlPurifier::process($this->serial);
        $record->managenum = HtmlPurifier::process($this->managenum);
        $record->place = HtmlPurifier::process($this->place);
        $record->rec_time = HtmlPurifier::process($this->rec_time);
        $record->status_time = HtmlPurifier::process($this->status_time);
        $record->jdtime = HtmlPurifier::process($this->jdtime);
        $record->jdcycle = HtmlPurifier::process($this->jdcycle);
        $record->note = HtmlPurifier::process($this->note);

        return $record->save();
    }
}
补充于 2017-06-01 12:18

写这么清楚还是没有人回答?

最佳答案

  • 晦涩de咚 发布于 2017-06-01 17:29 举报

    执行前先打印一下你的sql;方法:$query->createCommand()->getRawSql(),在数据库里执行下看看对不对呢?

    1 条回复
    回复于 2017-06-16 21:01 回复

    这个问题我解决了,谢谢您~
    对啦,我想问一下,当我们就这么使用AR访问数据库,yii框架是否默认自带了连接池呢?

  • 回答于 2017-05-31 08:35 举报

    EquipmentArchives::find()->where(['entname' => $entname])->asarray()->all();符合条件的所有数据列并转为数组
    EquipmentArchives::find()->where(['entname' => $entname])->asarray()->one();符合条件的第一行数据列并转为数组
    EquipmentArchives::find()->where(['entname' => $entname])->all();没有asarray默认取出来的是对象

    1 条回复
    回复于 2017-05-31 12:35 回复

    你说的这三个解决不了问题的,我是需要分页,后面gridview需要实例化的对象

  • 回答于 2017-05-31 08:57 举报

    当前你贴出的代码没有错误的地方.应该是你views/major文件有错误的地方.可以再检查一下,或者在贴出那边的代码.
    同时,错误提示上应该有错误位置的提示.可以关注下错误位置.

    1 条回复
    回复于 2017-05-31 12:36 回复

    下面的是major的前端代码

    <?php
    
    $this->title = '主要用能计量器具管理';
    ?>
    
    <form class="form-inline" role="form" method="get" action="<?php echo \yii\helpers\Url::toRoute('equipment-archives/major'); ?>">
        <input type="hidden" name="r" value="equipment-archives/major">
        <div class="form-group">
            <label class="sr-only" for="key">名称</label>
            <input type="text" class="form-control" id="key" name="key" placeholder="请输入查询的关键字">
        </div>
        &ensp;
        <button type="submit" class="btn btn-success glyphicon glyphicon-search">搜索</button>
    </form>
    <br/>
    
    <?php
        \yii\helpers\Html::a('<span class="	glyphicon glyphicon-plus btn btn-success btn-sm">新增</span>', \yii\helpers\Url::toRoute('equipment-archives/add'), ['target' => '_blank']);
    ?>
    
    <?php
    echo \yii\grid\GridView::widget([
        'dataProvider' => $dataProvider,
        // rowOptions可以设置每行的id,class,width,height等,此处设置每行的id,id由拼接而成,$model是会自动获取传递
        // 过来的dataProvider的query中的一个对象(查询结果的每一行,每个对象将在gridview显示一行)。由于ID的唯一性
        // 每行tr的id都是唯一的。
        'rowOptions' => function ($model) {
            return [
                'id' => 'tr-' . $model->id,
            ];
        },
        'columns' => [
            [
                // 此处增加一列复选框
                'class' => 'yii\grid\CheckboxColumn',
                // 对整个checkbox列都设置class=check。
                'cssClass' => 'check',
                // ---------额外的知识----------
                // 1 如果要使得该列不可见,必须设置‘visible’ => false,因为该属性默认是true。
                // 2 如果想设置该列不要排序,可以设置'enableSorting' => false,因为该属性默认为true。
                // 3 header也可以设置列名的名称,比如:'header' => '选中',并且header默认是不排序的。另外,header
                //   支持对html的解析并展示。
                // 4 label是列名的默认形式 -> 默认排序,不支持对html的解析。不管是使用header还是label,都需要指明
                // 对那个属性进行设置:'attribute' => ‘数据库表字段名’。
            ],
            // 这些属性会自动关联到User表,并且展示时会显示每个属性对应的Label。
            'id',
            'name',
            'type',
            'entname',
            'model',
            [
                'attribute' => 'jdtime',
                'header' => '检定状态',
                'value' => function ($model) {
                    return  date('Y-m-d', strtotime("$model->jdtime + $model->jdcycle + 1 year")) > date("Y-m-d", time())? '已检定' : '待检定';
                }
            ],
            [
                'class' => 'yii\grid\ActionColumn',
                'header' => '操作',
                'template' => '{update} {delete}',
                'buttons' => [
                    'update' => function ($url, $model, $key) {
                        return \yii\helpers\Html::a('<span class="glyphicon glyphicon-pencil btn btn-success btn-xs"></span>', \yii\helpers\Url::toRoute('equipment-archives/update') . "&name={$model->id}", ['target' => '_blank']);
                    },
                    'delete' => function ($url, $model) {
                        return "<button class='btn-delete btn btn-danger btn-xs' data-url='" . YII::$app->urlManager->createUrl(['/equipment-archives/delete', 'id' => $model->id]) . "'><span class='glyphicon glyphicon-trash'></span></button>";
                    },
                ]
            ]
        ],
        // 当没有查询到结果时,显示这条消息
        'emptyText' => '平台还没有创建用户',
        // 当没有查询到结果时,对显示的消息设置css
        'emptyText' => ['style' => 'color:red;font-weight:bold'],
        // 去掉gridview显示在左上方的类似“Showing 1-1 of 2 items.”的内容
        'layout' => "{items}\n{pager}",
        // 如果没有查询到结果,那么整个gridview连表的标题都不会显示。
        'showOnEmpty' => false,
    ]);
    
您需要登录后才可以回答。登录 | 立即注册
Sanlence
助理

Sanlence

注册时间:2017-04-10
最后登录:2021-07-24
在线时长:5小时48分
  • 粉丝0
  • 金钱115
  • 威望0
  • 积分165

热门问题