Not signed in (Sign In)
Welcome Guest!
Want to take part in these discussions? If you have an account, sign in now.
If you don't have an account, apply for one now.
    • CommentAuthorJCC
    • CommentTimeJul 2nd 2008
     
    Hi,

    I have a model A that implements the "belongs_to" property B.

    I.e.
    A.id
    A.b_id

    B.id
    B.name

    When B exists load() works fine, but if b_id is NULL then load() doesn't tell me.

    I.e.

    A.B.load()->name works and if A.b_id is null then A.B.load()->name produces "Undefined property: AkAssociatedActiveRecord::$name"

    How can I tell if load() actually loaded an object or not?

    Also, calling load() seems quite inefficient. Can I load B automatically when A loads from model As $belongs_to definition? (i.e. eager loading)

    Kind regards,

    John
    •  
      CommentAuthorfrancois
    • CommentTimeJul 3rd 2008
     
    Hi JCC,

    The load method return false when no association object is found. Just check this condition

    if(!$objectAssociatedB = $A->B->load()){ treat error }

    Francois
    • CommentAuthorKaste
    • CommentTimeJul 8th 2008
     

    Also, calling load() seems quite inefficient. Can I load B automatically when A loads from model As $belongs_to definition?

    yes, you can.

    $A->find('all',array('include'=>'B'));
    
Add your comments
    Username Password
  • Format comments as