Skip to content

Instantly share code, notes, and snippets.

@Mopkau
Created July 14, 2016 11:24
Show Gist options
  • Save Mopkau/904ee08a79c18ecc322e3ee007c4f422 to your computer and use it in GitHub Desktop.
Save Mopkau/904ee08a79c18ecc322e3ee007c4f422 to your computer and use it in GitHub Desktop.
LoadModel Yii1
public function loadModel($id, $class = false, $attributes = false, $prepare = true, $seo = true, $condition = '', $params = array())
{
if ($class === false) {
$class = $this->getModelClass();
}
/** @var ActiveRecord $finder */
$finder = ActiveRecord::model($class)->published();
if (is_array($id) && $attributes) {
$model = $finder->findByAttributes($id, $condition, $params);
} else {
$model = $finder->findByPk($id, $condition, $params);
}
if ($model === null) {
throw new \CHttpException(404, t('The requested page does not exist.'));
}
if ($prepare) {
$this->prepare($model);
}
if ($seo && $model->asa('seo') && $this->asa('seo')) {
$this->registerSEO($model);
}
return $model;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment