SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,147
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)

    Supporting Composite Primary Keys for ActiveRecord

    Which one of these make for a better interface when initiating the ActiveRecord finder through object instantiation?

    PHP Code:
    /* 
    * 1 maps to first key in primary key array
    * 2 maps to second  in primary key array
    */
    new BlogCategory(1,5); 
    PHP Code:
    /*
    * mock named parameters so order isn't required
    */
    new BlogCategory('blog_id:1','category_id:5');

    or

    new 
    BlogCategory('category_id:5','blog_id:1'); 
    Simple Model Definition:
    PHP Code:
    class BlogCategory extends ActiveRecord {
         public static 
    $primaryKey = array('blog_id','category_id');
         public static 
    $foreignKeys = array(
              
    'blog_id'=>'Blog'
              
    ,'category_id'=>'Category'
         
    );

    I like the first for its simplicity, but then the primary key "order" must be remembered. The second is much more flexible, but yields a more complex interface.

    Any other ideas?

    thx

  2. #2
    SitePoint Addict Mastodont's Avatar
    Join Date
    Mar 2007
    Location
    Czech Republic
    Posts
    375
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Any other ideas?
    Yes - do not use AR for M:N relation binding table ... Do you really need extra AR for finder?

  3. #3
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    Plano
    Posts
    643
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i agree. instead, use AR for Blog, or even Category which will contain multiple Blogs through the BlogCategory relationship table.

    edit: but to answer your question, if i had to pick, i'd go with your first approach. the second seems less elegant, partially because you lose your type enforcement, which -- using java often -- i'm a fan of.

  4. #4
    Spirit Coder allspiritseve's Avatar
    Join Date
    Dec 2002
    Location
    Ann Arbor, MI (USA)
    Posts
    648
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by oddz View Post
    Which one of these make for a better interface when initiating the ActiveRecord finder through object instantiation?
    Either the first example, or:
    PHP Code:
    new BlogCategory(array(15)); 

  5. #5
    PHP/Rails Developer Czaries's Avatar
    Join Date
    May 2004
    Location
    Central USA
    Posts
    806
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Or you could do something like this, to match the definition:

    PHP Code:
    new BlogCategory(array('blog_id' => 1'cateogry_id' => 5)); 


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •