SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast konsama's Avatar
    Join Date
    Jun 2007
    Location
    Sweden
    Posts
    95
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [ZF] How do you define a 1-to-1 relationship? - [SOLVED]

    I'm reading Rob Allens Zend Framework in Action book and he has examples for Many-to-Many, and in the below case a 1-to-Many relationship.
    But he never showed any examples on how to do a 1-to-1 relationship. Can somebody teach me how it should be done?



    1-to-Many example
    PHP Code:
    class Books extends Zend_Db_Table_Abstract 
    {
        protected 
    $_name 'books';
        protected 
    $_dependentTables = array('Reviews');
    }

    class 
    Reviews extends Zend_Db_Table_Abstract 
    {
        protected 
    $_name 'reviews';
        protected 
    $_referenceMap    = array(
            
    'Book' => array(
                
    'columns'           => array('book_id'),
                
    'refTableClass'     => 'Books',
                
    'refColumns'        => array('id')
            )        
        );


    Let's say that I want to connect Class Books with Class Genre, a 1-to-1 relationship. How do I determine which of the tables should use either one of these? Which table is depending on the other table?

    • $_dependentTables
    • $_referenceMap


    Do I still define their relationship exactly like how the 1-to-Many example is done?
    Last edited by konsama; Jul 31, 2009 at 13:56. Reason: [SOLVED]

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    One genre has many books... Though to map the two it'd be a case of mapping ID to ID.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    SitePoint Enthusiast konsama's Avatar
    Join Date
    Jun 2007
    Location
    Sweden
    Posts
    95
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    One genre has many books...
    Yeah you're right, now my memory is starting to come back to me. All I need is 1:M and N:M to make a basic database web application.

    The 1:1 relationship was only for the more advanced database theorists out there.
    I've been working 2-dimensionally for so long that I forgot how to think 4th-dimensionally. Like Doc says from "Back to the future".

    Thanks arkinstall!

    PHP Code:
    class Genre extends Zend_Db_Table_Abstract 
    {
        protected 
    $_name 'genre';
        protected 
    $_dependentTables = array('Books');
    }


    class 
    Books extends Zend_Db_Table_Abstract 
    {
        protected 
    $_name 'books';
        protected 
    $_referenceMap    = array(
            
    'Genre' => array(
                
    'columns'           => array('genre_id'),
                
    'refTableClass'     => 'Genre',
                
    'refColumns'        => array('id')
            )        
        );


  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,869
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    The simplest solution to a 1:1 is to put them into the one table. No second table required.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  5. #5
    SitePoint Enthusiast konsama's Avatar
    Join Date
    Jun 2007
    Location
    Sweden
    Posts
    95
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the tip now I'm a database theorist too


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
  •