CodeIgniter Forums
Many-To-Many Relationships - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Many-To-Many Relationships (/showthread.php?tid=35746)



Many-To-Many Relationships - El Forum - 11-09-2010

[eluser]lcoon[/eluser]
I am new to OOP in PHP, and a search the Internet for solution involving many to many relationships inside the database. I understand the whole concept behind having a class relate to one database, for instance I have a many to many relationship between two classes named "delay" and "business". inside my models I have two classes one named delay and one in business and they handle reading and writing from the database very well. But when it comes to associations they're dumb, would I have to write another class to deal with just the associations on these two classes? For instance if I had a associate class, that just deals with associations between the business and delay model.

and how would one implements this inside a controller? If I were to create a new delay I would have to save that first before associating it with the business. But also to make sure that I only have one delay class representing one row.

If anyone knows a good resource on the web that deals with the situation please link it below. Most articles I've seen on the Internet deal with just the Database structure part of the situation and not the OOP aspect. Hopefully I'm making sense, it's hard to describe the problem I face and hopefully someone out there understands what I'm trying to say. LOL
Code:
class Business extends Model{
  //code omitted
}

class Delay extends Model{
  //code omitted
}

class Association extends Model{

      public function __construct($bus_obj, $delay_obj){
        //First Check if they are saved (have Primary ID)
        //Then SQL and association in the association table.
      }

      //other useful functions below.
}

//CONTROLLER --------------------------------------

      $business = new Business(1); //Primary ID To Pre-load
      $delay = new Delay(); //No ID -- New Record
      $delay->addFields( "field_name", "value" );

      $association = new Association($business, $delay);



Many-To-Many Relationships - El Forum - 11-10-2010

[eluser]WanWizard[/eluser]
Instead of trying to work this out for yourself, you could look at Datamapper, which takes care of all of this for you.


Many-To-Many Relationships - El Forum - 11-10-2010

[eluser]lcoon[/eluser]
Thanks WanWizard. I appreciate the reply. I'll look at it right away.