Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError Cannot assign Cycle\ORM\Reference\Reference to property App\Invoice\Entity\Quote::$client of type ?App\Invoice\Entity\Client #510

Closed
rossaddison opened this issue Oct 4, 2022 · 8 comments
Assignees

Comments

@rossaddison
Copy link
Contributor

rossaddison commented Oct 4, 2022

What steps will reproduce the problem? In src\Invoice\Entity\Quote.php

class Quote
{    
    #[BelongsTo(target:Client::class, nullable: false, fkAction:'NO ACTION')]     <----- changing the nullable: true to false
    public ?Client $client = null;    

    #[BelongsTo(target:Group::class, nullable: false, fkAction:'NO ACTION')]     <----- changing the nullable: true to false
    private ?Group $group = null;

What is the expected result? No error.

What do you get instead? The above error.

TypeError
Cannot assign Cycle\ORM\Reference\Reference to property App\Invoice\Entity\Quote::$client of type ?App\Invoice\Entity\Client

Additional info

Q A
Version mySql 8.0.27
PHP version 8.1.9
Operating system Windows 10
@rossaddison
Copy link
Contributor Author

rossaddison commented Oct 4, 2022

Here is the stack trace.

TypeError: Cannot assign Cycle\ORM\Reference\Reference to property App\Invoice\Entity\Quote::$client of type ?App\Invoice\Entity\Client in Project\vendor\cycle\orm\src\Mapper\Proxy\Hydrator\ClosureHydrator.php:35
Stack trace:
#0 Project\vendor\cycle\orm\src\Mapper\Proxy\ProxyEntityFactory.php(81): Cycle\ORM\Mapper\Proxy\Hydrator\ClosureHydrator->hydrate(Object(Cycle\ORM\RelationMap), Array, Object(App\Invoice\Entity\Quote), Array)
#1 Project\vendor\cycle\orm\src\Mapper\Mapper.php(47): Cycle\ORM\Mapper\Proxy\ProxyEntityFactory->upgrade(Object(Cycle\ORM\RelationMap), Object(App\Invoice\Entity\Quote), Array)
#2 Project\vendor\cycle\orm\src\Transaction\UnitOfWork.php(185): Cycle\ORM\Mapper\Mapper->hydrate(Object(App\Invoice\Entity\Quote), Array)
#3 Project\vendor\cycle\orm\src\Transaction\UnitOfWork.php(119): Cycle\ORM\Transaction\UnitOfWork->syncHeap()
#4 Project\vendor\cycle\orm\src\EntityManager.php(47): Cycle\ORM\Transaction\UnitOfWork->run()
#5 Project\vendor\yiisoft\yii-cycle\src\Data\Writer\EntityWriter.php(25): Cycle\ORM\EntityManager->run()
#6 Project\src\Invoice\Quote\QuoteRepository.php(76): Yiisoft\Yii\Cycle\Data\Writer\EntityWriter->write(Array)
#7 Project\src\Invoice\Quote\QuoteService.php(59): App\Invoice\Quote\QuoteRepository->save(Object(App\Invoice\Entity\Quote))
#8 Project\src\Invoice\Quote\QuoteController.php(285): App\Invoice\Quote\QuoteService->saveQuote(Object(App\User\User Cycle ORM Proxy), Object(App\Invoice\Entity\Quote), Object(App\Invoice\Quote\QuoteForm), Object(App\Invoice\Setting\SettingRepository))
#9 [internal function]: App\Invoice\Quote\QuoteController->create_confirm(Object(HttpSoft\Message\ServerRequest), Object(Yiisoft\Validator\Validator), Object(App\Invoice\Group\GroupRepository), Object(App\Invoice\TaxRate\TaxRateRepository), Object(App\Invoice\QuoteAmount\QuoteAmountRepository), Object(App\Handler\NotFoundHandler))
#10 Project\vendor\yiisoft\injector\src\Injector.php(68): ReflectionFunction->invokeArgs(Array)
#11 Project\vendor\yiisoft\middleware-dispatcher\src\MiddlewareFactory.php(108): Yiisoft\Injector\Injector->invoke(Object(Closure), Array)
#12 Project\vendor\yiisoft\middleware-dispatcher\src\MiddlewareStack.php(104): Psr\Http\Server\MiddlewareInterface@anonymous->process(Object(HttpSoft\Message\ServerRequest), Object(App\Handler\NotFoundHandler))
#13 Project\vendor\yiisoft\auth\src\Middleware\Authentication.php(57): Psr\Http\Server\RequestHandlerInterface@anonymous->handle(Object(HttpSoft\Message\ServerRequest))
#14 Project\vendor\yiisoft\middleware-dispatcher\src\MiddlewareStack.php(104): Yiisoft\Auth\Middleware\Authentication->process(Object(HttpSoft\Message\ServerRequest), Object(Psr\Http\Server\RequestHandlerInterface@anonymous))
#15 Project\src\Middleware\AccessChecker.php(35): Psr\Http\Server\RequestHandlerInterface@anonymous->handle(Object(HttpSoft\Message\ServerRequest))
#16 Project\vendor\yiisoft\middleware-dispatcher\src\MiddlewareFactory.php(145): App\Middleware\AccessChecker->process(Object(HttpSoft\Message\ServerRequest), Object(Psr\Http\Server\RequestHandlerInterface@anonymous))
#17 Project\vendor\yiisoft\middleware-dispatcher\src\MiddlewareStack.php(104): Psr\Http\Server\MiddlewareInterface@anonymous->process(Object(HttpSoft\Message\ServerRequest), Object(Psr\Http\Server\RequestHandlerInterface@anonymous))
#18 Project\vendor\yiisoft\yii-debug-viewer\src\Middleware\ToolbarMiddleware.php(38): Psr\Http\Server\RequestHandlerInterface@anonymous->handle(Object(HttpSoft\Message\ServerRequest))
#19 Project\vendor\yiisoft\middleware-dispatcher\src\MiddlewareStack.php(104): Yiisoft\Yii\Debug\Viewer\Middleware\ToolbarMiddleware->process(Object(HttpSoft\Message\ServerRequest), Object(Psr\Http\Server\RequestHandlerInterface@anonymous))
#20 Project\vendor\yiisoft\data-response\src\Middleware\FormatDataResponse.php(29): Psr\Http\Server\RequestHandlerInterface@anonymous->handle(Object(HttpSoft\Message\ServerRequest))
#21 Project\vendor\yiisoft\middleware-dispatcher\src\MiddlewareStack.php(104): Yiisoft\DataResponse\Middleware\FormatDataResponse->process(Object(HttpSoft\Message\ServerRequest), Object(Psr\Http\Server\RequestHandlerInterface@anonymous))
#22 Project\vendor\yiisoft\csrf\src\CsrfMiddleware.php(48): Psr\Http\Server\RequestHandlerInterface@anonymous->handle(Object(HttpSoft\Message\ServerRequest))
#23 Project\vendor\yiisoft\middleware-dispatcher\src\MiddlewareStack.php(104): Yiisoft\Csrf\CsrfMiddleware->process(Object(HttpSoft\Message\ServerRequest), Object(Psr\Http\Server\RequestHandlerInterface@anonymous))
#24 Project\vendor\yiisoft\middleware-dispatcher\src\MiddlewareStack.php(53): Psr\Http\Server\RequestHandlerInterface@anonymous->handle(Object(HttpSoft\Message\ServerRequest))
#25 Project\vendor\yiisoft\middleware-dispatcher\src\MiddlewareDispatcher.php(50): Yiisoft\Middleware\Dispatcher\MiddlewareStack->handle(Object(HttpSoft\Message\ServerRequest))
#26 Project\vendor\yiisoft\router\src\MatchingResult.php(118): Yiisoft\Middleware\Dispatcher\MiddlewareDispatcher->dispatch(Object(HttpSoft\Message\ServerRequest), Object(App\Handler\NotFoundHandler))
#27 Project\vendor\yiisoft\router\src\Middleware\Router.php(65): Yiisoft\Router\MatchingResult->process(Object(HttpSoft\Message\ServerRequest), Object(App\Handler\NotFoundHandler))
#28 Project\vendor\yiisoft\middleware-dispatcher\src\MiddlewareStack.php(104): Yiisoft\Router\Middleware\Router->process(Object(HttpSoft\Message\ServerRequest), Object(App\Handler\NotFoundHandler))
#29 Project\src\Middleware\LocaleMiddleware.php(84): Psr\Http\Server\RequestHandlerInterface@anonymous->handle(Object(HttpSoft\Message\ServerRequest))
#30 Project\vendor\yiisoft\middleware-dispatcher\src\MiddlewareStack.php(104): App\Middleware\LocaleMiddleware->process(Object(HttpSoft\Message\ServerRequest), Object(Psr\Http\Server\RequestHandlerInterface@anonymous))
#31 Project\vendor\yiisoft\user\src\Login\Cookie\CookieLoginMiddleware.php(71): Psr\Http\Server\RequestHandlerInterface@anonymous->handle(Object(HttpSoft\Message\ServerRequest))
#32 Project\vendor\yiisoft\middleware-dispatcher\src\MiddlewareStack.php(104): Yiisoft\User\Login\Cookie\CookieLoginMiddleware->process(Object(HttpSoft\Message\ServerRequest), Object(Psr\Http\Server\RequestHandlerInterface@anonymous))
#33 Project\vendor\yiisoft\cookies\src\CookieMiddleware.php(84): Psr\Http\Server\RequestHandlerInterface@anonymous->handle(Object(HttpSoft\Message\ServerRequest))
#34 Project\vendor\yiisoft\middleware-dispatcher\src\MiddlewareStack.php(104): Yiisoft\Cookies\CookieMiddleware->process(Object(HttpSoft\Message\ServerRequest), Object(Psr\Http\Server\RequestHandlerInterface@anonymous))
#35 Project\vendor\yiisoft\session\src\SessionMiddleware.php(36): Psr\Http\Server\RequestHandlerInterface@anonymous->handle(Object(HttpSoft\Message\ServerRequest))
#36 Project\vendor\yiisoft\middleware-dispatcher\src\MiddlewareStack.php(104): Yiisoft\Session\SessionMiddleware->process(Object(HttpSoft\Message\ServerRequest), Object(Psr\Http\Server\RequestHandlerInterface@anonymous))
#37 Project\vendor\yiisoft\yii-sentry\src\SentryMiddleware.php(27): Psr\Http\Server\RequestHandlerInterface@anonymous->handle(Object(HttpSoft\Message\ServerRequest))
#38 Project\vendor\yiisoft\middleware-dispatcher\src\MiddlewareStack.php(104): Yiisoft\Yii\Sentry\SentryMiddleware->process(Object(HttpSoft\Message\ServerRequest), Object(Psr\Http\Server\RequestHandlerInterface@anonymous))
#39 Project\vendor\yiisoft\error-handler\src\Middleware\ErrorCatcher.php(135): Psr\Http\Server\RequestHandlerInterface@anonymous->handle(Object(HttpSoft\Message\ServerRequest))
#40 Project\vendor\yiisoft\middleware-dispatcher\src\MiddlewareStack.php(104): Yiisoft\ErrorHandler\Middleware\ErrorCatcher->process(Object(HttpSoft\Message\ServerRequest), Object(Psr\Http\Server\RequestHandlerInterface@anonymous))
#41 Project\vendor\yiisoft\middleware-dispatcher\src\MiddlewareStack.php(53): Psr\Http\Server\RequestHandlerInterface@anonymous->handle(Object(HttpSoft\Message\ServerRequest))
#42 Project\vendor\yiisoft\middleware-dispatcher\src\MiddlewareDispatcher.php(50): Yiisoft\Middleware\Dispatcher\MiddlewareStack->handle(Object(HttpSoft\Message\ServerRequest))
#43 Project\vendor\yiisoft\yii-http\src\Application.php(87): Yiisoft\Middleware\Dispatcher\MiddlewareDispatcher->dispatch(Object(HttpSoft\Message\ServerRequest), Object(App\Handler\NotFoundHandler))
#44 Project\vendor\yiisoft\yii-runner-http\src\HttpApplicationRunner.php(105): Yiisoft\Yii\Http\Application->handle(Object(HttpSoft\Message\ServerRequest))
#45 Project\public\index.php(36): Yiisoft\Yii\Runner\Http\HttpApplicationRunner->run()
#46 {main}

@rossaddison
Copy link
Contributor Author

rossaddison commented Oct 4, 2022

The issue has been resolved by simply changing the nullable: false to true. The Invoice entity, which is very similar to the Quote entity however has its nullable: set to false for the very same foreign keys and is running without this error. So this is confusing. Is it related to the schema perhaps?

@roxblnfk
Copy link
Member

roxblnfk commented Oct 4, 2022

There are no enough code to reproduce or to understand the issue in description.

@rossaddison
Copy link
Contributor Author

rossaddison commented Oct 4, 2022

Yes I agree there is very little to understand in the description of the error. I am not using any mapper. How would the mapper help me do you think? Basically a quote is created if the client_id is supplied and the quote group rather than the invoice group is chosen.

Entity: Quote
QuoteForm:

Sequence of events:

  1. modal_create_quote ...quote.js picks up the variables and passes them to quote/create_confirm.
  2. QuoteController/create_confirm
  3. QuoteService/addQuote

If nullable is set to true in the Entity, the quote can be added through the modal. However when it comes to editing either the client or the group using the quote/_form I get a 1452 integrity constraint violation error.

The invoice is working perfectly and you can look at the code in the fork but for some reason the quote is not at this stage. I have uploaded recent changes to the fork.

Under the QuoteService/saveQuote I have had to initialize the relation with a eg. setClient(null) command which works in the
InvService/saveInv function and ensures that the invoice can be edited. However this is not the case with the Quote.

@roxblnfk
Copy link
Member

roxblnfk commented Oct 4, 2022

I think the exception has been thrown because an Entity class has been used directly instead of EntityProxy. But why i see ProxyEntityFactory in the stack in this case?

@rossaddison
Copy link
Contributor Author

rossaddison commented Oct 4, 2022

Here is the config/params:

 'orm-promise-factory' => null,
        //'orm-promise-factory' => \Cycle\ORM\Promise\ProxyFactory::class, 
        /**
         * A list of DB schema providers for {@see \Yiisoft\Yii\Cycle\Schema\Provider\Support\SchemaProviderPipeline}
         * Providers are implementing {@see SchemaProviderInterface}.
         * The configuration is an array of provider class names. Alternatively, you can specify provider class as key
         * and its config as value:
         */

 '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
                ],
            ],
        ],

@rossaddison
Copy link
Contributor Author

rossaddison commented Oct 4, 2022

Changes are made directly in the Entities and I keep the :

\Yiisoft\Yii\Cycle\Schema\Provider\FromConveyorSchemaProvider::class => [
'generators' => [
Cycle\Schema\Generator\SyncTables::class, // sync table changes to database
],
],

setting enabled in case these changes to the entity ie. Quote, need to be synced to the database.

There is currently no schema file that has been generated. I am trying to isolate with as few variables as possible and I have never used a ProxyFactory.

@rossaddison
Copy link
Contributor Author

This issue has been solved. Typo public should be private.

public ?Client $client = null; <-------- change public to private.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants