SitePoint Sponsor

User Tag List

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

    ActiveRecord: Assigning and saving unsaved dependencies

    Given two models Blog and Category would it be right to assign a blog to a category that hasn't been created yet? Then when the save() method is called on the Blog the Category would be saved through the Blog by being inserted first. Then the Blogs foreign key would be set to the Categories primary key.

    PHP Code:
    $blog = new Blog();
    $blog['title'] = 'The Blogs Title';
    $blog['entry'] = 'entry for this blog...';
    $blog['created'] = time();
    $blog['category'] =  new Category(array('name'=>'vehicles'));
    $blog->save(); // category inserted then blog 
    The reason why I ask is because hypothetically in terms of a dependency the Category seems like it should exist before being assigned as a dependency to the Blog/Model which it would relate (shown below).

    PHP Code:
    $category = new Category();
    $category['name'] = 'vehicles';
    $category->save(); // inserted and assigned primary key

    $blog = new Blog();
    $blog['title'] = 'The Blogs Title';
    $blog['entry'] = 'entry for this blog...';
    $blog['created'] = time();
    $blog['category'] =  $category;
    $blog->save(); 
    One particular instance where this would be increasingly convenient is any one to one relationship.

    PHP Code:
    $profile Profile(array('email'=>'blah@blah.com'));
    $profile->user = new User(array('name'=>'oddz'));
    $profile->save(); // user would be saved before profile 

  2. #2
    SitePoint Addict Mastodont's Avatar
    Join Date
    Mar 2007
    Location
    Czech Republic
    Posts
    375
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why do you want to limit yourself only to 1-1 relationship?
    PHP Code:
    $blog['category'] =  new Category(array('vehicles''bicycles')); 
    the Category seems like it should exist before being assigned as a dependency to the Blog/Model which it would relate
    You can create non-existent categories in constructor of Category class.

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,182
    Mentioned
    16 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Mastodont
    Why do you want to limit yourself only to 1-1 relationship?
    Not limited to 1:1. The scenario presented merely represents a generic case.

    Quote Originally Posted by Mastodont
    You can create non-existent categories in constructor of Category class.
    The system is generic. The class files are only responsible for defining model and table information. Running queries, collecting queries, saving and deleting data. Nothing more. Everything else is controlled outside of the core library files. That includes the management of specific entities (Category, Blog, ect). Assigning a Category to a Blog is specific to the application not the generic purpose of the library.

  4. #4
    SitePoint Guru
    Join Date
    Jun 2006
    Posts
    638
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    make sure you can rollback, so if your "user" fails to save, you don't save the "profile" (or anything else that might be linked to it)

  5. #5
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,182
    Mentioned
    16 Post(s)
    Tagged
    4 Thread(s)
    If the user save were to fail the profile would not be saved.

    I decided that advantages of this made it worth allowing. However, the same thing could also be accomplished the other way around.

    PHP Code:
    $user = new User();
    $user->name 'oddz';
    $user->profile = new Profile(array('email'=>'blah@blah.com'));
    $user->save(); 
    Just nice to be able to accomplish it both ways I think.

  6. #6
    SitePoint Guru
    Join Date
    Jun 2006
    Posts
    638
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $user = new User();
    $user->name 'oddz';
    $user->profile1 = new Profile(array('email'=>'blah1@blah.com'));
    $user->profile2 = new Profile(array('email'=>'blah2@blah.com')); # failed
    $user->profile3 = new Profile(array('email'=>'blah3@blah.com'));
    $user->save(); 
    If profile2 fails, but save profile1 and profile3, you will have a problem.

  7. #7
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,182
    Mentioned
    16 Post(s)
    Tagged
    4 Thread(s)
    Well in that case all the profiles would be inserted in the same query. If that query fails the only data that would be inserted is the user currently. In the case which you provided only 2 queries will be executed not 4.


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
  •