ClassMetadataInfo.php 63KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the LGPL. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\ORM\Mapping;
  20. use Doctrine\Common\Persistence\Mapping\ClassMetadata;
  21. use ReflectionClass;
  22. /**
  23. * A <tt>ClassMetadata</tt> instance holds all the object-relational mapping metadata
  24. * of an entity and it's associations.
  25. *
  26. * Once populated, ClassMetadata instances are usually cached in a serialized form.
  27. *
  28. * <b>IMPORTANT NOTE:</b>
  29. *
  30. * The fields of this class are only public for 2 reasons:
  31. * 1) To allow fast READ access.
  32. * 2) To drastically reduce the size of a serialized instance (private/protected members
  33. * get the whole class name, namespace inclusive, prepended to every property in
  34. * the serialized representation).
  35. *
  36. * @author Roman Borschel <roman@code-factory.org>
  37. * @author Jonathan H. Wage <jonwage@gmail.com>
  38. * @since 2.0
  39. */
  40. class ClassMetadataInfo implements ClassMetadata
  41. {
  42. /* The inheritance mapping types */
  43. /**
  44. * NONE means the class does not participate in an inheritance hierarchy
  45. * and therefore does not need an inheritance mapping type.
  46. */
  47. const INHERITANCE_TYPE_NONE = 1;
  48. /**
  49. * JOINED means the class will be persisted according to the rules of
  50. * <tt>Class Table Inheritance</tt>.
  51. */
  52. const INHERITANCE_TYPE_JOINED = 2;
  53. /**
  54. * SINGLE_TABLE means the class will be persisted according to the rules of
  55. * <tt>Single Table Inheritance</tt>.
  56. */
  57. const INHERITANCE_TYPE_SINGLE_TABLE = 3;
  58. /**
  59. * TABLE_PER_CLASS means the class will be persisted according to the rules
  60. * of <tt>Concrete Table Inheritance</tt>.
  61. */
  62. const INHERITANCE_TYPE_TABLE_PER_CLASS = 4;
  63. /* The Id generator types. */
  64. /**
  65. * AUTO means the generator type will depend on what the used platform prefers.
  66. * Offers full portability.
  67. */
  68. const GENERATOR_TYPE_AUTO = 1;
  69. /**
  70. * SEQUENCE means a separate sequence object will be used. Platforms that do
  71. * not have native sequence support may emulate it. Full portability is currently
  72. * not guaranteed.
  73. */
  74. const GENERATOR_TYPE_SEQUENCE = 2;
  75. /**
  76. * TABLE means a separate table is used for id generation.
  77. * Offers full portability.
  78. */
  79. const GENERATOR_TYPE_TABLE = 3;
  80. /**
  81. * IDENTITY means an identity column is used for id generation. The database
  82. * will fill in the id column on insertion. Platforms that do not support
  83. * native identity columns may emulate them. Full portability is currently
  84. * not guaranteed.
  85. */
  86. const GENERATOR_TYPE_IDENTITY = 4;
  87. /**
  88. * NONE means the class does not have a generated id. That means the class
  89. * must have a natural, manually assigned id.
  90. */
  91. const GENERATOR_TYPE_NONE = 5;
  92. /**
  93. * DEFERRED_IMPLICIT means that changes of entities are calculated at commit-time
  94. * by doing a property-by-property comparison with the original data. This will
  95. * be done for all entities that are in MANAGED state at commit-time.
  96. *
  97. * This is the default change tracking policy.
  98. */
  99. const CHANGETRACKING_DEFERRED_IMPLICIT = 1;
  100. /**
  101. * DEFERRED_EXPLICIT means that changes of entities are calculated at commit-time
  102. * by doing a property-by-property comparison with the original data. This will
  103. * be done only for entities that were explicitly saved (through persist() or a cascade).
  104. */
  105. const CHANGETRACKING_DEFERRED_EXPLICIT = 2;
  106. /**
  107. * NOTIFY means that Doctrine relies on the entities sending out notifications
  108. * when their properties change. Such entity classes must implement
  109. * the <tt>NotifyPropertyChanged</tt> interface.
  110. */
  111. const CHANGETRACKING_NOTIFY = 3;
  112. /**
  113. * Specifies that an association is to be fetched when it is first accessed.
  114. */
  115. const FETCH_LAZY = 2;
  116. /**
  117. * Specifies that an association is to be fetched when the owner of the
  118. * association is fetched.
  119. */
  120. const FETCH_EAGER = 3;
  121. /**
  122. * Specifies that an association is to be fetched lazy (on first access) and that
  123. * commands such as Collection#count, Collection#slice are issued directly against
  124. * the database if the collection is not yet initialized.
  125. */
  126. const FETCH_EXTRA_LAZY = 4;
  127. /**
  128. * Identifies a one-to-one association.
  129. */
  130. const ONE_TO_ONE = 1;
  131. /**
  132. * Identifies a many-to-one association.
  133. */
  134. const MANY_TO_ONE = 2;
  135. /**
  136. * Combined bitmask for to-one (single-valued) associations.
  137. */
  138. const TO_ONE = 3;
  139. /**
  140. * Identifies a one-to-many association.
  141. */
  142. const ONE_TO_MANY = 4;
  143. /**
  144. * Identifies a many-to-many association.
  145. */
  146. const MANY_TO_MANY = 8;
  147. /**
  148. * Combined bitmask for to-many (collection-valued) associations.
  149. */
  150. const TO_MANY = 12;
  151. /**
  152. * READ-ONLY: The name of the entity class.
  153. */
  154. public $name;
  155. /**
  156. * READ-ONLY: The namespace the entity class is contained in.
  157. *
  158. * @var string
  159. * @todo Not really needed. Usage could be localized.
  160. */
  161. public $namespace;
  162. /**
  163. * READ-ONLY: The name of the entity class that is at the root of the mapped entity inheritance
  164. * hierarchy. If the entity is not part of a mapped inheritance hierarchy this is the same
  165. * as {@link $entityName}.
  166. *
  167. * @var string
  168. */
  169. public $rootEntityName;
  170. /**
  171. * The name of the custom repository class used for the entity class.
  172. * (Optional).
  173. *
  174. * @var string
  175. */
  176. public $customRepositoryClassName;
  177. /**
  178. * READ-ONLY: Whether this class describes the mapping of a mapped superclass.
  179. *
  180. * @var boolean
  181. */
  182. public $isMappedSuperclass = false;
  183. /**
  184. * READ-ONLY: The names of the parent classes (ancestors).
  185. *
  186. * @var array
  187. */
  188. public $parentClasses = array();
  189. /**
  190. * READ-ONLY: The names of all subclasses (descendants).
  191. *
  192. * @var array
  193. */
  194. public $subClasses = array();
  195. /**
  196. * READ-ONLY: The named queries allowed to be called directly from Repository.
  197. *
  198. * @var array
  199. */
  200. public $namedQueries = array();
  201. /**
  202. * READ-ONLY: The field names of all fields that are part of the identifier/primary key
  203. * of the mapped entity class.
  204. *
  205. * @var array
  206. */
  207. public $identifier = array();
  208. /**
  209. * READ-ONLY: The inheritance mapping type used by the class.
  210. *
  211. * @var integer
  212. */
  213. public $inheritanceType = self::INHERITANCE_TYPE_NONE;
  214. /**
  215. * READ-ONLY: The Id generator type used by the class.
  216. *
  217. * @var string
  218. */
  219. public $generatorType = self::GENERATOR_TYPE_NONE;
  220. /**
  221. * READ-ONLY: The field mappings of the class.
  222. * Keys are field names and values are mapping definitions.
  223. *
  224. * The mapping definition array has the following values:
  225. *
  226. * - <b>fieldName</b> (string)
  227. * The name of the field in the Entity.
  228. *
  229. * - <b>type</b> (string)
  230. * The type name of the mapped field. Can be one of Doctrine's mapping types
  231. * or a custom mapping type.
  232. *
  233. * - <b>columnName</b> (string, optional)
  234. * The column name. Optional. Defaults to the field name.
  235. *
  236. * - <b>length</b> (integer, optional)
  237. * The database length of the column. Optional. Default value taken from
  238. * the type.
  239. *
  240. * - <b>id</b> (boolean, optional)
  241. * Marks the field as the primary key of the entity. Multiple fields of an
  242. * entity can have the id attribute, forming a composite key.
  243. *
  244. * - <b>nullable</b> (boolean, optional)
  245. * Whether the column is nullable. Defaults to FALSE.
  246. *
  247. * - <b>columnDefinition</b> (string, optional, schema-only)
  248. * The SQL fragment that is used when generating the DDL for the column.
  249. *
  250. * - <b>precision</b> (integer, optional, schema-only)
  251. * The precision of a decimal column. Only valid if the column type is decimal.
  252. *
  253. * - <b>scale</b> (integer, optional, schema-only)
  254. * The scale of a decimal column. Only valid if the column type is decimal.
  255. *
  256. [* - <b>'unique'] (string, optional, schema-only)</b>
  257. * Whether a unique constraint should be generated for the column.
  258. *
  259. * @var array
  260. */
  261. public $fieldMappings = array();
  262. /**
  263. * READ-ONLY: An array of field names. Used to look up field names from column names.
  264. * Keys are column names and values are field names.
  265. * This is the reverse lookup map of $_columnNames.
  266. *
  267. * @var array
  268. */
  269. public $fieldNames = array();
  270. /**
  271. * READ-ONLY: A map of field names to column names. Keys are field names and values column names.
  272. * Used to look up column names from field names.
  273. * This is the reverse lookup map of $_fieldNames.
  274. *
  275. * @var array
  276. * @todo We could get rid of this array by just using $fieldMappings[$fieldName]['columnName'].
  277. */
  278. public $columnNames = array();
  279. /**
  280. * READ-ONLY: The discriminator value of this class.
  281. *
  282. * <b>This does only apply to the JOINED and SINGLE_TABLE inheritance mapping strategies
  283. * where a discriminator column is used.</b>
  284. *
  285. * @var mixed
  286. * @see discriminatorColumn
  287. */
  288. public $discriminatorValue;
  289. /**
  290. * READ-ONLY: The discriminator map of all mapped classes in the hierarchy.
  291. *
  292. * <b>This does only apply to the JOINED and SINGLE_TABLE inheritance mapping strategies
  293. * where a discriminator column is used.</b>
  294. *
  295. * @var mixed
  296. * @see discriminatorColumn
  297. */
  298. public $discriminatorMap = array();
  299. /**
  300. * READ-ONLY: The definition of the descriminator column used in JOINED and SINGLE_TABLE
  301. * inheritance mappings.
  302. *
  303. * @var array
  304. */
  305. public $discriminatorColumn;
  306. /**
  307. * READ-ONLY: The primary table definition. The definition is an array with the
  308. * following entries:
  309. *
  310. * name => <tableName>
  311. * schema => <schemaName>
  312. * indexes => array
  313. * uniqueConstraints => array
  314. *
  315. * @var array
  316. */
  317. public $table;
  318. /**
  319. * READ-ONLY: The registered lifecycle callbacks for entities of this class.
  320. *
  321. * @var array
  322. */
  323. public $lifecycleCallbacks = array();
  324. /**
  325. * READ-ONLY: The association mappings of this class.
  326. *
  327. * The mapping definition array supports the following keys:
  328. *
  329. * - <b>fieldName</b> (string)
  330. * The name of the field in the entity the association is mapped to.
  331. *
  332. * - <b>targetEntity</b> (string)
  333. * The class name of the target entity. If it is fully-qualified it is used as is.
  334. * If it is a simple, unqualified class name the namespace is assumed to be the same
  335. * as the namespace of the source entity.
  336. *
  337. * - <b>mappedBy</b> (string, required for bidirectional associations)
  338. * The name of the field that completes the bidirectional association on the owning side.
  339. * This key must be specified on the inverse side of a bidirectional association.
  340. *
  341. * - <b>inversedBy</b> (string, required for bidirectional associations)
  342. * The name of the field that completes the bidirectional association on the inverse side.
  343. * This key must be specified on the owning side of a bidirectional association.
  344. *
  345. * - <b>cascade</b> (array, optional)
  346. * The names of persistence operations to cascade on the association. The set of possible
  347. * values are: "persist", "remove", "detach", "merge", "refresh", "all" (implies all others).
  348. *
  349. * - <b>orderBy</b> (array, one-to-many/many-to-many only)
  350. * A map of field names (of the target entity) to sorting directions (ASC/DESC).
  351. * Example: array('priority' => 'desc')
  352. *
  353. * - <b>fetch</b> (integer, optional)
  354. * The fetching strategy to use for the association, usually defaults to FETCH_LAZY.
  355. * Possible values are: ClassMetadata::FETCH_EAGER, ClassMetadata::FETCH_LAZY.
  356. *
  357. * - <b>joinTable</b> (array, optional, many-to-many only)
  358. * Specification of the join table and its join columns (foreign keys).
  359. * Only valid for many-to-many mappings. Note that one-to-many associations can be mapped
  360. * through a join table by simply mapping the association as many-to-many with a unique
  361. * constraint on the join table.
  362. *
  363. * - <b>indexBy</b> (string, optional, to-many only)
  364. * Specification of a field on target-entity that is used to index the collection by.
  365. * This field HAS to be either the primary key or a unique column. Otherwise the collection
  366. * does not contain all the entities that are actually related.
  367. *
  368. * A join table definition has the following structure:
  369. * <pre>
  370. * array(
  371. * 'name' => <join table name>,
  372. * 'joinColumns' => array(<join column mapping from join table to source table>),
  373. * 'inverseJoinColumns' => array(<join column mapping from join table to target table>)
  374. * )
  375. * </pre>
  376. *
  377. *
  378. * @var array
  379. */
  380. public $associationMappings = array();
  381. /**
  382. * READ-ONLY: Flag indicating whether the identifier/primary key of the class is composite.
  383. *
  384. * @var boolean
  385. */
  386. public $isIdentifierComposite = false;
  387. /**
  388. * READ-ONLY: Flag indicating wheather the identifier/primary key contains at least one foreign key association.
  389. *
  390. * This flag is necessary because some code blocks require special treatment of this cases.
  391. *
  392. * @var boolean
  393. */
  394. public $containsForeignIdentifier = false;
  395. /**
  396. * READ-ONLY: The ID generator used for generating IDs for this class.
  397. *
  398. * @var AbstractIdGenerator
  399. * @todo Remove!
  400. */
  401. public $idGenerator;
  402. /**
  403. * READ-ONLY: The definition of the sequence generator of this class. Only used for the
  404. * SEQUENCE generation strategy.
  405. *
  406. * The definition has the following structure:
  407. * <code>
  408. * array(
  409. * 'sequenceName' => 'name',
  410. * 'allocationSize' => 20,
  411. * 'initialValue' => 1
  412. * )
  413. * </code>
  414. *
  415. * @var array
  416. * @todo Merge with tableGeneratorDefinition into generic generatorDefinition
  417. */
  418. public $sequenceGeneratorDefinition;
  419. /**
  420. * READ-ONLY: The definition of the table generator of this class. Only used for the
  421. * TABLE generation strategy.
  422. *
  423. * @var array
  424. * @todo Merge with tableGeneratorDefinition into generic generatorDefinition
  425. */
  426. public $tableGeneratorDefinition;
  427. /**
  428. * READ-ONLY: The policy used for change-tracking on entities of this class.
  429. *
  430. * @var integer
  431. */
  432. public $changeTrackingPolicy = self::CHANGETRACKING_DEFERRED_IMPLICIT;
  433. /**
  434. * READ-ONLY: A flag for whether or not instances of this class are to be versioned
  435. * with optimistic locking.
  436. *
  437. * @var boolean $isVersioned
  438. */
  439. public $isVersioned;
  440. /**
  441. * READ-ONLY: The name of the field which is used for versioning in optimistic locking (if any).
  442. *
  443. * @var mixed $versionField
  444. */
  445. public $versionField;
  446. /**
  447. * The ReflectionClass instance of the mapped class.
  448. *
  449. * @var ReflectionClass
  450. */
  451. public $reflClass;
  452. /**
  453. * Is this entity marked as "read-only"?
  454. *
  455. * That means it is never considered for change-tracking in the UnitOfWork. It is a very helpful performance
  456. * optimization for entities that are immutable, either in your domain or through the relation database
  457. * (coming from a view, or a history table for example).
  458. *
  459. * @var bool
  460. */
  461. public $isReadOnly = false;
  462. /**
  463. * Initializes a new ClassMetadata instance that will hold the object-relational mapping
  464. * metadata of the class with the given name.
  465. *
  466. * @param string $entityName The name of the entity class the new instance is used for.
  467. */
  468. public function __construct($entityName)
  469. {
  470. $this->name = $entityName;
  471. $this->rootEntityName = $entityName;
  472. }
  473. /**
  474. * Gets the ReflectionClass instance of the mapped class.
  475. *
  476. * @return ReflectionClass
  477. */
  478. public function getReflectionClass()
  479. {
  480. if ( ! $this->reflClass) {
  481. $this->reflClass = new ReflectionClass($this->name);
  482. }
  483. return $this->reflClass;
  484. }
  485. /**
  486. * Sets the change tracking policy used by this class.
  487. *
  488. * @param integer $policy
  489. */
  490. public function setChangeTrackingPolicy($policy)
  491. {
  492. $this->changeTrackingPolicy = $policy;
  493. }
  494. /**
  495. * Whether the change tracking policy of this class is "deferred explicit".
  496. *
  497. * @return boolean
  498. */
  499. public function isChangeTrackingDeferredExplicit()
  500. {
  501. return $this->changeTrackingPolicy == self::CHANGETRACKING_DEFERRED_EXPLICIT;
  502. }
  503. /**
  504. * Whether the change tracking policy of this class is "deferred implicit".
  505. *
  506. * @return boolean
  507. */
  508. public function isChangeTrackingDeferredImplicit()
  509. {
  510. return $this->changeTrackingPolicy == self::CHANGETRACKING_DEFERRED_IMPLICIT;
  511. }
  512. /**
  513. * Whether the change tracking policy of this class is "notify".
  514. *
  515. * @return boolean
  516. */
  517. public function isChangeTrackingNotify()
  518. {
  519. return $this->changeTrackingPolicy == self::CHANGETRACKING_NOTIFY;
  520. }
  521. /**
  522. * Checks whether a field is part of the identifier/primary key field(s).
  523. *
  524. * @param string $fieldName The field name
  525. * @return boolean TRUE if the field is part of the table identifier/primary key field(s),
  526. * FALSE otherwise.
  527. */
  528. public function isIdentifier($fieldName)
  529. {
  530. if ( ! $this->isIdentifierComposite) {
  531. return $fieldName === $this->identifier[0];
  532. }
  533. return in_array($fieldName, $this->identifier);
  534. }
  535. /**
  536. * Check if the field is unique.
  537. *
  538. * @param string $fieldName The field name
  539. * @return boolean TRUE if the field is unique, FALSE otherwise.
  540. */
  541. public function isUniqueField($fieldName)
  542. {
  543. $mapping = $this->getFieldMapping($fieldName);
  544. if ($mapping !== false) {
  545. return isset($mapping['unique']) && $mapping['unique'] == true;
  546. }
  547. return false;
  548. }
  549. /**
  550. * Check if the field is not null.
  551. *
  552. * @param string $fieldName The field name
  553. * @return boolean TRUE if the field is not null, FALSE otherwise.
  554. */
  555. public function isNullable($fieldName)
  556. {
  557. $mapping = $this->getFieldMapping($fieldName);
  558. if ($mapping !== false) {
  559. return isset($mapping['nullable']) && $mapping['nullable'] == true;
  560. }
  561. return false;
  562. }
  563. /**
  564. * Gets a column name for a field name.
  565. * If the column name for the field cannot be found, the given field name
  566. * is returned.
  567. *
  568. * @param string $fieldName The field name.
  569. * @return string The column name.
  570. */
  571. public function getColumnName($fieldName)
  572. {
  573. return isset($this->columnNames[$fieldName]) ?
  574. $this->columnNames[$fieldName] : $fieldName;
  575. }
  576. /**
  577. * Gets the mapping of a (regular) field that holds some data but not a
  578. * reference to another object.
  579. *
  580. * @param string $fieldName The field name.
  581. * @return array The field mapping.
  582. */
  583. public function getFieldMapping($fieldName)
  584. {
  585. if ( ! isset($this->fieldMappings[$fieldName])) {
  586. throw MappingException::mappingNotFound($this->name, $fieldName);
  587. }
  588. return $this->fieldMappings[$fieldName];
  589. }
  590. /**
  591. * Gets the mapping of an association.
  592. *
  593. * @see ClassMetadataInfo::$associationMappings
  594. * @param string $fieldName The field name that represents the association in
  595. * the object model.
  596. * @return array The mapping.
  597. */
  598. public function getAssociationMapping($fieldName)
  599. {
  600. if ( ! isset($this->associationMappings[$fieldName])) {
  601. throw MappingException::mappingNotFound($this->name, $fieldName);
  602. }
  603. return $this->associationMappings[$fieldName];
  604. }
  605. /**
  606. * Gets all association mappings of the class.
  607. *
  608. * @return array
  609. */
  610. public function getAssociationMappings()
  611. {
  612. return $this->associationMappings;
  613. }
  614. /**
  615. * Gets the field name for a column name.
  616. * If no field name can be found the column name is returned.
  617. *
  618. * @param string $columnName column name
  619. * @return string column alias
  620. */
  621. public function getFieldName($columnName)
  622. {
  623. return isset($this->fieldNames[$columnName]) ?
  624. $this->fieldNames[$columnName] : $columnName;
  625. }
  626. /**
  627. * Gets the named query.
  628. *
  629. * @see ClassMetadataInfo::$namedQueries
  630. * @throws MappingException
  631. * @param string $queryName The query name
  632. * @return string
  633. */
  634. public function getNamedQuery($queryName)
  635. {
  636. if ( ! isset($this->namedQueries[$queryName])) {
  637. throw MappingException::queryNotFound($this->name, $queryName);
  638. }
  639. return $this->namedQueries[$queryName];
  640. }
  641. /**
  642. * Gets all named queries of the class.
  643. *
  644. * @return array
  645. */
  646. public function getNamedQueries()
  647. {
  648. return $this->namedQueries;
  649. }
  650. /**
  651. * Validates & completes the given field mapping.
  652. *
  653. * @param array $mapping The field mapping to validated & complete.
  654. * @return array The validated and completed field mapping.
  655. */
  656. protected function _validateAndCompleteFieldMapping(array &$mapping)
  657. {
  658. // Check mandatory fields
  659. if ( ! isset($mapping['fieldName']) || strlen($mapping['fieldName']) == 0) {
  660. throw MappingException::missingFieldName($this->name);
  661. }
  662. if ( ! isset($mapping['type'])) {
  663. // Default to string
  664. $mapping['type'] = 'string';
  665. }
  666. // Complete fieldName and columnName mapping
  667. if ( ! isset($mapping['columnName'])) {
  668. $mapping['columnName'] = $mapping['fieldName'];
  669. } else {
  670. if ($mapping['columnName'][0] == '`') {
  671. $mapping['columnName'] = trim($mapping['columnName'], '`');
  672. $mapping['quoted'] = true;
  673. }
  674. }
  675. $this->columnNames[$mapping['fieldName']] = $mapping['columnName'];
  676. if (isset($this->fieldNames[$mapping['columnName']]) || ($this->discriminatorColumn != null && $this->discriminatorColumn['name'] == $mapping['columnName'])) {
  677. throw MappingException::duplicateColumnName($this->name, $mapping['columnName']);
  678. }
  679. $this->fieldNames[$mapping['columnName']] = $mapping['fieldName'];
  680. // Complete id mapping
  681. if (isset($mapping['id']) && $mapping['id'] === true) {
  682. if ($this->versionField == $mapping['fieldName']) {
  683. throw MappingException::cannotVersionIdField($this->name, $mapping['fieldName']);
  684. }
  685. if ( ! in_array($mapping['fieldName'], $this->identifier)) {
  686. $this->identifier[] = $mapping['fieldName'];
  687. }
  688. // Check for composite key
  689. if ( ! $this->isIdentifierComposite && count($this->identifier) > 1) {
  690. $this->isIdentifierComposite = true;
  691. }
  692. }
  693. }
  694. /**
  695. * Validates & completes the basic mapping information that is common to all
  696. * association mappings (one-to-one, many-ot-one, one-to-many, many-to-many).
  697. *
  698. * @param array $mapping The mapping.
  699. * @return array The updated mapping.
  700. * @throws MappingException If something is wrong with the mapping.
  701. */
  702. protected function _validateAndCompleteAssociationMapping(array $mapping)
  703. {
  704. if ( ! isset($mapping['mappedBy'])) {
  705. $mapping['mappedBy'] = null;
  706. }
  707. if ( ! isset($mapping['inversedBy'])) {
  708. $mapping['inversedBy'] = null;
  709. }
  710. $mapping['isOwningSide'] = true; // assume owning side until we hit mappedBy
  711. // unset optional indexBy attribute if its empty
  712. if (!isset($mapping['indexBy']) || !$mapping['indexBy']) {
  713. unset($mapping['indexBy']);
  714. }
  715. // If targetEntity is unqualified, assume it is in the same namespace as
  716. // the sourceEntity.
  717. $mapping['sourceEntity'] = $this->name;
  718. if (isset($mapping['targetEntity']) && strpos($mapping['targetEntity'], '\\') === false
  719. && strlen($this->namespace) > 0) {
  720. $mapping['targetEntity'] = $this->namespace . '\\' . $mapping['targetEntity'];
  721. }
  722. // Complete id mapping
  723. if (isset($mapping['id']) && $mapping['id'] === true) {
  724. if (isset($mapping['orphanRemoval']) && $mapping['orphanRemoval'] == true) {
  725. throw MappingException::illegalOrphanRemovalOnIdentifierAssociation($this->name, $mapping['fieldName']);
  726. }
  727. if ( ! in_array($mapping['fieldName'], $this->identifier)) {
  728. if (count($mapping['joinColumns']) >= 2) {
  729. throw MappingException::cannotMapCompositePrimaryKeyEntitiesAsForeignId(
  730. $mapping['targetEntity'], $this->name, $mapping['fieldName']
  731. );
  732. }
  733. $this->identifier[] = $mapping['fieldName'];
  734. $this->containsForeignIdentifier = true;
  735. }
  736. // Check for composite key
  737. if ( ! $this->isIdentifierComposite && count($this->identifier) > 1) {
  738. $this->isIdentifierComposite = true;
  739. }
  740. }
  741. // Mandatory attributes for both sides
  742. // Mandatory: fieldName, targetEntity
  743. if ( ! isset($mapping['fieldName']) || strlen($mapping['fieldName']) == 0) {
  744. throw MappingException::missingFieldName($this->name);
  745. }
  746. if ( ! isset($mapping['targetEntity'])) {
  747. throw MappingException::missingTargetEntity($mapping['fieldName']);
  748. }
  749. // Mandatory and optional attributes for either side
  750. if ( ! $mapping['mappedBy']) {
  751. if (isset($mapping['joinTable']) && $mapping['joinTable']) {
  752. if (isset($mapping['joinTable']['name']) && $mapping['joinTable']['name'][0] == '`') {
  753. $mapping['joinTable']['name'] = trim($mapping['joinTable']['name'], '`');
  754. $mapping['joinTable']['quoted'] = true;
  755. }
  756. }
  757. } else {
  758. $mapping['isOwningSide'] = false;
  759. }
  760. if (isset($mapping['id']) && $mapping['id'] === true && $mapping['type'] & self::TO_MANY) {
  761. throw MappingException::illegalToManyIdentifierAssoaction($this->name, $mapping['fieldName']);
  762. }
  763. // Fetch mode. Default fetch mode to LAZY, if not set.
  764. if ( ! isset($mapping['fetch'])) {
  765. $mapping['fetch'] = self::FETCH_LAZY;
  766. }
  767. // Cascades
  768. $cascades = isset($mapping['cascade']) ? array_map('strtolower', $mapping['cascade']) : array();
  769. if (in_array('all', $cascades)) {
  770. $cascades = array(
  771. 'remove',
  772. 'persist',
  773. 'refresh',
  774. 'merge',
  775. 'detach'
  776. );
  777. }
  778. $mapping['cascade'] = $cascades;
  779. $mapping['isCascadeRemove'] = in_array('remove', $cascades);
  780. $mapping['isCascadePersist'] = in_array('persist', $cascades);
  781. $mapping['isCascadeRefresh'] = in_array('refresh', $cascades);
  782. $mapping['isCascadeMerge'] = in_array('merge', $cascades);
  783. $mapping['isCascadeDetach'] = in_array('detach', $cascades);
  784. return $mapping;
  785. }
  786. /**
  787. * Validates & completes a one-to-one association mapping.
  788. *
  789. * @param array $mapping The mapping to validate & complete.
  790. * @return array The validated & completed mapping.
  791. * @override
  792. */
  793. protected function _validateAndCompleteOneToOneMapping(array $mapping)
  794. {
  795. $mapping = $this->_validateAndCompleteAssociationMapping($mapping);
  796. if (isset($mapping['joinColumns']) && $mapping['joinColumns']) {
  797. $mapping['isOwningSide'] = true;
  798. }
  799. if ($mapping['isOwningSide']) {
  800. if ( ! isset($mapping['joinColumns']) || ! $mapping['joinColumns']) {
  801. // Apply default join column
  802. $mapping['joinColumns'] = array(array(
  803. 'name' => $mapping['fieldName'] . '_id',
  804. 'referencedColumnName' => 'id'
  805. ));
  806. }
  807. $uniqueContraintColumns = array();
  808. foreach ($mapping['joinColumns'] as $key => &$joinColumn) {
  809. if ($mapping['type'] === self::ONE_TO_ONE) {
  810. if (count($mapping['joinColumns']) == 1) {
  811. $joinColumn['unique'] = true;
  812. } else {
  813. $uniqueContraintColumns[] = $joinColumn['name'];
  814. }
  815. }
  816. if (empty($joinColumn['name'])) {
  817. $joinColumn['name'] = $mapping['fieldName'] . '_id';
  818. }
  819. if (empty($joinColumn['referencedColumnName'])) {
  820. $joinColumn['referencedColumnName'] = 'id';
  821. }
  822. $mapping['sourceToTargetKeyColumns'][$joinColumn['name']] = $joinColumn['referencedColumnName'];
  823. $mapping['joinColumnFieldNames'][$joinColumn['name']] = isset($joinColumn['fieldName'])
  824. ? $joinColumn['fieldName'] : $joinColumn['name'];
  825. }
  826. if ($uniqueContraintColumns) {
  827. if (!$this->table) {
  828. throw new \RuntimeException("ClassMetadataInfo::setTable() has to be called before defining a one to one relationship.");
  829. }
  830. $this->table['uniqueConstraints'][$mapping['fieldName']."_uniq"] = array(
  831. 'columns' => $uniqueContraintColumns
  832. );
  833. }
  834. $mapping['targetToSourceKeyColumns'] = array_flip($mapping['sourceToTargetKeyColumns']);
  835. }
  836. //TODO: if orphanRemoval, cascade=remove is implicit!
  837. $mapping['orphanRemoval'] = isset($mapping['orphanRemoval']) ?
  838. (bool) $mapping['orphanRemoval'] : false;
  839. if (isset($mapping['id']) && $mapping['id'] === true && !$mapping['isOwningSide']) {
  840. throw MappingException::illegalInverseIdentifierAssocation($this->name, $mapping['fieldName']);
  841. }
  842. return $mapping;
  843. }
  844. /**
  845. * Validates and completes the mapping.
  846. *
  847. * @param array $mapping The mapping to validate and complete.
  848. * @return array The validated and completed mapping.
  849. * @override
  850. */
  851. protected function _validateAndCompleteOneToManyMapping(array $mapping)
  852. {
  853. $mapping = $this->_validateAndCompleteAssociationMapping($mapping);
  854. // OneToMany-side MUST be inverse (must have mappedBy)
  855. if ( ! isset($mapping['mappedBy'])) {
  856. throw MappingException::oneToManyRequiresMappedBy($mapping['fieldName']);
  857. }
  858. //TODO: if orphanRemoval, cascade=remove is implicit!
  859. $mapping['orphanRemoval'] = isset($mapping['orphanRemoval']) ?
  860. (bool) $mapping['orphanRemoval'] : false;
  861. if (isset($mapping['orderBy'])) {
  862. if ( ! is_array($mapping['orderBy'])) {
  863. throw new \InvalidArgumentException("'orderBy' is expected to be an array, not ".gettype($mapping['orderBy']));
  864. }
  865. }
  866. return $mapping;
  867. }
  868. protected function _validateAndCompleteManyToManyMapping(array $mapping)
  869. {
  870. $mapping = $this->_validateAndCompleteAssociationMapping($mapping);
  871. if ($mapping['isOwningSide']) {
  872. if (strpos($mapping['sourceEntity'], '\\') !== false) {
  873. $sourceShortName = strtolower(substr($mapping['sourceEntity'], strrpos($mapping['sourceEntity'], '\\') + 1));
  874. } else {
  875. $sourceShortName = strtolower($mapping['sourceEntity']);
  876. }
  877. if (strpos($mapping['targetEntity'], '\\') !== false) {
  878. $targetShortName = strtolower(substr($mapping['targetEntity'], strrpos($mapping['targetEntity'], '\\') + 1));
  879. } else {
  880. $targetShortName = strtolower($mapping['targetEntity']);
  881. }
  882. // owning side MUST have a join table
  883. if ( ! isset($mapping['joinTable']['name'])) {
  884. $mapping['joinTable']['name'] = $sourceShortName .'_' . $targetShortName;
  885. }
  886. if ( ! isset($mapping['joinTable']['joinColumns'])) {
  887. $mapping['joinTable']['joinColumns'] = array(array(
  888. 'name' => $sourceShortName . '_id',
  889. 'referencedColumnName' => 'id',
  890. 'onDelete' => 'CASCADE'));
  891. }
  892. if ( ! isset($mapping['joinTable']['inverseJoinColumns'])) {
  893. $mapping['joinTable']['inverseJoinColumns'] = array(array(
  894. 'name' => $targetShortName . '_id',
  895. 'referencedColumnName' => 'id',
  896. 'onDelete' => 'CASCADE'));
  897. }
  898. foreach ($mapping['joinTable']['joinColumns'] as &$joinColumn) {
  899. if (empty($joinColumn['name'])) {
  900. $joinColumn['name'] = $sourceShortName . '_id';
  901. }
  902. if (empty($joinColumn['referencedColumnName'])) {
  903. $joinColumn['referencedColumnName'] = 'id';
  904. }
  905. if (isset($joinColumn['onDelete']) && strtolower($joinColumn['onDelete']) == 'cascade') {
  906. $mapping['isOnDeleteCascade'] = true;
  907. }
  908. $mapping['relationToSourceKeyColumns'][$joinColumn['name']] = $joinColumn['referencedColumnName'];
  909. $mapping['joinTableColumns'][] = $joinColumn['name'];
  910. }
  911. foreach ($mapping['joinTable']['inverseJoinColumns'] as &$inverseJoinColumn) {
  912. if (empty($inverseJoinColumn['name'])) {
  913. $inverseJoinColumn['name'] = $targetShortName . '_id';
  914. }
  915. if (empty($inverseJoinColumn['referencedColumnName'])) {
  916. $inverseJoinColumn['referencedColumnName'] = 'id';
  917. }
  918. if (isset($inverseJoinColumn['onDelete']) && strtolower($inverseJoinColumn['onDelete']) == 'cascade') {
  919. $mapping['isOnDeleteCascade'] = true;
  920. }
  921. $mapping['relationToTargetKeyColumns'][$inverseJoinColumn['name']] = $inverseJoinColumn['referencedColumnName'];
  922. $mapping['joinTableColumns'][] = $inverseJoinColumn['name'];
  923. }
  924. }
  925. if (isset($mapping['orderBy'])) {
  926. if ( ! is_array($mapping['orderBy'])) {
  927. throw new \InvalidArgumentException("'orderBy' is expected to be an array, not ".gettype($mapping['orderBy']));
  928. }
  929. }
  930. return $mapping;
  931. }
  932. /**
  933. * Gets the identifier (primary key) field names of the class.
  934. *
  935. * @return mixed
  936. */
  937. public function getIdentifierFieldNames()
  938. {
  939. return $this->identifier;
  940. }
  941. /**
  942. * Gets the name of the single id field. Note that this only works on
  943. * entity classes that have a single-field pk.
  944. *
  945. * @return string
  946. * @throws MappingException If the class has a composite primary key.
  947. */
  948. public function getSingleIdentifierFieldName()
  949. {
  950. if ($this->isIdentifierComposite) {
  951. throw MappingException::singleIdNotAllowedOnCompositePrimaryKey($this->name);
  952. }
  953. return $this->identifier[0];
  954. }
  955. /**
  956. * Gets the column name of the single id column. Note that this only works on
  957. * entity classes that have a single-field pk.
  958. *
  959. * @return string
  960. * @throws MappingException If the class has a composite primary key.
  961. */
  962. public function getSingleIdentifierColumnName()
  963. {
  964. return $this->getColumnName($this->getSingleIdentifierFieldName());
  965. }
  966. /**
  967. * INTERNAL:
  968. * Sets the mapped identifier/primary key fields of this class.
  969. * Mainly used by the ClassMetadataFactory to assign inherited identifiers.
  970. *
  971. * @param array $identifier
  972. */
  973. public function setIdentifier(array $identifier)
  974. {
  975. $this->identifier = $identifier;
  976. $this->isIdentifierComposite = (count($this->identifier) > 1);
  977. }
  978. /**
  979. * Gets the mapped identifier field of this class.
  980. *
  981. * @return string $identifier
  982. */
  983. public function getIdentifier()
  984. {
  985. return $this->identifier;
  986. }
  987. /**
  988. * Checks whether the class has a (mapped) field with a certain name.
  989. *
  990. * @return boolean
  991. */
  992. public function hasField($fieldName)
  993. {
  994. return isset($this->fieldMappings[$fieldName]);
  995. }
  996. /**
  997. * Gets an array containing all the column names.
  998. *
  999. * @return array
  1000. */
  1001. public function getColumnNames(array $fieldNames = null)
  1002. {
  1003. if ($fieldNames === null) {
  1004. return array_keys($this->fieldNames);
  1005. } else {
  1006. $columnNames = array();
  1007. foreach ($fieldNames as $fieldName) {
  1008. $columnNames[] = $this->getColumnName($fieldName);
  1009. }
  1010. return $columnNames;
  1011. }
  1012. }
  1013. /**
  1014. * Returns an array with all the identifier column names.
  1015. *
  1016. * @return array
  1017. */
  1018. public function getIdentifierColumnNames()
  1019. {
  1020. if ($this->isIdentifierComposite) {
  1021. $columnNames = array();
  1022. foreach ($this->identifier as $idField) {
  1023. if (isset($this->associationMappings[$idField])) {
  1024. // no composite pk as fk entity assumption:
  1025. $columnNames[] = $this->associationMappings[$idField]['joinColumns'][0]['name'];
  1026. } else {
  1027. $columnNames[] = $this->fieldMappings[$idField]['columnName'];
  1028. }
  1029. }
  1030. return $columnNames;
  1031. } else if(isset($this->fieldMappings[$this->identifier[0]])) {
  1032. return array($this->fieldMappings[$this->identifier[0]]['columnName']);
  1033. } else {
  1034. // no composite pk as fk entity assumption:
  1035. return array($this->associationMappings[$this->identifier[0]]['joinColumns'][0]['name']);
  1036. }
  1037. }
  1038. /**
  1039. * Sets the type of Id generator to use for the mapped class.
  1040. */
  1041. public function setIdGeneratorType($generatorType)
  1042. {
  1043. $this->generatorType = $generatorType;
  1044. }
  1045. /**
  1046. * Checks whether the mapped class uses an Id generator.
  1047. *
  1048. * @return boolean TRUE if the mapped class uses an Id generator, FALSE otherwise.
  1049. */
  1050. public function usesIdGenerator()
  1051. {
  1052. return $this->generatorType != self::GENERATOR_TYPE_NONE;
  1053. }
  1054. /**
  1055. * @return boolean
  1056. */
  1057. public function isInheritanceTypeNone()
  1058. {
  1059. return $this->inheritanceType == self::INHERITANCE_TYPE_NONE;
  1060. }
  1061. /**
  1062. * Checks whether the mapped class uses the JOINED inheritance mapping strategy.
  1063. *
  1064. * @return boolean TRUE if the class participates in a JOINED inheritance mapping,
  1065. * FALSE otherwise.
  1066. */
  1067. public function isInheritanceTypeJoined()
  1068. {
  1069. return $this->inheritanceType == self::INHERITANCE_TYPE_JOINED;
  1070. }
  1071. /**
  1072. * Checks whether the mapped class uses the SINGLE_TABLE inheritance mapping strategy.
  1073. *
  1074. * @return boolean TRUE if the class participates in a SINGLE_TABLE inheritance mapping,
  1075. * FALSE otherwise.
  1076. */
  1077. public function isInheritanceTypeSingleTable()
  1078. {
  1079. return $this->inheritanceType == self::INHERITANCE_TYPE_SINGLE_TABLE;
  1080. }
  1081. /**
  1082. * Checks whether the mapped class uses the TABLE_PER_CLASS inheritance mapping strategy.
  1083. *
  1084. * @return boolean TRUE if the class participates in a TABLE_PER_CLASS inheritance mapping,
  1085. * FALSE otherwise.
  1086. */
  1087. public function isInheritanceTypeTablePerClass()
  1088. {
  1089. return $this->inheritanceType == self::INHERITANCE_TYPE_TABLE_PER_CLASS;
  1090. }
  1091. /**
  1092. * Checks whether the class uses an identity column for the Id generation.
  1093. *
  1094. * @return boolean TRUE if the class uses the IDENTITY generator, FALSE otherwise.
  1095. */
  1096. public function isIdGeneratorIdentity()
  1097. {
  1098. return $this->generatorType == self::GENERATOR_TYPE_IDENTITY;
  1099. }
  1100. /**
  1101. * Checks whether the class uses a sequence for id generation.
  1102. *
  1103. * @return boolean TRUE if the class uses the SEQUENCE generator, FALSE otherwise.
  1104. */
  1105. public function isIdGeneratorSequence()
  1106. {
  1107. return $this->generatorType == self::GENERATOR_TYPE_SEQUENCE;
  1108. }
  1109. /**
  1110. * Checks whether the class uses a table for id generation.
  1111. *
  1112. * @return boolean TRUE if the class uses the TABLE generator, FALSE otherwise.
  1113. */
  1114. public function isIdGeneratorTable()
  1115. {
  1116. $this->generatorType == self::GENERATOR_TYPE_TABLE;
  1117. }
  1118. /**
  1119. * Checks whether the class has a natural identifier/pk (which means it does
  1120. * not use any Id generator.
  1121. *
  1122. * @return boolean
  1123. */
  1124. public function isIdentifierNatural()
  1125. {
  1126. return $this->generatorType == self::GENERATOR_TYPE_NONE;
  1127. }
  1128. /**
  1129. * Gets the type of a field.
  1130. *
  1131. * @param string $fieldName
  1132. * @return Doctrine\DBAL\Types\Type
  1133. */
  1134. public function getTypeOfField($fieldName)
  1135. {
  1136. return isset($this->fieldMappings[$fieldName]) ?
  1137. $this->fieldMappings[$fieldName]['type'] : null;
  1138. }
  1139. /**
  1140. * Gets the type of a column.
  1141. *
  1142. * @return Doctrine\DBAL\Types\Type
  1143. */
  1144. public function getTypeOfColumn($columnName)
  1145. {
  1146. return $this->getTypeOfField($this->getFieldName($columnName));
  1147. }
  1148. /**
  1149. * Gets the name of the primary table.
  1150. *
  1151. * @return string
  1152. */
  1153. public function getTableName()
  1154. {
  1155. return $this->table['name'];
  1156. }
  1157. /**
  1158. * Gets the table name to use for temporary identifier tables of this class.
  1159. *
  1160. * @return string
  1161. */
  1162. public function getTemporaryIdTableName()
  1163. {
  1164. // replace dots with underscores because PostgreSQL creates temporary tables in a special schema
  1165. return str_replace('.', '_', $this->table['name'] . '_id_tmp');
  1166. }
  1167. /**
  1168. * Sets the mapped subclasses of this class.
  1169. *
  1170. * @param array $subclasses The names of all mapped subclasses.
  1171. */
  1172. public function setSubclasses(array $subclasses)
  1173. {
  1174. foreach ($subclasses as $subclass) {
  1175. if (strpos($subclass, '\\') === false && strlen($this->namespace)) {
  1176. $this->subClasses[] = $this->namespace . '\\' . $subclass;
  1177. } else {
  1178. $this->subClasses[] = $subclass;
  1179. }
  1180. }
  1181. }
  1182. /**
  1183. * Sets the parent class names.
  1184. * Assumes that the class names in the passed array are in the order:
  1185. * directParent -> directParentParent -> directParentParentParent ... -> root.
  1186. */
  1187. public function setParentClasses(array $classNames)
  1188. {
  1189. $this->parentClasses = $classNames;
  1190. if (count($classNames) > 0) {
  1191. $this->rootEntityName = array_pop($classNames);
  1192. }
  1193. }
  1194. /**
  1195. * Sets the inheritance type used by the class and it's subclasses.
  1196. *
  1197. * @param integer $type
  1198. */
  1199. public function setInheritanceType($type)
  1200. {
  1201. if ( ! $this->_isInheritanceType($type)) {
  1202. throw MappingException::invalidInheritanceType($this->name, $type);
  1203. }
  1204. $this->inheritanceType = $type;
  1205. }
  1206. /**
  1207. * Checks whether a mapped field is inherited from an entity superclass.
  1208. *
  1209. * @return boolean TRUE if the field is inherited, FALSE otherwise.
  1210. */
  1211. public function isInheritedField($fieldName)
  1212. {
  1213. return isset($this->fieldMappings[$fieldName]['inherited']);
  1214. }
  1215. /**
  1216. * Checks whether a mapped association field is inherited from a superclass.
  1217. *
  1218. * @param string $fieldName
  1219. * @return boolean TRUE if the field is inherited, FALSE otherwise.
  1220. */
  1221. public function isInheritedAssociation($fieldName)
  1222. {
  1223. return isset($this->associationMappings[$fieldName]['inherited']);
  1224. }
  1225. /**
  1226. * Sets the name of the primary table the class is mapped to.
  1227. *
  1228. * @param string $tableName The table name.
  1229. * @deprecated Use {@link setPrimaryTable}.
  1230. */
  1231. public function setTableName($tableName)
  1232. {
  1233. $this->table['name'] = $tableName;
  1234. }
  1235. /**
  1236. * Sets the primary table definition. The provided array supports the
  1237. * following structure:
  1238. *
  1239. * name => <tableName> (optional, defaults to class name)
  1240. * indexes => array of indexes (optional)
  1241. * uniqueConstraints => array of constraints (optional)
  1242. *
  1243. * If a key is omitted, the current value is kept.
  1244. *
  1245. * @param array $table The table description.
  1246. */
  1247. public function setPrimaryTable(array $table)
  1248. {
  1249. if (isset($table['name'])) {
  1250. if ($table['name'][0] == '`') {
  1251. $this->table['name'] = trim($table['name'], '`');
  1252. $this->table['quoted'] = true;
  1253. } else {
  1254. $this->table['name'] = $table['name'];
  1255. }
  1256. }
  1257. if (isset($table['indexes'])) {
  1258. $this->table['indexes'] = $table['indexes'];
  1259. }
  1260. if (isset($table['uniqueConstraints'])) {
  1261. $this->table['uniqueConstraints'] = $table['uniqueConstraints'];
  1262. }
  1263. }
  1264. /**
  1265. * Checks whether the given type identifies an inheritance type.
  1266. *
  1267. * @param integer $type
  1268. * @return boolean TRUE if the given type identifies an inheritance type, FALSe otherwise.
  1269. */
  1270. private function _isInheritanceType($type)
  1271. {
  1272. return $type == self::INHERITANCE_TYPE_NONE ||
  1273. $type == self::INHERITANCE_TYPE_SINGLE_TABLE ||
  1274. $type == self::INHERITANCE_TYPE_JOINED ||
  1275. $type == self::INHERITANCE_TYPE_TABLE_PER_CLASS;
  1276. }
  1277. /**
  1278. * Adds a mapped field to the class.
  1279. *
  1280. * @param array $mapping The field mapping.
  1281. */
  1282. public function mapField(array $mapping)
  1283. {
  1284. $this->_validateAndCompleteFieldMapping($mapping);
  1285. if (isset($this->fieldMappings[$mapping['fieldName']]) || isset($this->associationMappings[$mapping['fieldName']])) {
  1286. throw MappingException::duplicateFieldMapping($this->name, $mapping['fieldName']);
  1287. }
  1288. $this->fieldMappings[$mapping['fieldName']] = $mapping;
  1289. }
  1290. /**
  1291. * INTERNAL:
  1292. * Adds an association mapping without completing/validating it.
  1293. * This is mainly used to add inherited association mappings to derived classes.
  1294. *
  1295. * @param array $mapping
  1296. */
  1297. public function addInheritedAssociationMapping(array $mapping/*, $owningClassName = null*/)
  1298. {
  1299. if (isset($this->associationMappings[$mapping['fieldName']])) {
  1300. throw MappingException::duplicateAssociationMapping($this->name, $mapping['fieldName']);
  1301. }
  1302. $this->associationMappings[$mapping['fieldName']] = $mapping;
  1303. }
  1304. /**
  1305. * INTERNAL:
  1306. * Adds a field mapping without completing/validating it.
  1307. * This is mainly used to add inherited field mappings to derived classes.
  1308. *
  1309. * @param array $mapping
  1310. */
  1311. public function addInheritedFieldMapping(array $fieldMapping)
  1312. {
  1313. $this->fieldMappings[$fieldMapping['fieldName']] = $fieldMapping;
  1314. $this->columnNames[$fieldMapping['fieldName']] = $fieldMapping['columnName'];
  1315. $this->fieldNames[$fieldMapping['columnName']] = $fieldMapping['fieldName'];
  1316. }
  1317. /**
  1318. * INTERNAL:
  1319. * Adds a named query to this class.
  1320. *
  1321. * @throws MappingException
  1322. * @param array $queryMapping
  1323. */
  1324. public function addNamedQuery(array $queryMapping)
  1325. {
  1326. if (isset($this->namedQueries[$queryMapping['name']])) {
  1327. throw MappingException::duplicateQueryMapping($this->name, $queryMapping['name']);
  1328. }
  1329. $query = str_replace('__CLASS__', $this->name, $queryMapping['query']);
  1330. $this->namedQueries[$queryMapping['name']] = $query;
  1331. }
  1332. /**
  1333. * Adds a one-to-one mapping.
  1334. *
  1335. * @param array $mapping The mapping.
  1336. */
  1337. public function mapOneToOne(array $mapping)
  1338. {
  1339. $mapping['type'] = self::ONE_TO_ONE;
  1340. $mapping = $this->_validateAndCompleteOneToOneMapping($mapping);
  1341. $this->_storeAssociationMapping($mapping);
  1342. }
  1343. /**
  1344. * Adds a one-to-many mapping.
  1345. *
  1346. * @param array $mapping The mapping.
  1347. */
  1348. public function mapOneToMany(array $mapping)
  1349. {
  1350. $mapping['type'] = self::ONE_TO_MANY;
  1351. $mapping = $this->_validateAndCompleteOneToManyMapping($mapping);
  1352. $this->_storeAssociationMapping($mapping);
  1353. }
  1354. /**
  1355. * Adds a many-to-one mapping.
  1356. *
  1357. * @param array $mapping The mapping.
  1358. */
  1359. public function mapManyToOne(array $mapping)
  1360. {
  1361. $mapping['type'] = self::MANY_TO_ONE;
  1362. // A many-to-one mapping is essentially a one-one backreference
  1363. $mapping = $this->_validateAndCompleteOneToOneMapping($mapping);
  1364. $this->_storeAssociationMapping($mapping);
  1365. }
  1366. /**
  1367. * Adds a many-to-many mapping.
  1368. *
  1369. * @param array $mapping The mapping.
  1370. */
  1371. public function mapManyToMany(array $mapping)
  1372. {
  1373. $mapping['type'] = self::MANY_TO_MANY;
  1374. $mapping = $this->_validateAndCompleteManyToManyMapping($mapping);
  1375. $this->_storeAssociationMapping($mapping);
  1376. }
  1377. /**
  1378. * Stores the association mapping.
  1379. *
  1380. * @param AssociationMapping $assocMapping
  1381. */
  1382. protected function _storeAssociationMapping(array $assocMapping)
  1383. {
  1384. $sourceFieldName = $assocMapping['fieldName'];
  1385. if (isset($this->fieldMappings[$sourceFieldName]) || isset($this->associationMappings[$sourceFieldName])) {
  1386. throw MappingException::duplicateFieldMapping($this->name, $sourceFieldName);
  1387. }
  1388. $this->associationMappings[$sourceFieldName] = $assocMapping;
  1389. }
  1390. /**
  1391. * Registers a custom repository class for the entity class.
  1392. *
  1393. * @param string $mapperClassName The class name of the custom mapper.
  1394. */
  1395. public function setCustomRepositoryClass($repositoryClassName)
  1396. {
  1397. $this->customRepositoryClassName = $repositoryClassName;
  1398. }
  1399. /**
  1400. * Dispatches the lifecycle event of the given entity to the registered
  1401. * lifecycle callbacks and lifecycle listeners.
  1402. *
  1403. * @param string $event The lifecycle event.
  1404. * @param Entity $entity The Entity on which the event occured.
  1405. */
  1406. public function invokeLifecycleCallbacks($lifecycleEvent, $entity)
  1407. {
  1408. foreach ($this->lifecycleCallbacks[$lifecycleEvent] as $callback) {
  1409. $entity->$callback();
  1410. }
  1411. }
  1412. /**
  1413. * Whether the class has any attached lifecycle listeners or callbacks for a lifecycle event.
  1414. *
  1415. * @param string $lifecycleEvent
  1416. * @return boolean
  1417. */
  1418. public function hasLifecycleCallbacks($lifecycleEvent)
  1419. {
  1420. return isset($this->lifecycleCallbacks[$lifecycleEvent]);
  1421. }
  1422. /**
  1423. * Gets the registered lifecycle callbacks for an event.
  1424. *
  1425. * @param string $event
  1426. * @return array
  1427. */
  1428. public function getLifecycleCallbacks($event)
  1429. {
  1430. return isset($this->lifecycleCallbacks[$event]) ? $this->lifecycleCallbacks[$event] : array();
  1431. }
  1432. /**
  1433. * Adds a lifecycle callback for entities of this class.
  1434. *
  1435. * Note: If the same callback is registered more than once, the old one
  1436. * will be overridden.
  1437. *
  1438. * @param string $callback
  1439. * @param string $event
  1440. */
  1441. public function addLifecycleCallback($callback, $event)
  1442. {
  1443. $this->lifecycleCallbacks[$event][] = $callback;
  1444. }
  1445. /**
  1446. * Sets the lifecycle callbacks for entities of this class.
  1447. * Any previously registered callbacks are overwritten.
  1448. *
  1449. * @param array $callbacks
  1450. */
  1451. public function setLifecycleCallbacks(array $callbacks)
  1452. {
  1453. $this->lifecycleCallbacks = $callbacks;
  1454. }
  1455. /**
  1456. * Sets the discriminator column definition.
  1457. *
  1458. * @param array $columnDef
  1459. * @see getDiscriminatorColumn()
  1460. */
  1461. public function setDiscriminatorColumn($columnDef)
  1462. {
  1463. if ($columnDef !== null) {
  1464. if (isset($this->fieldNames[$columnDef['name']])) {
  1465. throw MappingException::duplicateColumnName($this->name, $columnDef['name']);
  1466. }
  1467. if ( ! isset($columnDef['name'])) {
  1468. throw MappingException::nameIsMandatoryForDiscriminatorColumns($this->name, $columnDef);
  1469. }
  1470. if ( ! isset($columnDef['fieldName'])) {
  1471. $columnDef['fieldName'] = $columnDef['name'];
  1472. }
  1473. if ( ! isset($columnDef['type'])) {
  1474. $columnDef['type'] = "string";
  1475. }
  1476. if (in_array($columnDef['type'], array("boolean", "array", "object", "datetime", "time", "date"))) {
  1477. throw MappingException::invalidDiscriminatorColumnType($this->name, $columnDef['type']);
  1478. }
  1479. $this->discriminatorColumn = $columnDef;
  1480. }
  1481. }
  1482. /**
  1483. * Sets the discriminator values used by this class.
  1484. * Used for JOINED and SINGLE_TABLE inheritance mapping strategies.
  1485. *
  1486. * @param array $map
  1487. */
  1488. public function setDiscriminatorMap(array $map)
  1489. {
  1490. foreach ($map as $value => $className) {
  1491. if (strpos($className, '\\') === false && strlen($this->namespace)) {
  1492. $className = $this->namespace . '\\' . $className;
  1493. }
  1494. $className = ltrim($className, '\\');
  1495. $this->discriminatorMap[$value] = $className;
  1496. if ($this->name == $className) {
  1497. $this->discriminatorValue = $value;
  1498. } else {
  1499. if ( ! class_exists($className)) {
  1500. throw MappingException::invalidClassInDiscriminatorMap($className, $this->name);
  1501. }
  1502. if (is_subclass_of($className, $this->name)) {
  1503. $this->subClasses[] = $className;
  1504. }
  1505. }
  1506. }
  1507. }
  1508. /**
  1509. * Checks whether the class has a named query with the given query name.
  1510. *
  1511. * @param string $fieldName
  1512. * @return boolean
  1513. */
  1514. public function hasNamedQuery($queryName)
  1515. {
  1516. return isset($this->namedQueries[$queryName]);
  1517. }
  1518. /**
  1519. * Checks whether the class has a mapped association with the given field name.
  1520. *
  1521. * @param string $fieldName
  1522. * @return boolean
  1523. */
  1524. public function hasAssociation($fieldName)
  1525. {
  1526. return isset($this->associationMappings[$fieldName]);
  1527. }
  1528. /**
  1529. * Checks whether the class has a mapped association for the specified field
  1530. * and if yes, checks whether it is a single-valued association (to-one).
  1531. *
  1532. * @param string $fieldName
  1533. * @return boolean TRUE if the association exists and is single-valued, FALSE otherwise.
  1534. */
  1535. public function isSingleValuedAssociation($fieldName)
  1536. {
  1537. return isset($this->associationMappings[$fieldName]) &&
  1538. ($this->associationMappings[$fieldName]['type'] & self::TO_ONE);
  1539. }
  1540. /**
  1541. * Checks whether the class has a mapped association for the specified field
  1542. * and if yes, checks whether it is a collection-valued association (to-many).
  1543. *
  1544. * @param string $fieldName
  1545. * @return boolean TRUE if the association exists and is collection-valued, FALSE otherwise.
  1546. */
  1547. public function isCollectionValuedAssociation($fieldName)
  1548. {
  1549. return isset($this->associationMappings[$fieldName]) &&
  1550. ! ($this->associationMappings[$fieldName]['type'] & self::TO_ONE);
  1551. }
  1552. /**
  1553. * Is this an association that only has a single join column?
  1554. *
  1555. * @param string $fieldName
  1556. * @return bool
  1557. */
  1558. public function isAssociationWithSingleJoinColumn($fieldName)
  1559. {
  1560. return (
  1561. isset($this->associationMappings[$fieldName]) &&
  1562. isset($this->associationMappings[$fieldName]['joinColumns'][0]) &&
  1563. !isset($this->associationMappings[$fieldName]['joinColumns'][1])
  1564. );
  1565. }
  1566. /**
  1567. * Return the single association join column (if any).
  1568. *
  1569. * @param string $fieldName
  1570. * @return string
  1571. */
  1572. public function getSingleAssociationJoinColumnName($fieldName)
  1573. {
  1574. if (!$this->isAssociationWithSingleJoinColumn($fieldName)) {
  1575. throw MappingException::noSingleAssociationJoinColumnFound($this->name, $fieldName);
  1576. }
  1577. return $this->associationMappings[$fieldName]['joinColumns'][0]['name'];
  1578. }
  1579. /**
  1580. * Return the single association referenced join column name (if any).
  1581. *
  1582. * @param string $fieldName
  1583. * @return string
  1584. */
  1585. public function getSingleAssociationReferencedJoinColumnName($fieldName)
  1586. {
  1587. if (!$this->isAssociationWithSingleJoinColumn($fieldName)) {
  1588. throw MappingException::noSingleAssociationJoinColumnFound($this->name, $fieldName);
  1589. }
  1590. return $this->associationMappings[$fieldName]['joinColumns'][0]['referencedColumnName'];
  1591. }
  1592. /**
  1593. * Used to retrieve a fieldname for either field or association from a given column,
  1594. *
  1595. * This method is used in foreign-key as primary-key contexts.
  1596. *
  1597. * @param string $columnName
  1598. * @return string
  1599. */
  1600. public function getFieldForColumn($columnName)
  1601. {
  1602. if (isset($this->fieldNames[$columnName])) {
  1603. return $this->fieldNames[$columnName];
  1604. } else {
  1605. foreach ($this->associationMappings AS $assocName => $mapping) {
  1606. if ($this->isAssociationWithSingleJoinColumn($assocName) &&
  1607. $this->associationMappings[$assocName]['joinColumns'][0]['name'] == $columnName) {
  1608. return $assocName;
  1609. }
  1610. }
  1611. throw MappingException::noFieldNameFoundForColumn($this->name, $columnName);
  1612. }
  1613. }
  1614. /**
  1615. * Sets the ID generator used to generate IDs for instances of this class.
  1616. *
  1617. * @param AbstractIdGenerator $generator
  1618. */
  1619. public function setIdGenerator($generator)
  1620. {
  1621. $this->idGenerator = $generator;
  1622. }
  1623. /**
  1624. * Sets the definition of the sequence ID generator for this class.
  1625. *
  1626. * The definition must have the following structure:
  1627. * <code>
  1628. * array(
  1629. * 'sequenceName' => 'name',
  1630. * 'allocationSize' => 20,
  1631. * 'initialValue' => 1
  1632. * )
  1633. * </code>
  1634. *
  1635. * @param array $definition
  1636. */
  1637. public function setSequenceGeneratorDefinition(array $definition)
  1638. {
  1639. $this->sequenceGeneratorDefinition = $definition;
  1640. }
  1641. /**
  1642. * Sets the version field mapping used for versioning. Sets the default
  1643. * value to use depending on the column type.
  1644. *
  1645. * @param array $mapping The version field mapping array
  1646. */
  1647. public function setVersionMapping(array &$mapping)
  1648. {
  1649. $this->isVersioned = true;
  1650. $this->versionField = $mapping['fieldName'];
  1651. if ( ! isset($mapping['default'])) {
  1652. if ($mapping['type'] == 'integer') {
  1653. $mapping['default'] = 1;
  1654. } else if ($mapping['type'] == 'datetime') {
  1655. $mapping['default'] = 'CURRENT_TIMESTAMP';
  1656. } else {
  1657. throw MappingException::unsupportedOptimisticLockingType($this->name, $mapping['fieldName'], $mapping['type']);
  1658. }
  1659. }
  1660. }
  1661. /**
  1662. * Sets whether this class is to be versioned for optimistic locking.
  1663. *
  1664. * @param boolean $bool
  1665. */
  1666. public function setVersioned($bool)
  1667. {
  1668. $this->isVersioned = $bool;
  1669. }
  1670. /**
  1671. * Sets the name of the field that is to be used for versioning if this class is
  1672. * versioned for optimistic locking.
  1673. *
  1674. * @param string $versionField
  1675. */
  1676. public function setVersionField($versionField)
  1677. {
  1678. $this->versionField = $versionField;
  1679. }
  1680. /**
  1681. * Mark this class as read only, no change tracking is applied to it.
  1682. *
  1683. * @return void
  1684. */
  1685. public function markReadOnly()
  1686. {
  1687. $this->isReadOnly = true;
  1688. }
  1689. /**
  1690. * A numerically indexed list of field names of this persistent class.
  1691. *
  1692. * This array includes identifier fields if present on this class.
  1693. *
  1694. * @return array
  1695. */
  1696. public function getFieldNames()
  1697. {
  1698. return array_keys($this->fieldMappings);
  1699. }
  1700. /**
  1701. * A numerically indexed list of association names of this persistent class.
  1702. *
  1703. * This array includes identifier associations if present on this class.
  1704. *
  1705. * @return array
  1706. */
  1707. public function getAssociationNames()
  1708. {
  1709. return array_keys($this->associationMappings);
  1710. }
  1711. /**
  1712. * Returns the target class name of the given association.
  1713. *
  1714. * @param string $assocName
  1715. * @return string
  1716. */
  1717. public function getAssociationTargetClass($assocName)
  1718. {
  1719. if (!isset($this->associationMappings[$assocName])) {
  1720. throw new \InvalidArgumentException("Association name expected, '" . $assocName ."' is not an association.");
  1721. }
  1722. return $this->associationMappings[$assocName]['targetEntity'];
  1723. }
  1724. /**
  1725. * Get fully-qualified class name of this persistent class.
  1726. *
  1727. * @return string
  1728. */
  1729. public function getName()
  1730. {
  1731. return $this->name;
  1732. }
  1733. /**
  1734. * Gets the (possibly quoted) column name of a mapped field for safe use
  1735. * in an SQL statement.
  1736. *
  1737. * @param string $field
  1738. * @param AbstractPlatform $platform
  1739. * @return string
  1740. */
  1741. public function getQuotedColumnName($field, $platform)
  1742. {
  1743. return isset($this->fieldMappings[$field]['quoted']) ?
  1744. $platform->quoteIdentifier($this->fieldMappings[$field]['columnName']) :
  1745. $this->fieldMappings[$field]['columnName'];
  1746. }
  1747. /**
  1748. * Gets the (possibly quoted) primary table name of this class for safe use
  1749. * in an SQL statement.
  1750. *
  1751. * @param AbstractPlatform $platform
  1752. * @return string
  1753. */
  1754. public function getQuotedTableName($platform)
  1755. {
  1756. return isset($this->table['quoted']) ?
  1757. $platform->quoteIdentifier($this->table['name']) :
  1758. $this->table['name'];
  1759. }
  1760. /**
  1761. * Gets the (possibly quoted) name of the join table.
  1762. *
  1763. * @param AbstractPlatform $platform
  1764. * @return string
  1765. */
  1766. public function getQuotedJoinTableName(array $assoc, $platform)
  1767. {
  1768. return isset($assoc['joinTable']['quoted'])
  1769. ? $platform->quoteIdentifier($assoc['joinTable']['name'])
  1770. : $assoc['joinTable']['name'];
  1771. }
  1772. }