0
我有一個大學課程實體和一個學生(用戶)實體。用戶可以是許多班級的成員,班級可以有很多學生。 (Manytomany)Doctrine2:ManyToMany沒有提供正確的列表
我創建了多對多,它似乎堅持罰款。我可以得到該課程的用戶($class->getUsers()
),但$user->getClasses()
似乎不起作用。 (重要的是:我的實體被稱爲Classes
,因爲PHP埋怨被保留名稱Class
)
這是從我的用戶實體:
/**
* @ORM\ManyToMany(targetEntity="Confidence\ClassBundle\Entity\Classes", inversedBy="users")
* @ORM\JoinTable(name="users_classes",
* joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="classes_id", referencedColumnName="id")}
*)
*/
protected $classes;
.
.
.
.
public function __construct()
{
$this->classes = new ArrayCollection();
}
.
.
.
.
.
/**
* Get classes
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getClasses()
{
return $this->classes;
}
,這是從類實體
/**
* @ORM\ManyToMany(targetEntity="Confidence\UserBundle\Entity\User", mappedBy="classes")
*/
protected $users;
.
.
.
function __construct()
{
$this->users = new ArrayCollection();
}
.
.
.
.
/**
* Get classes
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getUsers()
{
return $this->users;
}
這是symfony2控制器的一部分,在這裏堅持不懈
......
unset($classArray['product_ids']);
$class = new Classes();
$class->fromArray($classArray);
$loggedInUser->addClasses($class);
$class->addUser($loggedInUser);
$em->persist($loggedInUser);
$em->persist($class);
foreach ($productIds as $product) {
$prod = $productRepo->find($product);
if ($prod === null) {
throw new \Confidence\CoreBundle\Exception\NotFoundException();
}
$prod->addClasses($class);
$em->persist($prod);
}
$em->flush();
return $class;
......
而且這是我做的var_dump部分或我運行Xdebug的觀察點
/* @var $user \Confidence\UserBundle\Entity\User */
$user = $this->getEntityManager()->getRepository('ConfidenceUserBundle:User')->find($this->getSession()->getUserId());
$classes = $user->getClasses();
var_dump($classes);die();
這裏是$user->getClasses()
的var_dump
,這基本上是空的
object(Doctrine\ORM\PersistentCollection)[268]
private 'snapshot' =>
array (size=0)
empty
private 'owner' =>
object(Confidence\UserBundle\Entity\User)[259]
protected 'id' => int 39533
protected 'token' => null
protected 'tokenExpiration' => null
protected 'canvasId' => int 6870
protected 'oauthToken' => string 'fTn2CCK7BY4uaUN6h71q78xRwnn0Cx43T3JgxBjRNJMBBu4LpvHLRvkRmuUrJ5md' (length=64)
protected 'classes' =>
&object(Doctrine\ORM\PersistentCollection)[268]
protected 'lastLogin' => int 0
protected 'systemTime' => int 1375694342
protected 'products' =>
array (size=0)
empty
protected 'dtoType' => null
private 'association' =>
array (size=19)
'fieldName' => string 'classes' (length=7)
'joinTable' =>
array (size=4)
'name' => string 'users_classes' (length=13)
'schema' => null
'joinColumns' =>
array (size=1)
...
'inverseJoinColumns' =>
array (size=1)
...
'targetEntity' => string 'Confidence\ClassBundle\Entity\Classes' (length=31)
'mappedBy' => null
'inversedBy' => string 'users' (length=5)
'cascade' =>
array (size=0)
empty
'orphanRemoval' => boolean false
'fetch' => int 2
'type' => int 8
'isOwningSide' => boolean true
'sourceEntity' => string 'Confidence\UserBundle\Entity\User' (length=27)
'isCascadeRemove' => boolean false
'isCascadePersist' => boolean false
'isCascadeRefresh' => boolean false
'isCascadeMerge' => boolean false
'isCascadeDetach' => boolean false
'joinTableColumns' =>
array (size=2)
0 => string 'user_id' (length=7)
1 => string 'classes_id' (length=10)
'relationToSourceKeyColumns' =>
array (size=1)
'user_id' => string 'id' (length=2)
'relationToTargetKeyColumns' =>
array (size=1)
'classes_id' => string 'id' (length=2)
private 'em' =>
object(Doctrine\ORM\EntityManager)[132]
private 'config' =>
object(Doctrine\ORM\Configuration)[116]
protected '_attributes' =>
array (size=12)
...
private 'conn' =>
object(Doctrine\DBAL\Connection)[129]
protected '_conn' =>
object(Doctrine\DBAL\Driver\PDOConnection)[249]
...
protected '_config' =>
object(Doctrine\DBAL\Configuration)[126]
...
protected '_eventManager' =>
object(Symfony\Bridge\Doctrine\ContainerAwareEventManager)[127]
...
protected '_expr' =>
object(Doctrine\DBAL\Query\Expression\ExpressionBuilder)[130]
...
private '_isConnected' => boolean true
private '_transactionNestingLevel' => int 0
private '_transactionIsolationLevel' => int 2
private '_nestTransactionsWithSavepoints' => null
private '_params' =>
array (size=8)
...
protected '_platform' =>
object(Doctrine\DBAL\Platforms\MySqlPlatform)[131]
...
protected '_schemaManager' => null
protected '_driver' =>
object(Doctrine\DBAL\Driver\PDOMySql\Driver)[128]
...
private '_isRollbackOnly' => boolean false
private '_defaultFetchMode' => int 2
private 'metadataFactory' =>
object(Doctrine\ORM\Mapping\ClassMetadataFactory)[133]
private 'em' =>
&object(Doctrine\ORM\EntityManager)[132]
private 'targetPlatform' =>
object(Doctrine\DBAL\Platforms\MySqlPlatform)[131]
...
private 'driver' =>
object(Doctrine\ORM\Mapping\Driver\DriverChain)[221]
...
private 'evm' =>
object(Symfony\Bridge\Doctrine\ContainerAwareEventManager)[127]
...
protected 'cacheSalt' => string '$CLASSMETADATA' (length=14)
private 'cacheDriver' (Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory) =>
object(Doctrine\Common\Cache\ArrayCache)[117]
...
private 'loadedMetadata' (Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory) =>
array (size=3)
...
protected 'initialized' => boolean true
private 'reflectionService' (Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory) =>
object(Doctrine\Common\Persistence\Mapping\RuntimeReflectionService)[137]
...
private 'repositories' =>
array (size=1)
'ConfidenceUserBundle:User' =>
object(Doctrine\ORM\EntityRepository)[108]
...
private 'unitOfWork' =>
object(Doctrine\ORM\UnitOfWork)[134]
private 'identityMap' =>
array (size=1)
...
private 'entityIdentifiers' =>
array (size=1)
...
private 'originalEntityData' =>
array (size=1)
...
private 'entityChangeSets' =>
array (size=0)
...
private 'entityStates' =>
array (size=1)
...
private 'scheduledForDirtyCheck' =>
array (size=0)
...
private 'entityInsertions' =>
array (size=0)
...
private 'entityUpdates' =>
array (size=0)
...
private 'extraUpdates' =>
array (size=0)
...
private 'entityDeletions' =>
array (size=0)
...
private 'collectionDeletions' =>
array (size=0)
...
private 'collectionUpdates' =>
array (size=0)
...
private 'visitedCollections' =>
array (size=0)
...
private 'em' =>
&object(Doctrine\ORM\EntityManager)[132]
private 'commitOrderCalculator' => null
private 'persisters' =>
array (size=1)
...
private 'collectionPersisters' =>
array (size=0)
...
private 'evm' =>
object(Symfony\Bridge\Doctrine\ContainerAwareEventManager)[127]
...
private 'orphanRemovals' =>
array (size=0)
...
private 'readOnlyObjects' =>
array (size=0)
...
private 'eagerLoadingEntities' =>
array (size=0)
...
private 'eventManager' =>
object(Symfony\Bridge\Doctrine\ContainerAwareEventManager)[127]
private 'listeners' =>
array (size=0)
...
private 'initialized' =>
array (size=0)
...
private 'container' =>
object(appProdProjectContainer)[28]
...
private '_listeners' (Doctrine\Common\EventManager) =>
array (size=0)
...
private 'hydrators' =>
array (size=0)
empty
private 'proxyFactory' =>
object(Doctrine\ORM\Proxy\ProxyFactory)[135]
private '_em' =>
&object(Doctrine\ORM\EntityManager)[132]
private '_autoGenerate' => boolean false
private '_proxyNamespace' => string 'Proxies' (length=7)
private '_proxyDir' => string '/opt/arran/jura-backend/app/cache/prod/doctrine/orm/Proxies' (length=59)
private 'expressionBuilder' => null
private 'closed' => boolean false
private 'filterCollection' =>
object(Doctrine\ORM\Query\FilterCollection)[255]
private 'config' =>
object(Doctrine\ORM\Configuration)[116]
...
private 'em' =>
&object(Doctrine\ORM\EntityManager)[132]
private 'enabledFilters' =>
array (size=0)
...
private 'filterHash' => null
private 'filtersState' => int 1
private 'backRefFieldName' => string 'users' (length=5)
private 'typeClass' =>
object(Doctrine\ORM\Mapping\ClassMetadata)[260]
public 'name' => string 'Confidence\ClassBundle\Entity\Classes' (length=31)
public 'namespace' => string 'Confidence\ClassBundle\Entity' (length=23)
public 'rootEntityName' => string 'Confidence\ClassBundle\Entity\Classes' (length=31)
public 'customGeneratorDefinition' => null
public 'customRepositoryClassName' => null
public 'isMappedSuperclass' => boolean false
public 'parentClasses' =>
array (size=0)
empty
public 'subClasses' =>
array (size=0)
empty
public 'namedQueries' =>
array (size=0)
empty
public 'namedNativeQueries' =>
array (size=0)
empty
public 'sqlResultSetMappings' =>
array (size=0)
empty
public 'identifier' =>
array (size=1)
0 => string 'id' (length=2)
public 'inheritanceType' => int 1
public 'generatorType' => int 4
public 'fieldMappings' =>
array (size=7)
'id' =>
array (size=9)
...
'password' =>
array (size=8)
...
'publicDescription' =>
array (size=8)
...
'created' =>
array (size=9)
...
'startAt' =>
array (size=9)
...
'endAt' =>
array (size=9)
...
'name' =>
array (size=8)
...
public 'fieldNames' =>
array (size=7)
'id' => string 'id' (length=2)
'password' => string 'password' (length=8)
'publicDescription' => string 'publicDescription' (length=17)
'created' => string 'created' (length=7)
'startAt' => string 'startAt' (length=7)
'endAt' => string 'endAt' (length=5)
'name' => string 'name' (length=4)
public 'columnNames' =>
array (size=7)
'id' => string 'id' (length=2)
'password' => string 'password' (length=8)
'publicDescription' => string 'publicDescription' (length=17)
'created' => string 'created' (length=7)
'startAt' => string 'startAt' (length=7)
'endAt' => string 'endAt' (length=5)
'name' => string 'name' (length=4)
public 'discriminatorValue' => null
public 'discriminatorMap' =>
array (size=0)
empty
public 'discriminatorColumn' => null
public 'table' =>
array (size=2)
'name' => string 'classes' (length=7)
'options' =>
array (size=0)
...
public 'lifecycleCallbacks' =>
array (size=0)
empty
public 'associationMappings' =>
array (size=2)
'products' =>
array (size=16)
...
'users' =>
array (size=16)
...
public 'isIdentifierComposite' => boolean false
public 'containsForeignIdentifier' => boolean false
public 'idGenerator' =>
object(Doctrine\ORM\Id\IdentityGenerator)[277]
private '_seqName' => null
public 'sequenceGeneratorDefinition' => null
public 'tableGeneratorDefinition' => null
public 'changeTrackingPolicy' => int 1
public 'isVersioned' => null
public 'versionField' => null
public 'reflClass' =>
object(ReflectionClass)[267]
public 'name' => string 'Confidence\ClassBundle\Entity\Classes' (length=31)
public 'isReadOnly' => boolean false
protected 'namingStrategy' =>
object(Doctrine\ORM\Mapping\DefaultNamingStrategy)[124]
public 'reflFields' =>
array (size=9)
'id' =>
object(ReflectionProperty)[276]
...
'password' =>
object(ReflectionProperty)[261]
...
'publicDescription' =>
object(ReflectionProperty)[275]
...
'created' =>
object(ReflectionProperty)[274]
...
'startAt' =>
object(ReflectionProperty)[273]
...
'endAt' =>
object(ReflectionProperty)[272]
...
'name' =>
object(ReflectionProperty)[271]
...
'products' =>
object(ReflectionProperty)[270]
...
'users' =>
object(ReflectionProperty)[269]
...
private '_prototype' (Doctrine\ORM\Mapping\ClassMetadataInfo) => null
private 'isDirty' => boolean false
private 'initialized' => boolean false
private 'coll' =>
object(Doctrine\Common\Collections\ArrayCollection)[266]
private '_elements' =>
array (size=0)
empty