RAR understanding

Hello all,

I am quite stuck on an issue. I read the doc and did not understand.

I have the following database:

Table :

cv_langue (for each language)


-->id_langue (primary key, auto inc)


-->title





cv_categorie (for each categorie)


-->id_cv_categorie (primary key, auto inc)


-->id_langue (refer to table cv_langue and id_langue)


-->title





cv_sub_categorie (for each sub categorie)


-->id_sub_categorie (primary key, auto inc)


-->id_cv_categorie (refer to table cv_categorie and id_cv_categorie)


-->title





cv_texte (for each data)


-->id_cv_texte


-->id_cv_categorie (refer to table cv_categorie and id_cv_categorie)


-->id_cv_sub_categorie (refer to table cv_sub_categorie and id_cv_sous_categorie)


-->id_cv_langue (refer to table cv_langue and id_langue)


-->texte

I set up the Relation between each other. What I am trying to do is to browse each language, and all sub categorie in categorie and finally display the data.

So I basically done :

$cv_texte = new cv_texte;


$List = cv_texte::model()->findAll();

But I am stuck with : How to browse the array correctly to do that?

Any help is welcome :)

Hmm… not sure exactly what you want to do here.

What's the expected result?

I would like to have something like that:



foreach language {





     foreach categorie{





          foreach subcategorie{





               echo $texte;


          }


     }


}

Firs of all you need to create your databas models with relations like:

<?php 


class Language extends CActiveRecord


{


    public function relations()


    {


        return array(


            'categoires'=>array(self::HAS_MANY, 'categorie', 'categorie_id'),


        );


    }


}


 


class categorie extends CActiveRecord


{


    public function relations()


    {


        return array(


            'language'=>array(self::BELONGS_TO, 'language', 'language_id'),


            'subcategories'=>array(self::HAS_MANY, 'subcategorie', subcategorie_id'),


        );


    }


}


etc, etc…

Then you could loop through it with the following code:

<?php


foreach( $language->findAll() as $lang) 


{


    foreach( $lang->categories as $categorie )


    {


        ....


The code should just be seen as an example, i haven't tested it so it might contain some typos but the main idea should work.

You may also want to do eager loading to reduce the number of SQL queries:



$languages=Language::model()->with('categories')->findAll();


foreach($languages as $language)


{


      foreach($language->categories as $category)


            ....


}


Thank you for your answers.

But I still don't understand something, I may not explain well what I am trying to do…

Data are stored in cv_texte table.

In my view I am trying to browse all data and sort them by languages, categories, sub categories…

If a set up correctly the relation between each other, would it be possible to do like:

$cv_texte = new cv_texte;

$List = cv_texte::model()->findAll();

And after sort the array $List to have that?

foreach languages {

    foreach categories{

          foreach subcategories{

              echo $texte; (mean the field texte in cv_texte)

          }

    }

}

In fact for each data in cv_texte I have a language ID, a category ID and a sub category ID. Do I need to change it?

But if I am following your previous answer, language has no categories on my schema, so I am a bit confused.

Any help would be appreciated please :)

Thanks

From performance point of view, I think the following solution is the best:



$data=cv_texte::model()->with('category, subcategory, language')->findAll();


You may add ORDER clause to sort them by category, subcategory and language. Then use a single for-loop to traverse the data. You will need to do grouping in PHP. However, if the sorting is done as suggested, this shouldn't be a big problem.

If you still want to use nested loops, you will need to perform a query for cv_texte at the innermost loop using the current category, subcateory and language.

Thank you Qiang for your answer!

So I need to do something like that?



foreach($List as $result):





if($langue!=$result->langues->id_langue){


//new language code


}


echo $result->id_cv_texte;


echo $result->texte;


endforeach;

yeah, something like this.

Ok thank you Qiang  :)