私には 2 つのエンティティがAufgabeありVersion、oneToMany-Relation (1 つVersionは many を持つことができますAufgaben) を使用していますが、それはオプションであり、割り当てられAufgabeている必要がないことを意味します。Version両方の YAML マッピングを次に示します。
バージョン:
evenos\Bundle\TimeBundle\Entity\Version:
type: entity
repositoryClass: evenos\Bundle\TimeBundle\Repository\VersionRepository
table: version
id:
id:
type: integer
Projekt:
associationKey: true
fields:
nummer:
type: string
length: 20
nullable: false
beschreibung:
type: text
nullable: true
faktor:
type: decimal
precision: 2
scale: 1
uniqueConstraints:
version_nr_unique:
columns: nummer
oneToMany:
Aufgaben:
targetEntity: Aufgabe
mappedBy: Version
[...]
アウフガベ:
evenos\Bundle\TimeBundle\Entity\Aufgabe:
type: entity
repositoryClass: evenos\Bundle\TimeBundle\Repository\AufgabeRepository
table: aufgabe
id:
id:
type: integer
generator:
strategy: AUTO
fields:
name:
type: string
length: 40
beschreibung:
type: text
nullable: true
faktor:
type: decimal
precision: 2
scale: 1
soll_aufwand:
type: integer
oneToMany:
[...]
manyToOne:
[...]
Version:
targetEntity: Version
inversedBy: Aufgaben
joinColumns:
- name: projekt_nummer
nullable: true
referencedColumnName: projekt_nummer
- name: version_id
nullable: true
referencedColumnName: id
私AufgabeRepositoryにはカスタムメソッドがありますfindAufgabenByProjektAndVersion($projekt = null, $version = null)-$versionがバージョンオブジェクトの場合、$version->getId()クエリにその ID を使用し、それ以外の場合は検索します"[...] where version is null"。
問題:Aufgabeからへの関連付けVersionはオプション ( nullable: true) であるため、場合によって$whatever->getAufgabe()->getVersion()はnull. というかそうあるべきです。代わりに、これをリポジトリ関数に渡し、セットAufgabeがないVersion場合、エラーが発生します。
Notice: Undefined index: id in /home/manuel/project/zeiterfassung/dev/app/cache/dev/doctrine/orm/Proxies/__CG__evenosBundleTimeBundleEntityVersion.php line 54
プロキシクラスを調べたところ、言及されている行は次のとおりです。
public function getId()
{
if ($this->__isInitialized__ === false) {
return (int) $this->_identifier["id"];
}
$this->__load();
return parent::getId();
}
明らかにプロキシオブジェクトは初期化されていません...しかし、これが割り当てられていない場合は..->getAufgabe()->getVersion()返されるべきではありませんか?NULLAufgabeVersion
null ではなく初期化されていないプロキシであるため、カスタム finder 関数は有効なバージョンであると認識しています ...
...
どこかで、私は何かひどく間違ったことをしているのではないかと心配していますが、今はそれを見ていません:)何かを読み間違えてnullable: true、関連付けが許可されていないのでしょうか?
更新:の私のfindAufgabenByProjektAndVersion方法AufgabeRepository:
public function findAufgabenByProjektAndVersion(Projekt $projekt = NULL, Version $version = NULL)
{
$qb = $this->getEntityManager()->createQueryBuilder()->addSelect('A')
->from('evenosTimeBundle:Aufgabe', 'A')
->leftJoin('A.Version', 'V');
if (is_null($projekt))
{
$qb->where('A.Projekt is null');
}
else
{
$qb->where('A.Projekt is null OR A.Projekt = :projekt')->setParameter('projekt', $projekt);
}
if (is_null($version))
{
$qb->andWhere('V.nummer is null');
}
else
{
$qb->andWhere('V.nummer is null OR (V.Projekt = :vprojekt AND V.id = :vid)')
->setParameter('vprojekt', $projekt)
->setParameter('vid', $version->getId());
}
return $qb->getQuery()->getResult();
}
Version明示的な-keysをクエリする必要があったのは、Doctrine から、複合キーがある場合elseは検索できない(エラーがスローされた) とのことだったからです。p.Version = :version