Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6943893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:12:55+00:00 2026-05-27T13:12:55+00:00

I wish to list some Categories name on my layout main.php page. Since the

  • 0

I wish to list some Categories name on my layout main.php page.
Since the layout doesn’t have any associated controller or model, I wish to create a static method like this on Category model:

public static function getHeaderModels()
{
   // get all models here
   return $models;
}

and then in the main layout

<?php
$models = Category::getHeaderModels();
foreach($models as $model)
{
   // ....
}
?>

My question is a very basic one:
How can I retrieve those category names from the model ?

Here is the full model:

class Category extends CActiveRecord {


    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    public function tableName() {
        return 'category';
    }

    public function rules() {
        return array(
            array('parent_id', 'numerical', 'integerOnly' => true),
            array('name', 'length', 'max' => 255),
            array('id, parent_id, name', 'safe', 'on' => 'search'),
        );
    }

    public function relations() {
        return array(
            'users' => array(self::MANY_MANY, 'User', 'categories(category_id, user_id)'),
        );
    }

    public function scopes()
    {
        return array(
            'toplevel'=>array(
                'condition' => 'parent_id IS NULL'
            ),
        );
    }

    public function attributeLabels() {
        $id = Yii::t('trans', 'ID');
        $parentId = Yii::t('trans', 'Parent');
        $name = Yii::t('trans', 'Name');

        return array(
            'id' => $id,
            'parent_id' => $parentId,
            'name' => $name,
        );
    }

    public function search() {
        $criteria = new CDbCriteria;
        $criteria->compare('id', $this->id);
        $criteria->compare('parent_id', $this->parent_id);
        $criteria->compare('name', $this->name, true);

        return new CActiveDataProvider(get_class($this), array(
                'criteria' => $criteria,
            ));
    }


        public static function getHeaderModels() {

            //what sintax should I use to retrieve the models here ?

            return $models;

        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T13:12:56+00:00Added an answer on May 27, 2026 at 1:12 pm

    May be this answer can help you. First you must create a Widget so you can use it more effectively.

    First Create a new widget. Let say the name is CategoryWidget. Put this widget under components directory protected/components.

    class CategoryWidget extends CWidget {
    
        public function run() {
            $models = Category::model()->findAll();
    
            $this->render('category', array(
                'models'=>$models   
            ));
        }
    }
    

    Then create a view for this widget. The file name is category.php.
    Put it under protected/components/views

    category.php

    <?php if($models != null): ?>
    <ul>
        <?php foreach($models as $model): ?>
        <li><?php echo $model->name; ?></li>
        <?php endforeach; ?>
    </ul>
    <?php endif; ?>
    

    Then call this widget from your main layout.

    main.php

    // your code ...
    
    <?php $this->widget('CategoryWidget') ?>
    
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have List with n items. I wish transform my list to new list,
I have a class that includes a std::list and wish to provide public begin()
I have a list of object I wish to sort in C#.Net 3.5, the
Suppose I have a list that I wish not to return but to yield
I have a list of events that I wish to store the starting and
I have a case where I wish to store a list of resources in
I have an object of type list from which I wish to use to
i have some images in an un ordered list. <ul> <li id='chain'> <a href=>
I have some confusing True and False questions I wish anyone answers them and
I have a list of elements describing events that took place at some time,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.