我有一个大学班级实体和一个学生(用户)实体。一个用户可以是许多班级的成员,班级可以有许多学生。(多头)
我创建了多对多,并且它似乎持续存在。我可以获得类 ( $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();
这是一个var_dump
of the $user->getClasses()
,它基本上是空的
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