Frage

I have such scheme of tables: enter image description here

My task is to get the event's description string from third table via relations, which look so:

'dates'=>array(self::HAS_MANY, 'CalendarDates', '', 
                                "on" => '"t"."CalendarDateId"="dates"."calendar_date_id"'),

'events'=>array(self::HAS_MANY, 'CalendarDayEvents', '', 
                                "on" => '"dates"."list_events" = "events"."date_id"')

So, I consider, that getting datesfrom first relation

$UserCalendar =  CalendarsUsers::model()->
                    with(array('dates','events'))->
                    find('user_id =:user_id', array(':user_id'=> $userId));

I would get the such scheme

Date as Dates

and list of events fron third table, which is linked to every Date

But using the relation, which is presented above, I'm getting "parallel" relation, where dates and events are "parallel", so I can't write code like this:

<?php foreach ($calendar->dates as $day): ?>

       <b>Date:</b> <?php echo($day->date_event); ?> <br>
       <i>Event:</i> 
            <?php foreach ($day as $event): ?>
                <?php echo($event->event_description); ?>
            <?php endforeach; ?>
            <br/>        
<?php endforeach; ?>

which returns me error as expected.

War es hilfreich?

Lösung

As a suggestion try the following modification. As I do not know your accurate tables name, I call them first, second and third relatively.

In your first model, put the following relation:

'second' => array(self::HAS_MANY, 'SECOND_TABLE_MODEL', 'FIRST_TABLE_KEY_WHICH_IS_RELATED_TO_SECOND')

In your second model, put the following relation:

'third' => array(self::HAS_MANY, 'THIRD_TABLE_MODEL', 'SECOND_TABLE_KEY_WHICH_IS_RELATED_TO_THIRD')

now, let's skip the third table relations. I consider you only want to access the description in third table. Then, if you do like bellow:

$firstRecords=First::model()->findAll();

You have all records from your First table. You do not need to use with here. to access the second table data, which is related to first table you can do like below:

foreach($firstRecords as $fr){
    //$fr->second->ATTRIBUTE_NAME
}

Finally, if you want to access the description from third table you can do like bellow:

foreach($firstRecords as $fr){
    $second=$first->second;
    foreach($second->third as $st){
       //$st->description
    }
}

I hope it help

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top