Welcome Guest, Not a member yet? Register   Sign In
IgnitedRecord 1.0 pre-release
#91

[eluser]croustibat[/eluser]
hi everybody,

First thanks m4rw3r for your work. That's really great.

I'm using the ORM class for 2 days, so sorry if i missed something i could find in the manual or in this thread, but i can't figure out how to fetch a related object with its own relationship.
I mean, if you do that :

Code:
$record = $this->artist->find($id);

so you get an "artist" object and then, you want to fetch its related "interviews" :


Code:
$interviews = $record->get_related('interviews');

the $interviews object is ok, but there's not the related object i define in the interview_model, even if i declare __auto_load_relationship = true.

do you have an idea to do that ?

hope I've been clear enough. thanks for your reply.
#92

[eluser]sophistry[/eluser]
yes, post your model code.
#93

[eluser]croustibat[/eluser]
hi sophistry,
here is my controller :

Code:
public function edit(){
...

// get artist record object
$record = $this->artist->find($id);
                        
$data['artist']['id']         = $id;
$data['artist']['name']       = $record->name;
$data['artist']['biography']  = $record->biography;

//fetch related table -- and here i would like interviews with sub-relation albums (which has been set in the interview model)
$interviews = $record->get_related('interviews');
        
$data['interviews'] = $interviews;
$this->load->view('admin/artists_form', $data);
}

my artist_model (only the properties) :

Code:
class Artist_Model extends IgnitedRecord {
    
    /**
     *Table Name
     */
    public $__table = 'artists';
    
    /**
     * Relationship
     */
    public $__has_many = array(
      array(
       'table'     => 'interviews',
       'col'       => 'artist_id',
      ),
      array(
       'table'     => 'photos',
       'col'       => 'artist_id',
      ),...

and my interview_model :

Code:
class Interviews_Model extends IgnitedRecord {
    /**
     *Table Name
     */
    public $__table = 'chroniques';
    
    /**
     * Relationship
     */    
    public $__auto_load_relationships = true;
    public $__belongs_to = array(
      array(
        'table'    => 'albums',
        'col'      => 'album_id'
      ),...

Is it possible to automatically get the album relation when i call the interviews relation from the artist record ?
#94

[eluser]sophistry[/eluser]
i'm not sure that i understand your data model... why are you relating albums belongs to interviews? and artists has many interviews? it is confusing.

are you trying to get all the interviews from an artist and then find the album that the interview "belongs to"? if this is your main way of getting interview data, (ie, you need the album info too) shouldn't your data model be "artists have many albums and albums have many interviews"... that seems simpler to me.

also, i found this in the user manual:
"(Warning, can cause endless loops because of a related table having $__auto_load_relationships switched on and relates back to this table)"

is this part of the problem?
#95

[eluser]croustibat[/eluser]
thanks sophistry for your answers.

Quote:are you trying to get all the interviews from an artist and then find the album that the interview “belongs to”

that's what i want to do. Maybe you're right, but my database has been designed with album_id as foreign key in the interviews table. so according to the IgnitedRecord manual, i should set in interview_model the __belongs_to relatoinship with albums. But i agree maybe that's not the best way.

but do you confirm me that it is possible to get multiple relationship by just calling $record->load_rel() or $record->get_related(tablename) ?

btw, i think the load_rel() or get_related() method should accept a $__depth parameter to specify how many relationship you want from a record.
#96

[eluser]m4rw3r[/eluser]
The load_rel() method just loads ONE level, but if the related objects autoload, they load their related objects.
So the responsibility for loading related objects lies only at load_rel() in the first instance, all the other "autoloaded" relations is loaded by their models so it is not possible with a depth parameter.

BTW. The whole relation system has been redesigned in the coming version 0.2.0.
And in 0.2.0 you would use join_related to "autoload" related objects with has one/belongs to relations.

@sophistry
I see that you have read your homework! Tongue
You response was correct, thanks.
#97

[eluser]Hannes Nevalainen[/eluser]
I can barely wait for next version! =)
#98

[eluser]croustibat[/eluser]
thanks m4rw3r for your answer.

I will see with the __auto_load.

And i'll wait for the 0.2.0.

great work man !
#99

[eluser]sophistry[/eluser]
you don't have to wait for 020... you can get it from SVN checkout at the assembla site. well... a pre-release copy anyway. ;-)

BTW, while you are there in the SVN make sure to check out the new IgnitedRecord documentation - it is a wonder to behold.

[eluser]Hannes Nevalainen[/eluser]
I never got the grip with SVN, which is a shame for me. (I'm studying to Software Engineer) =P

Think I'll have to wait untill the "zipped vesion" exists ^^

Thanks for the tip though




Theme © iAndrew 2016 - Forum software by © MyBB