Quick little thing.

Was sad to see Yii2’s getOldAttributes() did not have to ability to limit based on a provided array; whereas getAttributes() does take an array to limit the returned attributes. So I whipped this up right quick:

/**
 * @param array $array
 *
 * @return array
 */
public function getOldAttributes($array = [])
{
    $returnData = parent::getOldAttributes();

    if (!empty($array)) {
        $returnData = array_intersect($array, $returnData);
    }

    return $returnData;
}

Simple little thing but super handy.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.