HAS_ONE Insert and update


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->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 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?

I think you have to create the related data separately. For instance:


                    $project->UserId  = Yii::app()->user->getId();

                    $memory = new Memory; // or get existing with Memory::model()->find(...)

                    // insert data into memory here (stupid example)

                    $memory->name = "something";

                    if(!$memory->save()) // You can for example do this to debug





                    $project->memory_id = $memory->id;

                    if(!$project->save()) // You can for example do this to debug





Thanks for your help, but as soon as I do memory->save() I get an error about FK constraint failing on memory. Im starting to think its a problem with the database.

this is the setup:

Projects table

ProjectId memory_id display_id etc

Memory table

MemoryId Maxmem

then I add a foreign key on memory table, MemoryId references Projects memory_id (or should I create a FK on projects referencing to memory->MemoryId?)

delete and update are set to CASCADE. Basicly I want, if a project gets deleted, then alsoo delete the memory record. Memory record will never get deleted on its own or transferred to another project.

I think I answered my own question. projects->memory_id is the foreign key referencing Memory->MemoryId. The child should reference to the parent else ofcourse you couldnt make multiple references. I changed the FK constraint and now I dont get any errors.

Its not saving atm although getErrors isnt showing any errors, but I think this is probally because of the other constraints I need to fix.

Are both saves not working or just the project?

the foreigns/submodels are being saved, Just not the project. No errors either. I generated all the models again with Gii and now it saves :) Probally a BELONGS_TO / HAS_ONE mix up in one of the files.

Still weird I had no detailed error. save was false and the errorlog was empty