I keep getting the following everytime I try to create my first record in the User model.
CDbException
CDbCommand failed to execute the SQL statement: SQLSTATE[42S02]: Base table or view not found: 1146 Table
'adxc.tbl_profile' doesn't exist. The SQL statement executed was: INSERT INTO `tbl_users` (`role`, `is_active`,
`first_name`, `last_name`, `email`, `username`, `password_salt`, `password`, `date_created`) VALUES (:yp0, :yp1,
:yp2, :yp3, :yp4, :yp5, :yp6, :yp7, :yp8)
‘adxc.tbl_profile’ indeed does not exsist, but in the Profile model:
/**
* @return string the associated database table name
*/
public function tableName() {
return '{{profiles}}';
}
The UserController contains:
/**
* Creates a new model.
* If creation is successful, the browser will be redirected to the 'view' page.
*/
public function actionCreate() {
$userModel = new User;
$profileModel = new Profile;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if (isset($_POST['User'], $_POST['Profile'])) {
// Populate the models with the inpute data
$userModel->attributes = $_POST['User'];
$profileModel->attributes = $_POST['Profile'];
// You must validate BOTH prior to saving to get validation errors
$isValid = $userModel->validate();
$isValid = $profileModel->validate() && $isValid;
//
if ($isValid) {
// Use the false parameter to disable re-validation on save()
$userModel->save(false);
$profileModel->user_id = $userModel->id;
$profileModel->save(false);
//Redirect to the 'view' page
$this->redirect(array('user/view', 'id' => $userModel->id));
} // EndIf ($isValid)
} //EndIf (isset())
I’m at a loss. Can anyone point me in the right direction? Thanks.
There IS a tbl_profiles in the db. I used gii to generate the models. The db has plural names (ie: tbl_users, tbl_profiles), but the models have singular names (ie: User, Profile)
I can’t figure out where the query is GETTING the ‘adxc.tbl_profile’ instead of ‘adxc.tbl_profiles’. Oh and BTW this error is generated in a $userModel->save(false) call NOT a $profileModel->save(false).
Please check any relation define in User model class for Profile.Might there is something wrong.
i think you should check CativeRecords class. How they are mapping tables. Usually it convert tbl_profile to "Profile" model and if this is case then Model class should be "Profiles".
Check this link link 1
As Guidelines given it should also work perfectly with plural names.
Please let me know what you have figure out here.Since, its something strange thing is happening here
I check the relations in the User & Profile models.
I read the link. I like to have plural names in the db, because it is a list of userS not user. Having read a number of post about standard name, I figured that the model could be singular with the table as plural. In gii I change the model name to singular, then in the generated model file I want and changed the relations to singular. It seemed everything worked fine until I tried to put two(2) models in a single form.
I will try your suggestions when I get back. I did try commennting out EVERYTHING to do with the Profile model and still had the problem. Do you think it could be how AR gets the data from a relation being different from the model?