关系 mysql Table 在 yii 中无主键搜索(调用非整数字段)

Relational mysql Table search without primary key in yii (calling a non integer field)

Table一个:

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

Table二:

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

现在我需要从 ar_codes table 中为 rcv_person 字段找到 ar_code 字段值35=]act_req_recvtable。两个 table 都有一个公共字段 act_id 并且不是两个 table 的主键。

现在我可以通过正常的 mysql 类似脚本的波纹管命令找到它,其中 $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] Array to string conversion (D:\Xampp\htdocs\framework\yii1.1.19\zii\widgets\CDetailView.php:240)

cDetail 查看代码为:

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

queryAll() returns行数组,所以它实际上是数组数组。类似于:

[
    ['ar_code' => 123],
]

如果你想查询单个值(例如123),你应该使用queryScalar()——它将return第一行第一列的值:

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