2

I'm using the Zend Framework database relationships for a couple of weeks now. My first impression is pretty good, but I do have a question related to inserting related data into multiple tables. For a little test application I've related two tables with each other by using a fuse table.

      +---------------+      +---------------+      +---------------+
      |     Pages     |      |     Fuse      |      |    Prints     |
      +---------------+      +---------------+      +---------------+
      | pageid        |      | fuseid        |      | printid       |
      | page_active   |      | fuse_page     |      | print_title   |
      | page_author   |      | fuse_print    |      | print_content |
      | page_created  |      | fuse_locale   |      | ...           |
      | ...           |      | ...           |      +---------------+
      +---------------+      +---------------+

Above is an example of my DB architecture

Now, my problem is how to insert related data to two separate tables and insert the two newly created ID's into the fuse table at the same time. If someone could could maybe explain or give me a topic related tutorial. I would appreciate it!

2 Answers 2

1

I assume you got separate models for each table. Then simply insert stuff in Prints table, store returned ID in variable. Then insert stuff in Pages table and store returned ID in another varialble. Eventually insert data in your Fuse table. You do not need any "at the same time" (atomic) operation here. ID of newly inserted rows are returned by save() (I assume you use autoincrement fields for this).

$printsModel = new Application_Model_Prints();
$pagesModel = new Application_Model_Pages();
$fuseModel = new Application_Model_Fuse();

$printData = array('print_title'=>'foo',
              ...);
$printId = $printsModel->insert( $printData );

$pagesData = array('page_author'=>'bar',
              ...);
$pageId = $pagesModel->insert($pagesData);

$fuseData = array('fuse_page' => $pageId,
                  'fuse_print' => $printId,
                  ...);
$fuseId = $fuseModel->insert($fuseData);

thus is pseudo code, so you may want to move inserts into your models and do somoe i.e. normalisation etc.

I also suggest paying more attention to fields naming convention. It usually helps and now you got fuseid but also fuse_page. So it either should be fuse_id or fusepage (not to mention I suspect this field stores id so it would be fuse_page_id or fusepageid).

Sign up to request clarification or add additional context in comments.

6 Comments

Ive used shorten field names for the example. All namings in my database are perfect. Thanks for the tip, I will look into it.
This works perfectly. But is it the right way to do :P ?... if not, I will keep it like this and update it later in time
Yes, it is perfectly fine. You do not need any atomic operation here (see en.wikipedia.org/wiki/Atomicity_%28database_systems%29) You just need to store some related data. and once you insert your page or print data, their ID are unaltered. So you can even create your fuse record next day - it still be fine as long as you remember your pageId and printId
Haha, I'm also trying the example below
I mainly meant "accept answer that helped", not "accept my answer". Many people asks but then do not "pay back" with points, which is annoying
|
0

Prints and Pages are two entities . Create row clases for each

class Model_Page extends Zend_Db_Table_Row_Abastract 
{

public function addPrint($print)
{
  $fuseTb = new Table_Fuse();
  $fuse = $fuseTb->createRow();
  $fuse->fuse_page = $this->pageid;
  $fuse->fuse_print = $print->printid;
  $fuse->save();
  return $fuse;
}

}

Now when you create page

$page = $pageTb->createRow() ; //instance of Model_Page is returned
$page->addPrint($printTb->find(1)->current());

6 Comments

Something isnt right. It gived me an error because of the $fuse->save() Fatal error: Call to undefined method Module_Model_ModulePagesFuses::save()
this is pseudo code , your class name will be different depending upon your autoloading namespace also you need to add $_rowClass = 'Model_Page' to your existing table class .
I've already done that. I didnt copy paste your code, but modified it to my application. But still gives me the save() error.
my bad forgot to create instance of fuse table class . now check
Ive used the example above. But will check this one aswell, Ill post the result in a minute :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.