Problem with MANY_MANY

I'm building a website for a client with a few locations. These locations have different brands. What I want to do is, to link the "Location" and the "Brand" table, so I put the following code into model class of "Location:

public function relations()

{

  return array(

    'arrBrand' => array(self::MANY_MANY, 'Brand', 'LocationBrand(idLocation, idBrand)')

  );

}

Now have the problem, that the table "LocationBrand" contains 3 fields (idLocation, idBrand, strUrl), but I can't access the strUrl field.

What i want to do, is in the form of "Location" a list of all entries in "Brand" table, with a checkbox, and if an entry is checked, the user can insert a URL for this brand. Has this got to be a MANY_MANY relation?

For every selected brand should be an entry in "LocationBrand" table, with idLocation, idBrand and strUrl.

P.S.: It's quite unclear to me, how read/write for this relation works.

Since you join table contains additional data, you should declare an AR class for it, and then relate to this new AR class using HAS_MANY, and this AR class is related to Brand via BELONGS_TO.

Quote

Since you join table contains additional data, you should declare an AR class for it, and then relate to this new AR class using HAS_MANY, and this AR class is related to Brand via BELONGS_TO.

Ok, I've changed this, but how can I achive, that wenn LocationBrand is used, that it calls brand automatically?

EDIT:

I think, I can answer this question by myself:

Location::model()->with('arrLocationBrand', 'arrLocationBrand.arrBrand')->together()->findAll();

Now the next question:

How can I validate the nested objects (Location and LocationBrands) in actionUpdate and in actionCreate, without having the code in two places?

What I want to do,is the following:

  • First should be the "Location" be validated

  • if data for "Location" is valid, the data for "LocationBrand" should be validated

public function actionUpdate()

{

$location  = $this->loadLocation();


$booValid = true;





if (isset($_POST['Location']))


{


	$location->attributes = $_POST['Location'];


	$booValid = $location->validate();





	if (isset($_POST['LocationBrand'])) {





	  $arrLocationBrand = array();





	  foreach ($_POST['LocationBrand'] as $i => $arrItem) {





	    if (intVal($arrItem['idBrand']) > 0)


	    {

                      $objLocationBrand                    = new LocationBrand();

                      $objLocationBrand->idLocation  = $location->id;

                      $objLocationBrand->idBrand      = $arrItem['idBrand'];

                      $objLocationBrand->strUrl        = $arrItem['strUrl'];

                      $booValid = $objLocationBrand->validate() && $booValid;

                      array_push($arrLocationBrand, $objLocationBrand);

	    }


	  }


	}


  if ($booValid && $location->save())


  {


    Yii::app()->db->createCommand('DELETE FROM LocationBrand WHERE idLocation='.$location->id)->execute();


	  foreach ($arrLocationBrand as $i => $arrItem) {


	    $arrItem->save();


	  }


  }


}


else {


  $arrLocationBrand = $location->arrLocationBrand;


}





$this->render('update', array('location' => $location, 'locationBrand' => $arrLocationBrand));

}

this works, but now I have to put the same code into actionCreate. Is there another way for doing this?

EDIT:

I've tried to move validation an saving of LocationBrand into "afterSave" method, of "Location" model so the code could be used by "actionCreate" and "actionUpdate", but if I do this, I can't show errors accoured in "LocationBrand" in the form, because "afterSave" only returns true or false, and not the models of "LocationBrand" :frowning: