I have a model Company with a HAS_MANY relationship to the CompanyLocationServed model.
In the CompanyController, I process the related CompanyLocationServed model, and add them to the $model->locationsServed attribute. That seems to work fine. Of course, I do not put in the company_id attribute since the Company model has not been saved yet.
When I try to save using with-related-behavior, I get an error about the company_id attribute being blank in the CompanyLocationServed models.
For some reason, the company_id attribute is not being filled in before attempting to save related CompanyLocationServed models ($company-locationsServed). But, another related model, Address ($company->physicalAddress), works and saves perfectly. What could cause this?
Following is my code. I removed everything that isn’t relevant.
Here is the Company model:
/** This is the model class for table "company".
*
* The followings are the available columns in table 'company':
* @property string $id
...
*
* The followings are the available model relations:
...
* @property LocationServed[] $locationsServed
...
*/
class Company extends CActiveRecord
{
...
public function relations()
{
return array(
...
'locationsServed' => array(self::HAS_MANY, 'CompanyLocationServed', 'company_id'),
...
);
}
...
public function behaviors()
{
return array(
'withRelated' => array(
'class' => 'ext.wr.WithRelatedBehavior',
),
);
}
}
Here is the CompanyLocationServed model:
/**
* The followings are the available columns in table 'company_location_served':
* @property string $id
* @property string $company_id
* @property string $location_id
* @property integer $radius_miles
*
* The followings are the available model relations:
* @property Company $company
* @property Location $location
*/
class CompanyLocationServed extends CActiveRecord
{
...
public function relations()
{
return array(
'company' => array(self::BELONGS_TO, 'Company', 'company_id'),
'location' => array(self::BELONGS_TO, 'Location', 'location_id'),
);
}
...
}
Here is the Location model (maybe not necessary to see):
/**
* This is the model class for table "location".
*
* The followings are the available columns in table 'location':
* @property string $id
...
*
* The followings are the available model relations:
* @property CompanyLocationServed[] $companyLocationServeds
*/
class Location extends CActiveRecord
{
...
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'companyLocationServeds' => array(self::HAS_MANY, 'CompanyLocationServed', 'location_id'),
);
}
}
Lastly, this is from the CompanyController:
public function actionCreate()
{
$model = new Company;
$physicalAddress = new Address;
if (isset($_POST['Company']))
{
$model->attributes = $_POST['Company'];
if(isset($_POST['Address']))
{
$physicalAddress->attributes=$_POST['Address'];
$model->physicalAddress=$physicalAddress;
}
// add any new locationsServed
if (isset($_POST['locations']))
$model->locationsServed = $this->addLocationsServed($_POST['locations']);
// save model
if ($model->withRelated->save(
true, array(
'locationsServed',
'physicalAddress',
)))
$this->render('preview', array(
'model' => $model,
));
else
echo '<pre>'; CVarDumper::dump($model);
}
else
{
$this->render('create', array(
'model' => $model,
'physicalAddress' => $physicalAddress,
));
}
}