Skip to content

Instantly share code, notes, and snippets.

@adroitcode
adroitcode / yii_model_relations_to_array.php
Last active August 29, 2015 14:10
Yii model to associative array with specified relations
//Put these two functions in your Controller.php
//and use like this:
//$department = Department::model->findBySql("SELECT * FROM department WHERE department_id=" . $department_id);
//$department_data = $this->get_model_associations($department,array('school'=>array('admins','pictureFile')),'students'=>array('pictureFile'));
//The first parameter is the model object, and the 2nd is an array of arrays of arrays and deep as you want to go.
//walk model detects if the current level is an associative array or just a regular array. If it is associative,
//it recursively gets the next layer of relations and then properly nests the data