关系mysql在yii中没有主键的表搜索(调用非整数字段)

时间:2018-05-26 06:38:37

标签: mysql yii

表一:

ar_codes
----------
ar_id (primary key),
act_id,
ar_code,
status

表二:

act_req_recv
----------
rcv_id (primary key),
act_id,
rcv_person,
status

现在,我需要在 act_req_recv 表的ar_code字段中找到 ar_codes 表中的rcv_person字段值。这两个表都有一个公共字段act_id,并且不是表的两个主键。

现在我可以通过普通的mysql scriptlike bellow命令找到它,其中$ actId携带该值。我怎样才能在Yii中找到这个值?

SELECT ar_code FROM ar_codes WHERE act_id=$actId

我试图通过模型的函数调用找到它。但由于该领域不是PK所以结果不会到来。

public static function getAR_code($actId) {
    $ActCode = Yii::app()->db->createCommand()
            ->select('ar_code')
            ->from('{{ar_codes}}')
            ->where('act_id=' . (int) $actId)
            ->queryAll();
    return $ActCode;
}

在函数运行时出现此错误:

  

[error] [php]数组到字符串的转换   (d:\ XAMPP \ htdocs中\框架\ yii1.1.19 \籽亿\部件\ CDetailView.php:240)

cDetail查看代码是:

array(
    'name' => 'actId',
    'type'=> 'raw',
    'value' => ActivityRecord::getAR_code($model->actId), 
    'htmlOptions' => array('style' => "text-align:left;"),
),

1 个答案:

答案 0 :(得分:0)

queryAll()返回行数组,因此它实际上是数组数组。类似的东西:

[
    ['ar_code' => 123],
]

如果要查询单个值(例如123),则应使用queryScalar() - 它将从第一行的第一列返回值:

public static function getAR_code($actId) {
    return Yii::app()->db->createCommand()
            ->select('ar_code')
            ->from('{{ar_codes}}')
            ->where('act_id=' . (int) $actId)
            ->queryScalar();
}