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

Typed property must not be accessed before initialization #162

Closed
anlamas opened this issue Feb 21, 2021 · 8 comments
Closed

Typed property must not be accessed before initialization #162

anlamas opened this issue Feb 21, 2021 · 8 comments
Labels
status:wontfix This will not be worked on type:question Further information is requested

Comments

@anlamas
Copy link

anlamas commented Feb 21, 2021

Использую PHP 7.4
Обычные сущности для Many To Many.
Пытаюсь связать существующие записи.
Ругается на типизированное свойство в pivot entity при сохранении.
Typed property App\CategoryContractor::$id must not be accessed before initialization

/**
 * @Cycle\Entity(table="category_contractor")
 */
class CategoryContractor
{
    /**
     * @Cycle\Column(type="primary")
     */
    public int $id;
}
@wolfy-j
Copy link
Contributor

wolfy-j commented Feb 21, 2021

Can you provide the ORM version, list of connected entities and full stack trace.

@anlamas
Copy link
Author

anlamas commented Feb 21, 2021

name     : cycle/orm
descrip. : PHP DataMapper ORM and Data Modelling Engine
keywords :
versions : * v1.3.3
<?php

namespace App;

use Cycle\Annotated\Annotation as Cycle;
use Cycle\ORM\Relation\Pivoted\PivotedCollection;
use Doctrine\Common\Collections\Collection;

/**
 * @Cycle\Entity()
 */
class Category
{
    /**
     * @Cycle\Column(type="primary")
     */
    protected int $id;


    /**
     * @Cycle\Relation\ManyToMany(target="Contractor", though="CategoryContractor")
     */
    protected Collection $contractors;

    public function __construct()
    {
        $this->contractors = new PivotedCollection();
    }

    public function getId(): int
    {
        return $this->id;
    }

    public function setId(int $id): Category
    {
        $this->id = $id;
        return $this;
    }
   
    /**
     * @return PivotedCollection
     */
    public function getContractors(): Collection
    {
        return $this->contractors;
    }
}
<?php

namespace App;

use Cycle\Annotated\Annotation as Cycle;
use Cycle\ORM\Relation\Pivoted\PivotedCollection;
use Doctrine\Common\Collections\Collection;

/**
 * @Cycle\Entity()
 */
class Contractor
{
    /**
     * @Cycle\Column(type="primary")
     */
    protected int $id;

    /**
     * @Cycle\Relation\ManyToMany(target="Category", though="CategoryContractor")
     */
    protected Collection $categories;

    public function __construct()
    {
        $this->categories = new PivotedCollection();
    }

    public function getId(): int
    {
        return $this->id;
    }

    public function setId(int $id): Contractor
    {
        $this->id = $id;
        return $this;
    }

    /**
     * @return PivotedCollection
     */
    public function getCategories(): Collection
    {
        return $this->categories;
    }
}
<?php

namespace App;

use Cycle\Annotated\Annotation as Cycle;

/**
 * @Cycle\Entity(table="category_contractor")
 */
class CategoryContractor
{
    /**
     * @Cycle\Column(type="primary")
     */
    public int $id;
}
PHP Fatal error:  Uncaught Error: Typed property App\CategoryContractor::$id must not be accessed before initialization in /home/vagrant/code/demo/www/vendor/laminas/laminas-hydrator/src/ReflectionHydrator.php:41
Stack trace:
#0 /home/vagrant/code/demo/www/vendor/laminas/laminas-hydrator/src/ReflectionHydrator.php(41): ReflectionProperty->getValue()
#1 /home/vagrant/code/demo/www/vendor/cycle/orm/src/Mapper/Mapper.php(81): Laminas\Hydrator\ReflectionHydrator->extract()
#2 /home/vagrant/code/demo/www/vendor/cycle/orm/src/Mapper/Mapper.php(92): Cycle\ORM\Mapper\Mapper->extract()
#3 /home/vagrant/code/demo/www/vendor/cycle/orm/src/Mapper/DatabaseMapper.php(91): Cycle\ORM\Mapper\Mapper->fetchFields()
#4 /home/vagrant/code/demo/www/vendor/cycle/orm/src/CommandGenerator.php(33): Cycle\ORM\Mapper\DatabaseMapper->queueCreate()
#5 /home/vagrant/code/demo/www/vendor/cycle/orm/src/ORM.php(344): Cycle\ORM\CommandGenerator->generateStore()
#6 /home/vagrant/code/demo/www/vendor in /home/vagrant/code/demo/www/vendor/laminas/laminas-hydrator/src/ReflectionHydrator.php on line 41

Fatal error: Uncaught Error: Typed property App\CategoryContractor::$id must not be accessed before initialization in /home/vagrant/code/demo/www/vendor/laminas/laminas-hydrator/src/ReflectionHydrator.php:41
Stack trace:
#0 /home/vagrant/code/demo/www/vendor/laminas/laminas-hydrator/src/ReflectionHydrator.php(41): ReflectionProperty->getValue()
#1 /home/vagrant/code/demo/www/vendor/cycle/orm/src/Mapper/Mapper.php(81): Laminas\Hydrator\ReflectionHydrator->extract()
#2 /home/vagrant/code/demo/www/vendor/cycle/orm/src/Mapper/Mapper.php(92): Cycle\ORM\Mapper\Mapper->extract()
#3 /home/vagrant/code/demo/www/vendor/cycle/orm/src/Mapper/DatabaseMapper.php(91): Cycle\ORM\Mapper\Mapper->fetchFields()
#4 /home/vagrant/code/demo/www/vendor/cycle/orm/src/CommandGenerator.php(33): Cycle\ORM\Mapper\DatabaseMapper->queueCreate()
#5 /home/vagrant/code/demo/www/vendor/cycle/orm/src/ORM.php(344): Cycle\ORM\CommandGenerator->generateStore()
#6 /home/vagrant/code/demo/www/vendor in /home/vagrant/code/demo/www/vendor/laminas/laminas-hydrator/src/ReflectionHydrator.php on line 41

@anlamas
Copy link
Author

anlamas commented Feb 21, 2021

What i'm trying to do

foreach ($contractors as $contractor) {
    foreach ($categories as $category) {
            $contractor->getCategories()->add($category);
            $tr->persist($contractor);
        }
}

$tr->run();

As a temporal solution i removed type int from property $id

@roxblnfk
Copy link
Member

As a temporal solution i removed type int from property $id

Как временное решение можно также определять поле как ?int $id = null. В какой-то степени это даже логично.

@wolfy-j
Copy link
Contributor

wolfy-j commented Feb 22, 2021

Если I’d генерируясь базой то он должен быть nullable. По другому ОРМ просто не знает где взять данные.

@wolfy-j wolfy-j added type:question Further information is requested status:wontfix This will not be worked on labels Mar 31, 2021
@adamdyson
Copy link

/**
 * @Column(type = "primary")
 */
protected ?int $id = null;

@wolfy-j
Copy link
Contributor

wolfy-j commented Jul 26, 2021

@roxblnfk

@roxblnfk
Copy link
Member

@wolfy-j what?

@anlamas anlamas closed this as completed Oct 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status:wontfix This will not be worked on type:question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants