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

  • SEARCH
  • Home
  • 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 8495637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:37:40+00:00 2026-06-10T23:37:40+00:00

I have a model with some relations defined as follows. public function relations() {

  • 0

I have a model with some relations defined as follows.

public function relations()
{
    return array(
        'linkingTable' => array(self::HAS_MANY, 'LinkingTable', array('this_id'=>'id'), 'scopes'=>array('valid')),
        'linkedItems' => array(self::HAS_MANY, 'LinkedItem', array('linked_item_id'=>'id'), 'through'=>'linkingTable', 'scopes'=>array('valid')),
    );
}

Both the linking table and the linked items have a valid scope:

public function scopes() {
    return array(
        'valid'=>array(
            'condition'=>"t.`valid`=1",
        ),
    );
}

In order for the generated join queries to work with the relation scope, I have had to modify the scopes as follows:

public function scopes() {
    return array(
        'valid'=>array(
            'condition'=>"`linkingTable`.`valid`=1",
        ),
    );
}

and:

public function scopes() {
    return array(
        'valid'=>array(
            'condition'=>"`linkedItems`.`valid`=1",
        ),
    );
}

The problem is that those scopes will not work when used from the linked model directly, i.e.:

$linkedItems = LinkedItem::model()->valid()->findAll();

Results in an error to say that linkedItems isn’t a defined alias. Which is understandable, of course. It also results in a need for any other model who wants to own some LinkedItems needing to define the relation in the exact same way.

Is the only solution to define a different scope for each use case, like this:

public function scopes() {
    return array(
        'valid'=>array(
            'condition'=>"t.`valid`=1",
        ),
        'validForModelRelation'=>array(
            'condition'=>"`linkedItems`.`valid`=1",
        )
    );
}

This feels a bit cludgey. I am wondering if there is a better way of doing this?

  • 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-10T23:37:41+00:00Added an answer on June 10, 2026 at 11:37 pm

    You need to be able to get the current alias of the table. t when it’s alone, or the relation name when it’s a related model. In the scope of the related model you can use:

    public function scopes() {
        return array(
            'valid'=>array(
                'condition'=>$this->tableAlias.".`valid`=1",
            ),
        );
    }
    

    If you use it in the defaultScope however, you need to use $this->getTableAlias(false, false). as the parameters to prevent an infinite loop, trying to find the alias.

    edit: dot missing

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

Sidebar

Related Questions

Let's say I have some model objects that resemble this: public class FooModel {
I have a Model which has some constants defined, like below: class Order(models.Model): WAITING
for example: I have created one javascript property array model with some properties like
I have a user model that can follow some tags User has_many :tag_followings has_many
I have model Article it has field title with some text that may contain
In my rails model I have some kind of template system. I want to
I have a class that contains data from some model. This class has metadata
I'm using sorl-thumbnail to get some images to crop. I have a model looking
I have a Project model and it has some text attributes, one is summary.
We have an entity framework model with has some inheritance in it. The following

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.