Hi all,
A beginner’s question from someone new to OOP. In my app, users can sign up with just an email and a password. However, some content is only visible to those who provided us with more info. Hence, I need to test in the database whether all user fields are filled out. To do so, I created a method in the model, which is called from the controller. This method in the model needs to access a function from the controller and I don’t know how to access it.
$model->loadModel(); doesn’t work. $this->loadModel(); obviously doesn’t work because $this refers to the instance of the model.
Any help would be much appreciated. Cheers…
Method in model User:
// tests whether all required fields of a table $testAllDataProvidedTable are set
// used by actionIndex of UserController
public function testAllDataProvided($testAllDataProvidedTable)
{
// needs to be loaded so $model->[required field] can be loaded in array $requiredColumns
$model = $this->loadModel(Yii::app()->user->id);
// set intial value of variable, will be set to 0 if one field is empty
$allDataProvided = 1;
// set required fields for tables
if ($testAllDataProvidedTable == "tbl_user") {
$requiredColumns = array(
$model->tbl_user_salutation_id,
$model->first_name,
$model->last_name,
$model->address_1,
$model->address_2,
$model->postcode,
$model->city,
$model->tbl_state_id,
$model->phone,
$model->email,
$model->tbl_user_interest_id
);
}
// iterate through each field and if one is empty, set $allDataProvided to 0
foreach ($requiredColumns as $value) {
if (empty($value)) {
$allDataProvided = 0;
}
}
// break the reference with the last element, http://php.net/manual/en/control-structures.foreach.php
unset($value);
// return value of fucntion
if ($allDataProvided == 1) {
return true;
} else {
return false;
}
}
}
UserController:
/** This is where user gets redirected after login
* Welcome screen after logging in
* @param integer $id the ID of the model to be displayed
*/
public function actionIndex()
{
// load all attributes and methods from db / model User
// by using the ID set in UserIdentity.php
// to see how to overwrite Yii::app()->user->id with id instead of email, see Agile-Web-Application-Development-with-Yii11-and-PHP5-eBook09022011_1063898.pdf p. 165
$model = $this->loadModel(Yii::app()->user->id);
// for testing whether all required fields in tbl_user have been filled out as a requirement for selling / buying
// $model->$allUserDataProvided is declared in model User and set by actionBuy() and actionSell() in UserController
// by calling method testAllDataProvided in UserController
$model->allUserDataProvided = $model->testAllDataProvided("tbl_user");
$this->render('index',array(
'model'=>$model,
));
}
/**
* Returns the data model based on the primary key given in the GET variable.
* If the data model is not found, an HTTP exception will be raised.
* @param integer the ID of the model to be loaded
*/
public function loadModel($id)
{
$model=User::model()->findByPk((int)$id);
if($model===null)
throw new CHttpException(404,'The requested page does not exist.');
return $model;
}