/** * for a given set of relationships, * load them into the entity so find* functions return all requested related data * * make this a getter? It doesn't actually return the array, so keeping as load * * always load parent model(s) * * auto = do nothing * all = load all possible relationships * csv,list = load only these relationships * * @return bool|void */ public final function loadActiveRelationships() { // no need to run this multiple times if (!is_null($this->activeRelations)) { return; } $this->activeRelations = array(); $requestedRelationships = $this->searchHelper->getWith(); $parentModels = $this->model->getParentModels(false); $modelRelationships = $this->model->getRelations(); $all = false; // load all relationships? // process the private array of relationships switch ($requestedRelationships) { case 'none': $all = false; // gotta load parents if there are any if ($parentModels) { $requestedRelationships = $parentModels; } else { $requestedRelationships = array(); } break; case 'all': $all = true; break; // expect & process a csv string // expect & process a csv string default: // expect csv list or simple string // user_addrs,user_phones $requestedRelationships = explode(',', strtolower($requestedRelationships)); // include parents if there are any if ($parentModels) { $requestedRelationships = array_merge($parentModels, $requestedRelationships); } break; } if ($all) { // load all defined relationships regardless of what was requested foreach ($modelRelationships as $relation) { $modelName = $relation->getModelName(); $aliasName = $relation->getAlias(); // figure out if we have a preferred alias if ($aliasName) { $this->activeRelations[$aliasName] = $relation; } else { $this->activeRelations[$modelName] = $relation; } } } else { // foreach ($requestedRelationships as $requestedRelationship) { $matchFound = false; foreach ($modelRelationships as $relation) { $tableName = $relation->getTableName(); $modelName = $relation->getModelName(); $aliasName = $relation->getAlias(); // make sure the relationship is approved either as the table name, model name or ALL // table names because end point resources = table names // model name because some auto generated relationships use this name instead // alias is used to STORE the active relationship in case multiple relationships point to the same model // but it is not a valid way for a client to request data if ($requestedRelationship == $aliasName) { $matchFound = true; // figure out if we have a preferred alias if ($aliasName) { $this->activeRelations[$aliasName] = $relation; } else { $this->activeRelations[$modelName] = $relation; } break; } } if (!$matchFound) { foreach ($modelRelationships as $relation) { $tableName = $relation->getTableName(); $modelName = $relation->getModelName(); $aliasName = $relation->getAlias(); if ($requestedRelationship == $tableName) { $matchFound = true; // figure out if we have a preferred alias if ($aliasName) { $this->activeRelations[$aliasName] = $relation; } else { $this->activeRelations[$modelName] = $relation; } break; } } } else { continue; } if (!$matchFound) { // if you are still here, then you have a problem! // a relation ship was requested that doesn't actually exist } } } $this->afterloadActiveRelationships(); return true; }