yiicactiverecord

Changing specific symbol of YII cactiverecord model's attribute


How to change specific symbol of YII cactiverecord model's attribute ? Dont understand why it doesnt work:

echo $model->attr; // aaa
$model->attr[1] = 'b';
echo $model->attr; // aaa

Solution

  • Use substr_replace function:

    echo $model->attr; // aaa
    $model->attr = substr_replace($model->attr, 'b', 1, 1);
    echo $model->attr; // aba
    

    http://www.php.net/manual/en/function.substr-replace.php

    Also you can use this approach:

    $newValue = $model->attr[1] = 'b';
    $model->attr = $newValue;
    echo $model->attr; // aba
    

    Your example does not work because actually $this->AttributeName execute CActiveRecord::getAttribute('AttributeName') method and not affect original value.