Get Unrelated Models

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!

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;

    }