Hi,
This is driving me nuts…
I have a project which on creation needs to create several other entries with just the defaultvalues (set in the db).
public function relations()
{
return array(
'control' => array(self::HAS_ONE, 'Control', 'ControlId'),
'display' => array(self::HAS_ONE, 'Display', 'DisplayId'),
'network' => array(self::HAS_ONE, 'Network', 'NetworkId'),
'memory' => array(self::HAS_ONE, 'Memory', 'MemoryId'),
'locations' => array(self::HAS_MANY, 'Locations', 'ProjectId'),
'user' => array(self::BELONGS_TO, 'Users', 'UserId'),
'defaultlocation' => array(self::BELONGS_TO, 'Locations', 'DefaultLocation'),
);
}
The projects create controller:
$project->attributes=$_POST['Projects'];
$project->UserId = Yii::app()->user->getId();
$project->defasultlocation = new Locations();
$project->autorotate = new Autorotate();
$project->control = new Control();
$project->display = new Display();
$project->memory = new Memory();
$project->network = new Network();
$project->memory_id = $project->memory->primaryKey;
$project->network_id = $project->network->primaryKey;
$project->autorotate_id = $project->autorotate->primaryKey;
$project->control_id = $project->control->primaryKey;
$project->display_id = $project->display->primaryKey;
$project->DefaultLocation= $project->defaultlocation->primaryKey;
$project->save();
project save fails. If I try to save the submodels (iow: $project->network->save()); I get a constraint error.
I have put FK constraint on project->display_id with display->DisplayId in my database.
Ive seen lots of articles about reading relationships, but none about creating new records with relationships. I found one topic on the forum, but that didn’t work.
Other issue: Is there a way to see why a save fails?