In this tutorial we will modify the tables and classes set up in RocketSled Tutorial 4 to use a many to many relationship between SugaryPastry and Ingredient.
$ echo "DROP TABLE ingredient;" | mysql -u user --password=dbpass pastries $ echo "CREATE TABLE ingredient (ingredient_id int(6) NOT NULL PRIMARY KEY auto_increment, ingredient_name varchar(50) NOT NULL);" | mysql -u user --password=dbpass pastries $ echo "CREATE TABLE pastry_ingredient (pastry_id int(6) NOT NULL, ingredient_id int(6) NOT NULL, PRIMARY KEY(pastry_id,ingredient_id));" | mysql -u user --password=dbpass pastries
<?php class PastryIngredient extends DbRelationshipObject { function PastryIngredient() { $this->DbRelationshipObject(); } } ?>
<?php class SugaryPastry extends MyDbObject { public function SugaryPastry() { parent::MyDbObject('SugaryPastry'); $this->join('Ingredient','PastryIngredient'); } public function toString() { return $this->get('pastry_name'); } } ?>
<?php class Ingredient extends MyDbObject { public function Ingredient() { parent::MyDbObject('Ingredient'); $this->join('SugaryPastry','PastryIngredient'); } public function toString() { return $this->get('ingredient_name'); } } ?>
Note that we have changed the depend() and support() lines to join(), where the first argument of DbObject::join() is the class we are joining to and the second is the class/table which the join uses.
public function performHandlerTasks() { if(Application::formPosted()) { $form = Form::load('my.NewPastry'); if ($form->validate()) { $p = new SugaryPastry(); $p->parse(); foreach (explode("\n",Application::param('ingredients')) as $ingredient) { $ingredient = trim($ingredient); $i = new Ingredient(); $i->clauseSafe('ingredient_name',$ingredient); if(!$i->id()) $i->setSafe('ingredient_name',$ingredient); $p->add($i); } $p->save(); Application::redirect('ListPastries'); } } }
We can use clauseSafe() to check if an Ingredient with this ingredient_name already exists, if not we add the Ingredient. The add() method takes care of the other required operations, based on the join()s we set up in the Ingredient/SugaryPastry classes. The ListPastries class/RSML files do not require changes.
1.5.7