Closed rossaddison closed 2 years ago
Hello.
ORM 2.0.0
Can you update the orm to v2.1.1?
About BelongsTo relation: Can you check that PR is about your issue? https://github.com/cycle/orm/pull/347
Hi Aleksei,
I have had a look at #347.
Here is my stack trace.
Cycle\ORM\Exception\TransactionException: Transaction can't be finished. Some relations can't be resolved:
Update `product`
- family (Cycle\ORM\Relation\BelongsTo) in /var/www/html/yii-invoice/vendor/cycle/orm/src/Exception/TransactionException.php:40
Stack trace:
#0 /var/www/html/yii-invoice/vendor/cycle/orm/src/Transaction/UnitOfWork.php(104): Cycle\ORM\Exception\TransactionException::unresolvedRelations()
#1 /var/www/html/yii-invoice/vendor/cycle/orm/src/EntityManager.php(47): Cycle\ORM\Transaction\UnitOfWork->run()
#2 /var/www/html/yii-invoice/vendor/yiisoft/yii-cycle/src/Data/Writer/EntityWriter.php(25): Cycle\ORM\EntityManager->run()
#3 /var/www/html/yii-invoice/src/Invoice/Product/ProductRepository.php(59): Yiisoft\Yii\Cycle\Data\Writer\EntityWriter->write()
#4 /var/www/html/yii-invoice/src/Invoice/Product/ProductService.php(30): App\Invoice\Product\ProductRepository->save()
#5 /var/www/html/yii-invoice/src/Invoice/Product/ProductController.php(244): App\Invoice\Product\ProductService->saveProduct()
#6 [internal function]: App\Invoice\Product\ProductController->edit()
#7 /var/www/html/yii-invoice/vendor/yiisoft/injector/src/Injector.php(66): ReflectionFunction->invokeArgs()
#8 /var/www/html/yii-invoice/vendor/yiisoft/middleware-dispatcher/src/MiddlewareFactory.php(89): Yiisoft\Injector\Injector->invoke()
#9 /var/www/html/yii-invoice/vendor/yiisoft/middleware-dispatcher/src/MiddlewareStack.php(104): ***@***.***>process()
#10 /var/www/html/yii-invoice/vendor/yiisoft/auth/src/Middleware/Authentication.php(57): ***@***.***>handle()
#11 /var/www/html/yii-invoice/vendor/yiisoft/middleware-dispatcher/src/MiddlewareStack.php(104): Yiisoft\Auth\Middleware\Authentication->process()
#12 /var/www/html/yii-invoice/src/Middleware/AccessChecker.php(39): ***@***.***>handle()
#13 /var/www/html/yii-invoice/vendor/yiisoft/middleware-dispatcher/src/MiddlewareFactory.php(126): App\Middleware\AccessChecker->process()
#14 /var/www/html/yii-invoice/vendor/yiisoft/middleware-dispatcher/src/MiddlewareStack.php(104): ***@***.***>process()
#15 /var/www/html/yii-invoice/vendor/yiisoft/yii-debug-viewer/src/Middleware/ToolbarMiddleware.php(33): ***@***.***>handle()
#16 /var/www/html/yii-invoice/vendor/yiisoft/middleware-dispatcher/src/MiddlewareStack.php(104): Yiisoft\Yii\Debug\Viewer\Middleware\ToolbarMiddleware->process()
#17 /var/www/html/yii-invoice/vendor/yiisoft/data-response/src/Middleware/FormatDataResponse.php(29): ***@***.***>handle()
#18 /var/www/html/yii-invoice/vendor/yiisoft/middleware-dispatcher/src/MiddlewareStack.php(104): Yiisoft\DataResponse\Middleware\FormatDataResponse->process()
#19 /var/www/html/yii-invoice/vendor/yiisoft/csrf/src/CsrfMiddleware.php(48): ***@***.***>handle()
#20 /var/www/html/yii-invoice/vendor/yiisoft/middleware-dispatcher/src/MiddlewareStack.php(104): Yiisoft\Csrf\CsrfMiddleware->process()
#21 /var/www/html/yii-invoice/vendor/yiisoft/yii-debug-api/src/Middleware/DebugHeaders.php(31): ***@***.***>handle()
#22 /var/www/html/yii-invoice/vendor/yiisoft/middleware-dispatcher/src/MiddlewareStack.php(104): Yiisoft\Yii\Debug\Api\Middleware\DebugHeaders->process()
#23 /var/www/html/yii-invoice/vendor/yiisoft/middleware-dispatcher/src/MiddlewareStack.php(53): ***@***.***>handle()
#24 /var/www/html/yii-invoice/vendor/yiisoft/middleware-dispatcher/src/MiddlewareDispatcher.php(50): Yiisoft\Middleware\Dispatcher\MiddlewareStack->handle()
#25 /var/www/html/yii-invoice/vendor/yiisoft/router/src/MatchingResult.php(116): Yiisoft\Middleware\Dispatcher\MiddlewareDispatcher->dispatch()
#26 /var/www/html/yii-invoice/vendor/yiisoft/router/src/Middleware/Router.php(61): Yiisoft\Router\MatchingResult->process()
#27 /var/www/html/yii-invoice/vendor/yiisoft/middleware-dispatcher/src/MiddlewareStack.php(104): Yiisoft\Router\Middleware\Router->process()
#28 /var/www/html/yii-invoice/src/Middleware/LocaleMiddleware.php(95): ***@***.***>handle()
#29 /var/www/html/yii-invoice/vendor/yiisoft/middleware-dispatcher/src/MiddlewareStack.php(104): App\Middleware\LocaleMiddleware->process()
#30 /var/www/html/yii-invoice/vendor/yiisoft/user/src/Login/Cookie/CookieLoginMiddleware.php(71): ***@***.***>handle()
#31 /var/www/html/yii-invoice/vendor/yiisoft/middleware-dispatcher/src/MiddlewareStack.php(104): Yiisoft\User\Login\Cookie\CookieLoginMiddleware->process()
#32 /var/www/html/yii-invoice/vendor/yiisoft/cookies/src/CookieMiddleware.php(84): ***@***.***>handle()
#33 /var/www/html/yii-invoice/vendor/yiisoft/middleware-dispatcher/src/MiddlewareStack.php(104): Yiisoft\Cookies\CookieMiddleware->process()
#34 /var/www/html/yii-invoice/vendor/yiisoft/session/src/SessionMiddleware.php(36): ***@***.***>handle()
#35 /var/www/html/yii-invoice/vendor/yiisoft/middleware-dispatcher/src/MiddlewareStack.php(104): Yiisoft\Session\SessionMiddleware->process()
#36 /var/www/html/yii-invoice/vendor/yiisoft/error-handler/src/Middleware/ErrorCatcher.php(135): ***@***.***>handle()
#37 /var/www/html/yii-invoice/vendor/yiisoft/middleware-dispatcher/src/MiddlewareStack.php(104): Yiisoft\ErrorHandler\Middleware\ErrorCatcher->process()
#38 /var/www/html/yii-invoice/vendor/yiisoft/middleware-dispatcher/src/MiddlewareStack.php(53): ***@***.***>handle()
#39 /var/www/html/yii-invoice/vendor/yiisoft/middleware-dispatcher/src/MiddlewareDispatcher.php(50): Yiisoft\Middleware\Dispatcher\MiddlewareStack->handle()
#40 /var/www/html/yii-invoice/vendor/yiisoft/yii-http/src/Application.php(87): Yiisoft\Middleware\Dispatcher\MiddlewareDispatcher->dispatch()
#41 /var/www/html/yii-invoice/vendor/yiisoft/yii-runner-http/src/HttpApplicationRunner.php(105): Yiisoft\Yii\Http\Application->handle()
#42 /var/www/html/yii-invoice/public/index.php(36): Yiisoft\Yii\Runner\Http\HttpApplicationRunner->run()
#43 {main}
My repo is publicly available at github.com/rossaddison/yii-invoice if you want to set it up.
From: Aleksei Gagarin @.>
Sent: 21 June 2022 16:28
To: cycle/orm @.>
Cc: Ross Addison @.>; Author @.>
Subject: Re: [cycle/orm] cycle\ORM\Exception\TransactionException Transaction can't be finished. Some relations can't be resolved: Update product
- tax_rate (Cycle\ORM\Relation\BelongsTo)π (Issue #348)
About BelongsTo relation: Can you check that PR is about your issue? #347https://github.com/cycle/orm/pull/347
β Reply to this email directly, view it on GitHubhttps://github.com/cycle/orm/issues/348#issuecomment-1161916137, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ACBERY536QCR4ZRTFDUZHGDVQHNQ3ANCNFSM5ZMKPQZQ. You are receiving this because you authored the thread.Message ID: @.***>
[cid:e78b8164-f878-43ad-a55a-574724011f87]
composer.json
"require": { "php": "^8.0", "ext-mbstring": "", "ext-pdo_sqlite": "", "cebe/markdown": @.**", "cycle/database": "^2.0", "cycle/entity-behavior": "^1.0", "cycle/orm": "^2.1.1", "doctrine/collections": "^1.6", "fakerphp/faker": "^1.14", "httpsoft/http-message": "^1.0.5", "mpdf/mpdf": "",
From: Ross Addison @.>
Sent: 21 June 2022 18:16
To: cycle/orm @.>
Subject: Re: [cycle/orm] cycle\ORM\Exception\TransactionException Transaction can't be finished. Some relations can't be resolved: Update product
- tax_rate (Cycle\ORM\Relation\BelongsTo)π (Issue #348)
Hi Aleksei,
I have had a look at #347.
Here is my stack trace.
Cycle\ORM\Exception\TransactionException: Transaction can't be finished. Some relations can't be resolved:
Update product
My repo is publicly available at github.com/rossaddison/yii-invoice if you want to set it up.
From: Aleksei Gagarin @.>
Sent: 21 June 2022 16:28
To: cycle/orm @.>
Cc: Ross Addison @.>; Author @.>
Subject: Re: [cycle/orm] cycle\ORM\Exception\TransactionException Transaction can't be finished. Some relations can't be resolved: Update product
- tax_rate (Cycle\ORM\Relation\BelongsTo)π (Issue #348)
About BelongsTo relation: Can you check that PR is about your issue? #347https://github.com/cycle/orm/pull/347
β Reply to this email directly, view it on GitHubhttps://github.com/cycle/orm/issues/348#issuecomment-1161916137, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ACBERY536QCR4ZRTFDUZHGDVQHNQ3ANCNFSM5ZMKPQZQ. You are receiving this because you authored the thread.Message ID: @.***>
Could you render the ORM Schema in a PHP format and show here?
public function setTax_rate( $tax_rate ): void { $this->tax_rate = $tax_rate ; }
do you have the code $product->setTax_rate( $tax_rate ); or $tax_rate->addProduct($product);
This is the _form:
This is the controller:
public function edit(ViewRenderer $head, SessionInterface $session, Request $request, CurrentRoute $currentRoute, ValidatorInterface $validator,
pR $pR, sR $sR, fR $fR, uR $uR, trR $trR,
): Response {
$this->rbac();
$parameters = [
'title' => $sR->trans('edit'),
'action' => ['product/edit', ['id' => $this->product($currentRoute, $pR)->getProduct_id()]],
'errors' => [],
'body' => $this->body($this->product($currentRoute, $pR)),
's'=>$sR,
'head'=>$head,
'families'=>$fR->findAllPreloaded(),
'units'=>$uR->findAllPreloaded(),
'tax_rates'=>$trR->findAllPreloaded()
];
if ($request->getMethod() === Method::POST) {
$form = new ProductForm();
$body = $request->getParsedBody();
if ($form->load($body) && $validator->validate($form)->isValid()) {
$this->productService->saveProduct($this->product($currentRoute, $pR), $form);
$this->flash('info', $sR->trans('record_successfully_updated'));
return $this->webService->getRedirectResponse('product/index');
}
$parameters['body'] = $body;
$parameters['errors'] = $form->getFormErrors();
}
// BelongsTo bug: => render a form different to _form with BelongTo fields family_id, unit_id, tax_rate_id hidden.
// The user will have to delete the product / service. This deletion will be prevented if the product exists on
// a quote or invoice.
return $this->viewRenderer->render('_form_edit', $parameters);
}
This is the service:
public function saveProduct(Product $model, ProductForm $form): void
{
$model->setProduct_sku($form->getProduct_sku());
$model->setProduct_name($form->getProduct_name());
$model->setProduct_description($form->getProduct_description());
$model->setProduct_price($form->getProduct_price());
$model->setPurchase_price($form->getPurchase_price());
$model->setProvider_name($form->getProvider_name());
$model->setProduct_tariff($form->getProduct_tariff());
$model->setTax_rate_id((int)$form->getTax_rate_id());
$model->setUnit_id((int)$form->getUnit_id());
$model->setFamily_id((int)$form->getFamily_id());
$this->repository->save($model);
}
This is the Form code:
How do you render the Orm schema?
github.com/rossaddison/yii-invoice
You can use https://github.com/cycle/schema-renderer for schema rendering
How do you render the Orm schema?
https://github.com/yiisoft/yii-cycle/blob/master/docs/en/console-commands.md#common
cycle/schema/php [file]
- Export current schema as PHP file
This is schema:
Schema php
Specifically the product schema table:
Schema looks OK. I was expecting a slightly different result from the inner/outer keys generator.
Can you dump the entity before edit and after edit (before save)? Needed fields: $tax_rate_id
and $tax_rate
This is a schema which I have pulled from rossaddison/yii-invoice which is a fork of yiisoft/demo. Specifically from
runtime/schema.php generated by the rossaddison/yii-invoice fork once it is running as a result of using config/params.php
'schema-providers' => [
// Uncomment next line to enable a Schema caching in the common cache
//\Yiisoft\Yii\Cycle\Schema\Provider\SimpleCacheSchemaProvider::class => ['key' => 'cycle-orm-cache-key'],
// Store generated Schema in the file
\Yiisoft\Yii\Cycle\Schema\Provider\PhpFileSchemaProvider::class => [
'mode' => \Yiisoft\Yii\Cycle\Schema\Provider\PhpFileSchemaProvider::MODE_READ_AND_WRITE,
'file' => 'runtime/schema.php',
],
////\Yiisoft\Yii\Cycle\Schema\Provider\FromConveyorSchemaProvider::class => [
//// 'generators' => [
//// Cycle\Schema\Generator\SyncTables::class, // sync table changes to database
//// ],
////],
],
I was not sure how to initialize the schema using the https://github.com/cycle/schema-renderer, specifically the details between the square brackets.
$schema = new Schema([...]);
Yii does the schema.php at runtime so I chose the above by simply copying the file from my runtime folder but I would still like to know how to do it through Cycle.
Yes I believe the outer key should be without the square brackets ie. id
and not [id]
so that when it is joined with the relation tax_rate
it creates the complete key tax_rate_id
but that is just my mind and I have not walked through the code yet.
How do I dump the entity?
$model->setTax_rate_id((int)$form->getTax_rate_id());
i mean try to change $model->setTax_rate_id((int)$form->getTax_rate_id());
to $model->setTax_rate( Tax_rate ); to establish the relations
$schema = new Schema([...]);
Yii does the schema.php at runtime so I chose the above by simply copying the file from my runtime folder but I would still like to know how to do it through Cycle.
It's OK. Yii3 uses the cycle/schema-renderer
package to generate schema.php
in schema-file-cache. You can use these commands to render the Cycle Schema - they also use cycle/schema-renderer
Ok, ran C:\wamp64\www\yii-invoice>yii cycle/schema and got the following.
[product] :: default.product
Entity: App\Invoice\Entity\Product
Mapper: Cycle\ORM\Mapper\Mapper
Repository: App\Invoice\Product\ProductRepository
Primary key: id
Fields:
(property -> db.field -> typecast)
id -> id -> int
product_sku -> product_sku
product_name -> product_name
product_description -> product_description
product_price -> product_price -> float
purchase_price -> purchase_price -> float
provider_name -> provider_name
family_id -> family_id -> int
tax_rate_id -> tax_rate_id -> int
unit_id -> unit_id -> int
product_tariff -> product_tariff -> int
Relations:
product->family belongs to family, lazy loading, cascaded
not null product.family_id <==> family.id
product->tax_rate belongs to taxRate, lazy loading, cascaded
not null product.tax_rate_id <==> taxRate.id
product->unit belongs to unit, lazy loading, cascaded
not null product.unit_id <==> unit.id
Yes I believe the outer key should be without the square brackets ie.
id
and not[id]
so that when it is joined with the relationtax_rate
it creates the complete keytax_rate_id
but that is just my mind and I have not walked through the code yet.
There PhpFileSchemaProvider
generator configured to read (like cache) and write schema. By the reason you can modify schema.php
manually to test your assumptions.
Also you should turn on the FromConveyorSchemaProvider
to generate new Schema based on attributes if Schema cache is clear.
'schema-providers' => [
// Uncomment next line to enable a Schema caching in the common cache
//\Yiisoft\Yii\Cycle\Schema\Provider\SimpleCacheSchemaProvider::class => ['key' => 'cycle-orm-cache-key'],
// Store generated Schema in the file
\Yiisoft\Yii\Cycle\Schema\Provider\PhpFileSchemaProvider::class => [
'mode' => \Yiisoft\Yii\Cycle\Schema\Provider\PhpFileSchemaProvider::MODE_READ_AND_WRITE,
'file' => 'runtime/schema.php',
],
\Yiisoft\Yii\Cycle\Schema\Provider\FromConveyorSchemaProvider::class => [
'generators' => [
// Cycle\Schema\Generator\SyncTables::class, // sync table changes to database
],
],
],
Ok, ran C:\wamp64\www\yii-invoice>yii cycle/schema and got the following.
Schema looks OK. We need to dump the Entity before changing and after it (before save).
service
public function saveProduct(Product $model, ProductForm $form): void
{
dump($model); // <<< HERE
$model->setProduct_sku($form->getProduct_sku());
$model->setProduct_name($form->getProduct_name());
$model->setProduct_description($form->getProduct_description());
$model->setProduct_price($form->getProduct_price());
$model->setPurchase_price($form->getPurchase_price());
$model->setProvider_name($form->getProvider_name());
$model->setProduct_tariff($form->getProduct_tariff());
$model->setTax_rate_id((int)$form->getTax_rate_id());
$model->setUnit_id((int)$form->getUnit_id());
$model->setFamily_id((int)$form->getFamily_id());
dump($model); // <<< AND HERE
$this->repository->save($model);
}
$dump_before
object(App\Invoice\Entity\Product Cycle ORM Proxy)#2611 (14) {
["id":"App\Invoice\Entity\Product":private]=> int(3)
["product_sku":"App\Invoice\Entity\Product":private]=> string(12) "dfghdfghdfgh"
["product_name":"App\Invoice\Entity\Product":private]=> string(15) "Window Cleaning"
["product_description":"App\Invoice\Entity\Product":private]=> string(7) "fasdfaf"
["product_price":"App\Invoice\Entity\Product":private]=> float(15)
["purchase_price":"App\Invoice\Entity\Product":private]=> float(7)
["provider_name":"App\Invoice\Entity\Product":private]=> string(0) ""
["family":"App\Invoice\Entity\Product":private]=> object(App\Invoice\Entity\Family Cycle ORM Proxy)#2614 (2) {
["id"]=> int(1) // 1
["family_name"]=> string(7) "Product" }
["family_id":"App\Invoice\Entity\Product":private]=> int(1) // 2
["tax_rate":"App\Invoice\Entity\Product":private]=> object(App\Invoice\Entity\TaxRate Cycle ORM Proxy)#2622 (4) {
["id":"App\Invoice\Entity\TaxRate":private]=> int(2) // 3
["tax_rate_name":"App\Invoice\Entity\TaxRate":private]=> string(8) "Standard"
["tax_rate_percent":"App\Invoice\Entity\TaxRate":private]=> float(20)
["tax_rate_default":"App\Invoice\Entity\TaxRate":private]=> bool(true) }
["tax_rate_id":"App\Invoice\Entity\Product":private]=> int(2) // 4
["unit":"App\Invoice\Entity\Product":private]=> object(App\Invoice\Entity\Unit Cycle ORM Proxy)#2624 (3) {
["id"]=> int(3) ["unit_name":"App\Invoice\Entity\Unit":private]=> string(10) "Third Unit" // 5
["unit_name_plrl":"App\Invoice\Entity\Unit":private]=> string(11) "Third units" }
["unit_id":"App\Invoice\Entity\Product":private]=> int(3) // 6
["product_tariff":"App\Invoice\Entity\Product":private]=> int(2) }
$dump_after
object(App\Invoice\Entity\Product Cycle ORM Proxy)#2611 (14) {
["id":"App\Invoice\Entity\Product":private]=> int(3)
["product_sku":"App\Invoice\Entity\Product":private]=> string(12) "dfghdfghdfgh"
["product_name":"App\Invoice\Entity\Product":private]=> string(15) "Window Cleaning"
["product_description":"App\Invoice\Entity\Product":private]=> string(7) "fasdfaf"
["product_price":"App\Invoice\Entity\Product":private]=> float(15)
["purchase_price":"App\Invoice\Entity\Product":private]=> float(7)
["provider_name":"App\Invoice\Entity\Product":private]=> string(0) ""
["family":"App\Invoice\Entity\Product":private]=> object(App\Invoice\Entity\Family Cycle ORM Proxy)#2614 (2) {
["id"]=> int(1) // 7
["family_name"]=> string(7) "Product" }
["family_id":"App\Invoice\Entity\Product":private]=> int(2) // 8
["tax_rate":"App\Invoice\Entity\Product":private]=> object(App\Invoice\Entity\TaxRate Cycle ORM Proxy)#2622 (4) {
["id":"App\Invoice\Entity\TaxRate":private]=> int(2) // 9
["tax_rate_name":"App\Invoice\Entity\TaxRate":private]=> string(8) "Standard"
["tax_rate_percent":"App\Invoice\Entity\TaxRate":private]=> float(20)
["tax_rate_default":"App\Invoice\Entity\TaxRate":private]=> bool(true) }
["tax_rate_id":"App\Invoice\Entity\Product":private]=> int(1) // 10
["unit":"App\Invoice\Entity\Product":private]=> object(App\Invoice\Entity\Unit Cycle ORM Proxy)#2624 (3) {
["id"]=> int(3) ["unit_name":"App\Invoice\Entity\Unit":private]=> string(10) "Third Unit" // 11
["unit_name_plrl":"App\Invoice\Entity\Unit":private]=> string(11) "Third units" }
["unit_id":"App\Invoice\Entity\Product":private]=> int(1) // 12
["product_tariff":"App\Invoice\Entity\Product":private]=> int(2) }
Using service:
public function saveProduct(Product $model, ProductForm $form): string
{
$dump_before = var_dump($model);
$model->setProduct_sku($form->getProduct_sku());
$model->setProduct_name($form->getProduct_name());
$model->setProduct_description($form->getProduct_description());
$model->setProduct_price($form->getProduct_price());
$model->setPurchase_price($form->getPurchase_price());
$model->setProvider_name($form->getProvider_name());
$model->setProduct_tariff($form->getProduct_tariff());
$model->setTax_rate_id((int)$form->getTax_rate_id());
$model->setUnit_id((int)$form->getUnit_id());
$model->setFamily_id((int)$form->getFamily_id());
$dump_after = var_dump($model);
$this->repository->save($model);
return $dump_before.$dump_after;
}
Controller:
if ($form->load($body) && $validator->validate($form)->isValid()) {
$dump = $this->productService->saveProduct($this->product($currentRoute, $pR), $form);
$this->flash('info', $dump);
return $this->webService->getRedirectResponse('product/index');
}
I only changed the relation keys ie.
dropdown for family changed from Product to Service, ie. family_id changed from 1 to 2. dropdown for tax_rate changed from Standard to Zero, ie. tax_rate_id changed from 2 to 1. dropdown for unit changed from unit/units to clean/cleans ie. unit_id changed from 3 to 1.
The id's for all of the above are being correctly changed by the form and service.
The service changes related entities ids in the Product but previous entities already loaded. Before save the Product entity has inconsistent state:
TaxRate
parent with id=2
and tax_rate_id=1
Famaly
with id=1
but family_id=2
Unit
with id=3
but unit_id=1
Can you try to make it consistent before save? I mean to unset related entities or set new related like
$product->tax_rate = $taxRate_with_id3;
Please can you use the forward slashes like // with a number after it eg. // 8 that I have put in the $dump_before and $dump_after at the end of each line in your comment above to understand you correctly.
Sorry for being pedantic / picky/ fussy but I think you have a typo in your comment:
it has TaxRate parent with id=1 and tax_rate_id=2
these should be <=> swopped.
I included the following into the Service and Entitys to resolve this problem basically providing setRelation functions to balance the getRelation functions. It just means we can initialize these to null before the product is saved.
Service
public function editProduct(Product $model, ProductForm $form): string
{
$dump_before = var_dump($model);
$model->setProduct_sku($form->getProduct_sku());
$model->setProduct_name($form->getProduct_name());
$model->setProduct_description($form->getProduct_description());
$model->setProduct_price($form->getProduct_price());
$model->setPurchase_price($form->getPurchase_price());
$model->setProvider_name($form->getProvider_name());
$model->setProduct_tariff($form->getProduct_tariff());
null!==$form->getTax_rate_id() ? $model->setTaxrate($model->getTaxrate()->getTax_rate_id() == $form->getTax_rate_id() ? $model->getTaxrate() : null): '';
$model->setTax_rate_id((int)$form->getTax_rate_id());
null!==$form->getUnit_id() ? $model->setUnit($model->getUnit()->getUnit_id() == $form->getUnit_id() ? $model->getUnit() : null) : '';
$model->setUnit_id((int)$form->getUnit_id());
null!== $form->getFamily_id() ?$model->setFamily($model->getFamily()->getFamily_id() == $form->getFamily_id() ? $model->getFamily() : null) : '';
$model->setFamily_id((int)$form->getFamily_id());
$dump_after = var_dump($model);
$this->repository->save($model);
return $dump_before.$dump_after;
}
public function addProduct(Product $model, ProductForm $form): void
{
$model->setProduct_sku($form->getProduct_sku());
$model->setProduct_name($form->getProduct_name());
$model->setProduct_description($form->getProduct_description());
$model->setProduct_price($form->getProduct_price());
$model->setPurchase_price($form->getPurchase_price());
$model->setProvider_name($form->getProvider_name());
$model->setProduct_tariff($form->getProduct_tariff());
$model->setTax_rate_id((int)$form->getTax_rate_id());
$model->setUnit_id((int)$form->getUnit_id());
$model->setFamily_id((int)$form->getFamily_id());
$this->repository->save($model);
}
Entity
//get relation $family
public function getFamily(): ?Family
{
return $this->family;
}
//set relation $family
public function setFamily(?Family $family): void
{
$this->family = $family;
}
//relation $tax_rate
public function getTaxrate(): ?TaxRate
{
return $this->tax_rate;
}
//set relation $taxrate
public function setTaxrate(?TaxRate $taxrate): void
{
$this->tax_rate = $taxrate;
}
//relation $unit
public function getUnit(): ?Unit
{
return $this->unit;
}
//set relation $unit
public function setUnit(?Unit $unit): void
{
$this->unit = $unit;
}
Ok the relations are saving now but new items added are experiencing an error. This is not going to be a quick solution. I have decided to create two separate functions in the service. One for adding and one for editing. This has solved my problem.
I will leave this open for a while just in case. Nevertheless, thanks a million for your help roxblnfk. Thank you for your willingness and determination.
Further reading: Here
No duplicates π₯².
What happened?
Ubuntu 22.04 LTS Testing and Wampserver 3.2.9 Testing Above error common to both Windows and Ubuntu Apache2 using fork rossaddison/yii-invoice. Event: A new product, not used in any quote, or invoice, when tax_rate_id BelongsTo dropdown edited and saved. Also if the product's family group BelongsTo relation changes, and is saved, same error occurs.
This error occurs when the record is edited, not created.
This error did not occur when I was using annotations instead of the current Atributes. This issue is similar to issue #329
Here is the entity that is being used:
Version