cornernote / yii2-linkall

Behavior to handle saving multiple many to many related records in Yii2
Other
25 stars 7 forks source link

Unable to link models #2

Open FractalMind opened 5 years ago

FractalMind commented 5 years ago
$model = TENANT_device::findOne($this->FK_tenant_station_id);
if( isset($model) ) {
   $this->linkAll('tenant_device', [$model], null, false, false);
} else {
    $this->addError('FK_tenant_station_id', 'The tenant_device "' . $this->FK_tenant_station_id . '" does\'nt exists');
}

{ "name": "Exception", "message": "Unable to link models: the models being linked cannot be newly created.", "code": 0, "type": "yii\base\InvalidCallException", "file": "/var/www/html/vendor/yiisoft/yii2/db/BaseActiveRecord.php", "line": 1293, "stack-trace": [ "#0 /var/www/html/vendor/cornernote/yii2-linkall/src/LinkAllBehavior.php(87): yii\db\BaseActiveRecord->link('tenant_device', Object(common\models\tenant\TENANT_device), NULL)", "#1 /var/www/html/vendor/cornernote/yii2-linkall/src/LinkAllBehavior.php(53): cornernote\linkall\LinkAllBehavior->link('tenant_device', 'id', Array, Array, NULL)", "#2 [internal function]: cornernote\linkall\LinkAllBehavior->linkAll('tenant_device', Array, NULL, false, false)", "#3 /var/www/html/vendor/yiisoft/yii2/base/Component.php(297): call_user_func_array(Array, Array)", "#4 /var/www/html/common/models/tenant/TENANT_station.php(42): yii\base\Component->__call('linkAll', Array)", "#5 /var/www/html/vendor/yiisoft/yii2/db/ActiveRecord.php(545): common\models\tenant\TENANT_station->beforeSave(true)", "#6 /var/www/html/vendor/yiisoft/yii2/db/ActiveRecord.php(515): yii\db\ActiveRecord->insertInternal(NULL)", "#7 /var/www/html/vendor/yiisoft/yii2/db/BaseActiveRecord.php(670): yii\db\ActiveRecord->insert(true, NULL)", "#8 /var/www/html/common/gtb/db/TENANT_ActiveRecord.php(141): yii\db\BaseActiveRecord->save(true, NULL)", "#9 /var/www/html/common/gtb/actions/CreateAction.php(51): gtb\db\TENANT_ActiveRecord->save()", "#10 [internal function]: gtb\actions\CreateAction->run()", "#11 /var/www/html/vendor/yiisoft/yii2/base/Action.php(94): call_user_func_array(Array, Array)", "#12 /var/www/html/vendor/yiisoft/yii2/base/Controller.php(157): yii\base\Action->runWithParams(Array)", "#13 /var/www/html/vendor/yiisoft/yii2/base/Module.php(528): yii\base\Controller->runAction('create', Array)", "#14 /var/www/html/vendor/yiisoft/yii2/web/Application.php(103): yii\base\Module->runAction('api2/station/cr...', Array)", "#15 /var/www/html/vendor/yiisoft/yii2/base/Application.php(387): yii\web\Application->handleRequest(Object(yii\web\Request))", "#16 /var/www/html/backend/web/index.php(21): yii\base\Application->run()", "#17 {main}" ] }

Im I doing this right?

cornernote commented 5 years ago

Remove the isset

Should just be if ($model)

On Sat, 16 Mar 2019 at 6:46 am, FractalMind notifications@github.com wrote:

$model = TENANT_device::findOne($this->FK_tenant_station_id); if( isset($model) ) { $this->linkAll('tenant_device', [$model], null, false, false); } else { $this->addError('FK_tenant_station_id', 'The tenant_device "' . $this->FK_tenant_station_id . '" does\'nt exists'); }

{ "name": "Exception", "message": "Unable to link models: the models being linked cannot be newly created.", "code": 0, "type": "yii\base\InvalidCallException", "file": "/var/www/html/vendor/yiisoft/yii2/db/BaseActiveRecord.php", "line": 1293, "stack-trace": [ "#0 /var/www/html/vendor/cornernote/yii2-linkall/src/LinkAllBehavior.php(87): yii\db\BaseActiveRecord->link('tenant_device', Object(common\models\tenant\TENANT_device), NULL)", "#1 https://github.com/cornernote/yii2-linkall/issues/1 /var/www/html/vendor/cornernote/yii2-linkall/src/LinkAllBehavior.php(53): cornernote\linkall\LinkAllBehavior->link('tenant_device', 'id', Array, Array, NULL)", "#2 https://github.com/cornernote/yii2-linkall/issues/2 [internal function]: cornernote\linkall\LinkAllBehavior->linkAll('tenant_device', Array, NULL, false, false)", "#3 /var/www/html/vendor/yiisoft/yii2/base/Component.php(297): call_user_func_array(Array, Array)", "#4 /var/www/html/common/models/tenant/TENANT_station.php(42): yii\base\Component->__call('linkAll', Array)", "#5 /var/www/html/vendor/yiisoft/yii2/db/ActiveRecord.php(545): common\models\tenant\TENANT_station->beforeSave(true)", "#6 /var/www/html/vendor/yiisoft/yii2/db/ActiveRecord.php(515): yii\db\ActiveRecord->insertInternal(NULL)", "#7 /var/www/html/vendor/yiisoft/yii2/db/BaseActiveRecord.php(670): yii\db\ActiveRecord->insert(true, NULL)", "#8 /var/www/html/common/gtb/db/TENANT_ActiveRecord.php(141): yii\db\BaseActiveRecord->save(true, NULL)", "#9 /var/www/html/common/gtb/actions/CreateAction.php(51): gtb\db\TENANT_ActiveRecord->save()", "#10 [internal function]: gtb\actions\CreateAction->run()", "#11 /var/www/html/vendor/yiisoft/yii2/base/Action.php(94): call_user_func_array(Array, Array)", "#12 /var/www/html/vendor/yiisoft/yii2/base/Controller.php(157): yii\base\Action->runWithParams(Array)", "#13 /var/www/html/vendor/yiisoft/yii2/base/Module.php(528): yii\base\Controller->runAction('create', Array)", "#14 /var/www/html/vendor/yiisoft/yii2/web/Application.php(103): yii\base\Module->runAction('api2/station/cr...', Array)", "#15 /var/www/html/vendor/yiisoft/yii2/base/Application.php(387): yii\web\Application->handleRequest(Object(yii\web\Request))", "#16 /var/www/html/backend/web/index.php(21): yii\base\Application->run()", "#17 {main}" ] }

Im I doing this right?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/cornernote/yii2-linkall/issues/2, or mute the thread https://github.com/notifications/unsubscribe-auth/AADKo4_8jeZIWbMHE-e1OhBVGJIaxCbXks5vW_-LgaJpZM4b3OpW .

--

-- Brett

FractalMind commented 5 years ago

The error "Unable to link models: the models being linked cannot be newly created." is at : $this->linkAll('tenant_device', [$model], null, false, false);

The model is not new, why it doesn't create intermediate records? station -|--< station_device >--|- device

cornernote commented 5 years ago

Does it help to use tenantDevice instead of tenant_device.

You should use whatever the relationship is called (without the get). For example if the relationship is getTennantDevice() then you should use tenantDevice.

If that doesn't help let me know. I've been away for a bit, but can reply faster now, if you still have the issue.