Yii2 ArrayHelper :: toArray递归不起作用

时间:2016-06-29 13:03:51

标签: php arrays yii2 helper

Yii2 ArrayHelper的辅助方法toArray不会转换嵌套对象。

这是我的测试代码。

if ($hal == 'kelolaagenda') {
    $judul = $_POST['judul'];
    $gambar = $_FILES['gambar'];
    $tempat = $_POST['tempat'];
    $alamat = $_POST['alamat'];
    $waktu_awal = $_POST['mulai'];
    $waktu_akhir = $_POST['akhir'];
    $id = $_GET['id'];

    if ($aksi == 'tambah') {
        if (empty($gambar['tmp_name'])) {
            $sql = query("insert into agenda (judul_agenda, tempat, alamat, waktu_awal, waktu_akhir) values ('$judul','$tempat','$alamat','$waktu_awal','$waktu_akhir')");
        } else {
            $namafile = uploadBerkas($gambar);
            $sql = query("insert into agenda (gambar_agenda, judul_agenda, tempat, alamat, waktu_awal, waktu_akhir) values ('$namafile','$judul','$tempat','$alamat','$waktu_awal','$waktu_akhir')");
        }
        if ($sql == TRUE) {
            echo"<script> alert('Agenda Telah Ditambahkan'); "
            . "window.location='agenda.php';"
            . "</script>";
        } else {
            echo "Sql Error " . mysql_error() . " ";
            echo"<script> alert ('Proses Gagal'); "
            . "history.back();"
            . "</script>";
        }
    }
    if ($aksi == 'hapus') {
        $sql = query("delete from agenda where id_agenda='$id'");
        if ($sql == TRUE) {
            echo"<script> alert('Agenda Telah Dihapus'); "
            . "window.location='agenda.php';"
            . "</script>";
        } else {
            echo"<script> alert ('Proses Gagal'); "
            . "history.back();"
            . "</script>";
        }
    }
    if ($aksi == 'edit') {
        if (empty($gambar['tmp_name'])) {
            $sql = query("Update agenda set judul_agenda = '$judul', tempat = '$tempat', alamat = '$alamat', waktu_awal = '$waktu_awal', waktu_akhir = '$waktu_akhir' where id_agenda ='$id'");
        } else {
            $namafile = uploadBerkas($gambar);
            $sql = query("Update agenda set gambar_agenda = '$namafile', judul_agenda = '$judul', tempat = '$tempat', alamat = '$alamat', waktu_awal = '$waktu_awal', waktu_akhir = '$waktu_akhir' where id_agenda ='$id'");
        }
        if ($sql == TRUE) {
            echo"<script> alert('Agenda Telah Diedit'); "
            . "window.location='agenda.php';"
            . "</script>";
        } else {
            echo"<script> alert ('Proses Gagal'); "
            . "history.back();"
            . "</script>";
        }
    }
}

默认情况下启用递归属性。

  

public static array toArray($ object,$ properties = [],$ recursive =   真)

所以这段代码应该正常工作,但事实并非如此。

Action返回没有public function actionTest() { $product = \common\models\Product::find() ->where(['id' => 5779]) ->with('firstImage') ->one(); $product = \yii\helpers\ArrayHelper::toArray($product); print_r($product); } 对象的一个​​级别数组。

我在这里做错了什么?

PS: 为了测试目的,简化了代码。我知道在这种情况下,可以简单地使用firstImage方法获取数组中的AR记录。

1 个答案:

答案 0 :(得分:3)

你应该改用:

$product = \common\models\Product::find()
    ->where(['id' => 5779])
    ->with('firstImage')
    ->asArray()
    ->one();

详细了解Retrieving Data in Arrays

如果你真的想使用toArray(),并且由于关系实际上不是属性或属性,你应该只使用第二个参数,例如:

$product = \yii\helpers\ArrayHelper::toArray($product, [
    'common\models\Product' => [
        // add needed properties here
        // ...
        'firstImage',
    ],
]);

或者,如果您使用的是REST,则可以覆盖模型中的extraFields()

public function extraFields()
{
    return ['firstImage'];
}

详细了解REST fields