Yii Framework Forum: Same Id Integer For Two Tables - Yii Framework Forum

Jump to content

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

Same Id Integer For Two Tables Rate Topic: -----

#1 User is offline   Deram 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 48
  • Joined: 07-June 12

Posted 29 January 2013 - 12:28 PM

Hey guys, i have a single form for two different models. let's call the first model "Books" and the second model for "Category". The primary key in Category is called "bookId" and (funny enough) there's a relation between the id in Books and bookId in Category (Books id has one Category bookId).

My question is: How to add the same integer number to the two tables when i submit my form from /books/create ?
0

#2 User is offline   Aneesh Asokan 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 240
  • Joined: 26-September 12

Posted 29 January 2013 - 01:02 PM

View PostDeram, on 29 January 2013 - 12:28 PM, said:

Hey guys, i have a single form for two different models. let's call the first model "Books" and the second model for "Category". The primary key in Category is called "bookId" and (funny enough) there's a relation between the id in Books and bookId in Category (Books id has one Category bookId).

My question is: How to add the same integer number to the two tables when i submit my form from /books/create ?


In the create action, after saving first model, set the primary key of first model to the primary key second model.
Thanks
Aneesh.
0

#3 User is offline   Deram 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 48
  • Joined: 07-June 12

Posted 29 January 2013 - 03:23 PM

i'm kind of lost on how you want to write that code?

My initial thoughts were to do something in a before validate function... but i guess your way is more "clean". I just can't see how to write it
0

#4 User is offline   Hesam 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 66
  • Joined: 12-November 12
  • Location:Toronto

Posted 29 January 2013 - 05:04 PM

View PostDeram, on 29 January 2013 - 03:23 PM, said:

i'm kind of lost on how you want to write that code?

My initial thoughts were to do something in a before validate function... but i guess your way is more "clean". I just can't see how to write it


If I am not mistakes, you're collecting category name in the single form, as well. so here is the actionCreate
public function actionCreate()
	{
		$model=new Book;

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['Book']))
		{
			$model->attributes=$_POST['Book'];
			if($model->save()){
                                $id=$model->id;
                                $category=new Category;
                                $category->bookId=$id;
                                $category->name=... etc
                          
                                $this->redirect(array('view','id'=>$model->id));
                        }




0

#5 User is offline   Deram 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 48
  • Joined: 07-June 12

Posted 30 January 2013 - 11:27 AM

I've now tried what you suggested, but it didn't work. I also tried:
$genre->game_id=$_POST['Game']['id'];

but it didn't work either. That was a long shot anyway, since id for the table Game is auto increment, and therefore isn't posted in the form.
0

#6 User is offline   Hesam 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 66
  • Joined: 12-November 12
  • Location:Toronto

Posted 30 January 2013 - 11:44 AM

View PostDeram, on 30 January 2013 - 11:27 AM, said:

I've now tried what you suggested, but it didn't work. I also tried:
$genre->game_id=$_POST['Game']['id'];

but it didn't work either. That was a long shot anyway, since id for the table Game is auto increment, and therefore isn't posted in the form.


I tried it on my system and I worked. I guess you're data is not validate, so you can't save it. Have you checked you database and verify if the main model is saved and the autoincrement id works? If yes, everything should work fine. And yes that was a very long shot :)
0

#7 User is offline   Deram 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 48
  • Joined: 07-June 12

Posted 30 January 2013 - 11:48 AM

My problem is that i validate both the models at the same time in order for the error-summary to show for both the tables at the same time. It's therefore not possible for me to do it in exactly the same way as you do.
0

#8 User is offline   Hesam 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 66
  • Joined: 12-November 12
  • Location:Toronto

Posted 30 January 2013 - 11:56 AM

View PostDeram, on 30 January 2013 - 11:48 AM, said:

My problem is that i validate both the models at the same time in order for the error-summary to show for both the tables at the same time. It's therefore not possible for me to do it in exactly the same way as you do.


Perhaps that's causes the problem. If you have a foreign key in the second model, even if you somehow validate it, because foreign key constrain of the database you end up having problem. And before saving the first model, you won't be able to access to its autoincrement primary key. You may used a trick to get a maximum id value of the first model and add 1 to it, to get actual id, but it's not recommended. You may also paste you view, model code here, maybe sb else can help you.
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