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 8675891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:03:45+00:00 2026-06-12T20:03:45+00:00

I have GoodsCats model with next relations: public function relations(){ return array( ‘GoodsCatsContent’ =>

  • 0

I have GoodsCats model with next relations:

public function relations(){
  return array(
    'GoodsCatsContent' => array(self::HAS_MANY, 'GoodsCatsContent', 'parent_id'),
  );
}

And now I want to find all GoodsCats elements but with one child element (with specific language_id):

$cats = GoodsCats::model()->with(
  array(
    'GoodsCatsContent'=>array(
      'on'=>'languages_id = 1'
    )
  )
)->findAll();

And recive an array where each elemt is like GoodsCats.id=>GoodsCatsContent.name:

CHtml::listData(cats, 'id', 'name')

But for now I’m getting an error GoodsCats.name not defined.
When I set GoodsCats relations as self::HAS_ONE all works good but I can’t change it for whole project.
Is it posible somehow set model()->with() to use not defined relation type but a specific one?

UPD my models rules:

GoodsCatsContent:

public function rules()
{
    return array(
        array('parent_id', 'required'),
        array('active', 'numerical', 'integerOnly'=>true),
        array('parent_id', 'length', 'max'=>10),
        array('name, seo_title, seo_keywords, seo_description', 'length', 'max'=>255),
        array('short_content, content', 'safe'),
        array('id, parent_id, active, name, short_content, content, seo_title, seo_keywords, seo_description', 'safe', 'on'=>'search'),
    );
}

GoodsCats:

public function rules()
{
    return array(
        array('active, prior', 'numerical', 'integerOnly'=>true),
        array('photo', 'length', 'max'=>255),
        array('id, photo, active, prior', 'safe', 'on'=>'search'),
    );
}
  • 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-06-12T20:03:46+00:00Added an answer on June 12, 2026 at 8:03 pm

    You can’t dynamically change the relation type.

    But, since you are sure that using language_id = 1 gives you only one related GoodsCatsContent record, you can do this instead of using listData() :

    $myList=array();
    foreach ($cats as $acat){
        $myList[$acat->id]=isset($acat->GoodsCatsContent[0]->name)? $acat->GoodsCatsContent[0]->name :''; 
        // you need isset incase some of the GoodsCats don't have a related GoodsCatsContent
    }
    // you can now use $myList as you would have used the array returned by listData()
    

    Because this is a HAS_MANY relation, each GoodsCats will have x number of related GoodsCatsContent records, and therefore the relation will return an array always, even if there is only 1 related record, and an empty array if there is no related record. So when there is 1 related record, the relationName[0] will return that record.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

have a php code like this,going to convert it in to C#. function isValid($n){
Have an issue with jquery autocomplete, focus function After i type something in the
**Have it working now. I forgot to populate the Array List. How embarrassing. I'm
Have a question about calling one prototype function in another prototype function. for instance
Have been trying to find a good description of what the next() method does.
have written this little class, which generates a UUID every time an object of
Have a procedure which looks like Procedure TestProc(TVar1, TVar2 : variant); Begin TVar1 :=
Have done quite a bit of searching for a guide (of any substance) for
Have deployed numerous report parts which reference the same view however one of them
have a problem. At first look at this HTML <div id=map style=background-image: url(map.png); width:

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.