Yii Framework Forum: Cdbexception: Missing Table In 2 Model Update - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Cdbexception: Missing Table In 2 Model Update Rate Topic: -----

#1 User is offline   jkofsky 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 527
  • Joined: 17-May 10
  • Location:Pensacola, Florida

Posted 20 August 2013 - 02:01 AM

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.
Do, or do not. There is no 'try.' Jedi Master Yoda
0

#2 User is offline   codesutra 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 596
  • Joined: 15-March 11
  • Location:India

Posted 20 August 2013 - 02:11 AM

        /**
         * @return string the associated database table name
         */
        public function tableName() {
                return '{{profiles}}';
        }


Here is the mistake your model class is returning table name as "profiles" which should be "profile" as i can see this.

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)


You have created this model class manually or by Gii tool. If you have created it by gii tool then i dont think this error should come here.

Anyways lets try to do backup first for you profile model and create again model class for tbl_profile by gii tool and lets see the difference.
CodeSutra
0

#3 User is offline   jkofsky 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 527
  • Joined: 17-May 10
  • Location:Pensacola, Florida

Posted 20 August 2013 - 02:44 AM

View Postcodesutra, on 20 August 2013 - 02:11 AM, said:

        /**
         * @return string the associated database table name
         */
        public function tableName() {
                return '{{profiles}}';
        }


Here is the mistake your model class is returning table name as "profiles" which should be "profile" as i can see this.

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)


You have created this model class manually or by Gii tool. If you have created it by gii tool then i dont think this error should come here.

Anyways lets try to do backup first for you profile model and create again model class for tbl_profile by gii tool and lets see the difference.

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).
Do, or do not. There is no 'try.' Jedi Master Yoda
0

#4 User is offline   codesutra 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 596
  • Joined: 15-March 11
  • Location:India

Posted 20 August 2013 - 03:57 AM

View Postjkofsky, on 20 August 2013 - 02:44 AM, said:

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 :unsure:
CodeSutra
0

#5 User is offline   jkofsky 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 527
  • Joined: 17-May 10
  • Location:Pensacola, Florida

Posted 21 August 2013 - 02:49 PM

View Postcodesutra, on 20 August 2013 - 03:57 AM, said:

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 :unsure:

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'm still at a loss :(

p.s. using Yii 1.1.13 (Dec 20, 2012)
Do, or do not. There is no 'try.' Jedi Master Yoda
0

#6 User is offline   Ankit Modi 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 867
  • Joined: 19-February 13
  • Location:India

Posted 22 August 2013 - 01:36 AM

i think yoy may try this

1)
if (isset($_POST['User'], isset($_POST['Profile'])))



if first code is not working please second one try..


2)
if(isset($_POST['Users']) && !empty($_POST['Users'])){
    $userModel->attributes = $_POST['User'];
    $userModel->save(false);

    $profileModel->user_id = $userModel->id;
    $profileModel->attributes = $_POST['Profile'];
    $profileModel->save(false);

    if($userModel->hasErros){
      //redirect page
    }



}


i hope it's works...
Thanks,
Ankit Modi
Skype : amodi06
0

#7 User is offline   jkofsky 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 527
  • Joined: 17-May 10
  • Location:Pensacola, Florida

Posted 23 August 2013 - 03:13 PM

View PostAnkit Modi, on 22 August 2013 - 01:36 AM, said:

i think yoy may try this

1)
if (isset($_POST['User'], isset($_POST['Profile'])))



if first code is not working please second one try..


2)
if(isset($_POST['Users']) && !empty($_POST['Users'])){
    $userModel->attributes = $_POST['User'];
    $userModel->save(false);

    $profileModel->user_id = $userModel->id;
    $profileModel->attributes = $_POST['Profile'];
    $profileModel->save(false);

    if($userModel->hasErros){
      //redirect page
    }



}


i hope it's works...


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?
Do, or do not. There is no 'try.' Jedi Master Yoda
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users