/**
  * @depends testHasManyRelationSqlQuery
  */
 public function testManyManyRelationSqlQuery()
 {
     $super = User::getByUsername('super');
     Yii::app()->user->userModel = $super;
     //I has many ls.
     $i = new I();
     $l = new L();
     $l->lMember = 'def';
     $this->assertTrue($l->save());
     $i->iMember = 'abc';
     $i->ls->add($l);
     $this->assertTrue($i->save());
     $searchAttributeData = array();
     $searchAttributeData['clauses'] = array(1 => array('attributeName' => 'ls', 'relatedAttributeName' => 'lMember', 'operatorType' => 'equals', 'value' => 'somevalue'));
     $searchAttributeData['structure'] = '1';
     $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('I');
     $quote = DatabaseCompatibilityUtil::getQuote();
     $where = RedBeanModelDataProvider::makeWhere('I', $searchAttributeData, $joinTablesAdapter);
     $compareWhere = "({$quote}l{$quote}.{$quote}lmember{$quote} = 'somevalue')";
     $this->assertEquals($compareWhere, $where);
     //Now test that the joinTablesAdapter has correct information.
     $this->assertEquals(0, $joinTablesAdapter->getFromTableJoinCount());
     $this->assertEquals(2, $joinTablesAdapter->getLeftTableJoinCount());
     $leftTables = $joinTablesAdapter->getLeftTablesAndAliases();
     $this->assertEquals('i_l', $leftTables[0]['tableName']);
     $this->assertEquals('l', $leftTables[1]['tableName']);
     //Now test that the subsetSQL query produced is correct.
     $subsetSql = I::makeSubsetOrCountSqlQuery('i', $joinTablesAdapter, 1, 5, $where, null, false, $joinTablesAdapter->getSelectDistinct());
     $compareSubsetSql = "select distinct {$quote}i{$quote}.{$quote}id{$quote} id ";
     $compareSubsetSql .= "from {$quote}i{$quote} ";
     $compareSubsetSql .= "left join {$quote}i_l{$quote} on ";
     $compareSubsetSql .= "{$quote}i_l{$quote}.{$quote}i_id{$quote} = {$quote}i{$quote}.{$quote}id{$quote} ";
     $compareSubsetSql .= "left join {$quote}l{$quote} on ";
     $compareSubsetSql .= "{$quote}l{$quote}.{$quote}id{$quote} = {$quote}i_l{$quote}.{$quote}l_id{$quote} ";
     $compareSubsetSql .= "where " . $compareWhere . ' ';
     $compareSubsetSql .= 'limit 5 offset 1';
     $this->assertEquals($compareSubsetSql, $subsetSql);
     //Make sure the sql runs properly.
     $dataProvider = new RedBeanModelDataProvider('I', null, false, $searchAttributeData);
     $data = $dataProvider->getData();
 }