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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:37:03+00:00 2026-06-05T10:37:03+00:00

This is the table for the model: CREATE TABLE IF NOT EXISTS `SomeModel` (

  • 0

This is the table for the model:

CREATE TABLE IF NOT EXISTS `SomeModel` (  
  `id` int NOT NULL AUTO_INCREMENT,  
  `parent_id` int NOT NULL
)  

My goal is to be able to query a model with its siblings using:

SomeModel::model()->with('siblings')->findByPk($id);

Here is my current attempt at the relation:

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

The problem is that I can’t find a way to create a condition so that the model itself isn’t returned along with it’s siblings in the $model->siblings array.

Any thoughts would be great.

Thanks!

  • 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-05T10:37:04+00:00Added an answer on June 5, 2026 at 10:37 am

    Change your relation to this:

    'siblings'=>array(self::HAS_MANY, 'Address', array('parent_id'=>'parent_id'),'condition'=>'siblings.id!=t.id')
    

    Edit: Some explanation, in the documentation for relation(), we can specify extra options for the join that takes place and these additional options:

    Additional options may be specified as name-value pairs in the rest array elements.

    Plus the default alias for the table is t hence use t.id.

    Edit: from the comments:

    Implementing lazy loading, the way you want it, will be tough to accomplish(I don’t know how, not sure if possible either), however i can suggest making the current code better, by

    1. using named scopes, use a scope when you are doing eager loading, and add the condition siblings.id!=t.id in the scope:

      // add this function to your model, and remove the condition from the relation
      public function scopes(){
       return array(
          'eagerSibs'=>array(
              'condition'=>'siblings.id!=t.id',
          ),
       );
      }
      

      Do eager loading with scope:

      SomeModel::model()->eagerSibs()->with('siblings')->findByPk($id);
      

      This will remove the error with lazy loading $model->siblings

    2. Although the error of lazy loading will be removed you will still be getting the current record, to counter that you can add and use a function of the model which will load the related records without the current one, but ofcourse you won’t be using $model->siblings, and instead have something like: $model->getLazySibs();

      public function getLazySibs(){
          $sibs=$this->siblings;
          foreach ($sibs as $asib){
              if ($asib->id != $this->id)
                  $lazySibs[]=$asib;
          }
          return $lazySibs;
      }
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

SQL: CREATE TABLE IF NOT EXISTS `photoalbums` ( `id` int(11) NOT NULL AUTO_INCREMENT, `title`
I have a database table that looks like this: CREATE TABLE IF NOT EXISTS
This is the table: CREATE TABLE IF NOT EXISTS `requests` ( `ctrc_id` varchar(45) NOT
Why does SQL server express 2008 give me this error? CREATE TABLE model (
This is my query to get model information from one table and a single
This table is used to store sessions (events): CREATE TABLE session ( id int(11)
We have a database with a very simple schema: CREATE TABLE IF NOT EXISTS
I'm trying to import a table in my database executing this query : CREATE
Querying a Nested Set Model table , here's the SQL... how can this be
I have a table like this: id make model year 1 chevy camaro 1969

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.