Yii Framework Forum: Get Unrelated Models - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Get Unrelated Models Rate Topic: -----

#1 User is offline   EdoFre 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 48
  • Joined: 24-February 11
  • Location:Groningen, The Netherlands

Posted 19 November 2012 - 06:44 AM

Hello everyone,

I've got the following problem and looked everywhere for a solution but havn't been able to find one.

My problem:
I've got 3 tables in my database, Pot, Vegetation and PotsPerVegetation. I can get the relations between pots and vegetations no problem, using the relations in the model, f.e. I can call $vegetation->pots no problem.

However now I want to find all the pots that don't have the current vegetation as a relation. so I want to get the difference from these results. Here is some code to illustrate my problem.
$pots = Pot::model()->findAll();
$relatedPots = $vegetation->tblPots;

// I know this doesn't work but this is basically what I want to do.
$difference = $pots - $relatedPots


Thanks in advance!
Moi,
0

#2 User is offline   EdoFre 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 48
  • Joined: 24-February 11
  • Location:Groningen, The Netherlands

Posted 19 November 2012 - 07:37 AM

Ok I found a solution to my problem after taking a break from it and returning to the problem, I don't know if this is a wanted solution but I solved it using the following code:

public function findNotRelated(Vegetation $vegetation) {
        $pots = Pot::model()->findAll();
        $relatedPots = $vegetation->tblPots;

        //$unrelatedModels = $pots - $related;
        $unrelatedModels = array();
        foreach ($pots as $pot) {
            if(!in_array($pot, $relatedPots)){
                $unrelatedModels[] = $pot;
            }
        }

        return $unrelatedModels;
    }

Moi,
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users