ClassMetadataBuilder.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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\Builder;
  20. use Doctrine\ORM\Mapping\ClassMetadata,
  21. Doctrine\ORM\Mapping\ClassMetadataInfo;
  22. /**
  23. * Builder Object for ClassMetadata
  24. *
  25. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  26. * @link www.doctrine-project.com
  27. * @since 2.2
  28. * @author Benjamin Eberlei <kontakt@beberlei.de>
  29. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  30. */
  31. class ClassMetadataBuilder
  32. {
  33. /**
  34. * @var \Doctrine\ORM\Mapping\ClassMetadataInfo
  35. */
  36. private $cm;
  37. /**
  38. * @param \Doctrine\ORM\Mapping\ClassMetadataInfo $cm
  39. */
  40. public function __construct(ClassMetadataInfo $cm)
  41. {
  42. $this->cm = $cm;
  43. }
  44. /**
  45. * @return ClassMetadata
  46. */
  47. public function getClassMetadata()
  48. {
  49. return $this->cm;
  50. }
  51. /**
  52. * Mark the class as mapped superclass.
  53. *
  54. * @return ClassMetadataBuilder
  55. */
  56. public function setMappedSuperClass()
  57. {
  58. $this->cm->isMappedSuperclass = true;
  59. return $this;
  60. }
  61. /**
  62. * Set custom Repository class name
  63. *
  64. * @param string $repositoryClassName
  65. * @return ClassMetadataBuilder
  66. */
  67. public function setCustomRepositoryClass($repositoryClassName)
  68. {
  69. $this->cm->setCustomRepositoryClass($repositoryClassName);
  70. return $this;
  71. }
  72. /**
  73. * Mark class read only
  74. *
  75. * @return ClassMetadataBuilder
  76. */
  77. public function setReadOnly()
  78. {
  79. $this->cm->markReadOnly();
  80. return $this;
  81. }
  82. /**
  83. * Set the table name
  84. *
  85. * @param string $name
  86. * @return ClassMetadataBuilder
  87. */
  88. public function setTable($name)
  89. {
  90. $this->cm->setPrimaryTable(array('name' => $name));
  91. return $this;
  92. }
  93. /**
  94. * Add Index
  95. *
  96. * @param array $columns
  97. * @param string $name
  98. * @return ClassMetadataBuilder
  99. */
  100. public function addIndex(array $columns, $name)
  101. {
  102. if (!isset($this->cm->table['indexes'])) {
  103. $this->cm->table['indexes'] = array();
  104. }
  105. $this->cm->table['indexes'][$name] = array('columns' => $columns);
  106. return $this;
  107. }
  108. /**
  109. * Add Unique Constraint
  110. *
  111. * @param array $columns
  112. * @param string $name
  113. * @return ClassMetadataBuilder
  114. */
  115. public function addUniqueConstraint(array $columns, $name)
  116. {
  117. if ( ! isset($this->cm->table['uniqueConstraints'])) {
  118. $this->cm->table['uniqueConstraints'] = array();
  119. }
  120. $this->cm->table['uniqueConstraints'][$name] = array('columns' => $columns);
  121. return $this;
  122. }
  123. /**
  124. * Add named query
  125. *
  126. * @param string $name
  127. * @param string $dqlQuery
  128. * @return ClassMetadataBuilder
  129. */
  130. public function addNamedQuery($name, $dqlQuery)
  131. {
  132. $this->cm->addNamedQuery(array(
  133. 'name' => $name,
  134. 'query' => $dqlQuery,
  135. ));
  136. return $this;
  137. }
  138. /**
  139. * Set class as root of a joined table inheritance hierachy.
  140. *
  141. * @return ClassMetadataBuilder
  142. */
  143. public function setJoinedTableInheritance()
  144. {
  145. $this->cm->setInheritanceType(ClassMetadata::INHERITANCE_TYPE_JOINED);
  146. return $this;
  147. }
  148. /**
  149. * Set class as root of a single table inheritance hierachy.
  150. *
  151. * @return ClassMetadataBuilder
  152. */
  153. public function setSingleTableInheritance()
  154. {
  155. $this->cm->setInheritanceType(ClassMetadata::INHERITANCE_TYPE_SINGLE_TABLE);
  156. return $this;
  157. }
  158. /**
  159. * Set the discriminator column details.
  160. *
  161. * @param string $name
  162. * @param string $type
  163. */
  164. public function setDiscriminatorColumn($name, $type = 'string', $length = 255)
  165. {
  166. $this->cm->setDiscriminatorColumn(array(
  167. 'name' => $name,
  168. 'type' => $type,
  169. 'length' => $length,
  170. ));
  171. return $this;
  172. }
  173. /**
  174. * Add a subclass to this inheritance hierachy.
  175. *
  176. * @param string $name
  177. * @param string $class
  178. * @return ClassMetadataBuilder
  179. */
  180. public function addDiscriminatorMapClass($name, $class)
  181. {
  182. $this->cm->addDiscriminatorMapClass($name, $class);
  183. return $this;
  184. }
  185. /**
  186. * Set deferred explicit change tracking policy.
  187. *
  188. * @return ClassMetadataBuilder
  189. */
  190. public function setChangeTrackingPolicyDeferredExplicit()
  191. {
  192. $this->cm->setChangeTrackingPolicy(ClassMetadata::CHANGETRACKING_DEFERRED_EXPLICIT);
  193. return $this;
  194. }
  195. /**
  196. * Set notify change tracking policy.
  197. *
  198. * @return ClassMetadataBuilder
  199. */
  200. public function setChangeTrackingPolicyNotify()
  201. {
  202. $this->cm->setChangeTrackingPolicy(ClassMetadata::CHANGETRACKING_NOTIFY);
  203. return $this;
  204. }
  205. /**
  206. * Add lifecycle event
  207. *
  208. * @param string $methodName
  209. * @param string $event
  210. * @return ClassMetadataBuilder
  211. */
  212. public function addLifecycleEvent($methodName, $event)
  213. {
  214. $this->cm->addLifecycleCallback($methodName, $event);
  215. return $this;
  216. }
  217. /**
  218. * Add Field
  219. *
  220. * @param string $name
  221. * @param string $type
  222. * @param array $mapping
  223. */
  224. public function addField($name, $type, array $mapping = array())
  225. {
  226. $mapping['fieldName'] = $name;
  227. $mapping['type'] = $type;
  228. $this->cm->mapField($mapping);
  229. return $this;
  230. }
  231. /**
  232. * Create a field builder.
  233. *
  234. * @param string $name
  235. * @param string $type
  236. * @return FieldBuilder
  237. */
  238. public function createField($name, $type)
  239. {
  240. return new FieldBuilder(
  241. $this,
  242. array(
  243. 'fieldName' => $name,
  244. 'type' => $type
  245. )
  246. );
  247. }
  248. /**
  249. * Add a simple many to one association, optionally with the inversed by field.
  250. *
  251. * @param string $name
  252. * @param string $targetEntity
  253. * @param string|null $inversedBy
  254. * @return ClassMetadataBuilder
  255. */
  256. public function addManyToOne($name, $targetEntity, $inversedBy = null)
  257. {
  258. $builder = $this->createManyToOne($name, $targetEntity);
  259. if ($inversedBy) {
  260. $builder->inversedBy($inversedBy);
  261. }
  262. return $builder->build();
  263. }
  264. /**
  265. * Create a ManyToOne Assocation Builder.
  266. *
  267. * Note: This method does not add the association, you have to call build() on the AssociationBuilder.
  268. *
  269. * @param string $name
  270. * @param string $targetEntity
  271. * @return AssociationBuilder
  272. */
  273. public function createManyToOne($name, $targetEntity)
  274. {
  275. return new AssociationBuilder(
  276. $this,
  277. array(
  278. 'fieldName' => $name,
  279. 'targetEntity' => $targetEntity
  280. ),
  281. ClassMetadata::MANY_TO_ONE
  282. );
  283. }
  284. /**
  285. * Create OneToOne Assocation Builder
  286. *
  287. * @param string $name
  288. * @param string $targetEntity
  289. * @return AssociationBuilder
  290. */
  291. public function createOneToOne($name, $targetEntity)
  292. {
  293. return new AssociationBuilder(
  294. $this,
  295. array(
  296. 'fieldName' => $name,
  297. 'targetEntity' => $targetEntity
  298. ),
  299. ClassMetadata::ONE_TO_ONE
  300. );
  301. }
  302. /**
  303. * Add simple inverse one-to-one assocation.
  304. *
  305. * @param string $name
  306. * @param string $targetEntity
  307. * @param string $mappedBy
  308. * @return ClassMetadataBuilder
  309. */
  310. public function addInverseOneToOne($name, $targetEntity, $mappedBy)
  311. {
  312. $builder = $this->createOneToOne($name, $targetEntity);
  313. $builder->mappedBy($mappedBy);
  314. return $builder->build();
  315. }
  316. /**
  317. * Add simple owning one-to-one assocation.
  318. *
  319. * @param string $name
  320. * @param string $targetEntity
  321. * @param string $inversedBy
  322. * @return ClassMetadataBuilder
  323. */
  324. public function addOwningOneToOne($name, $targetEntity, $inversedBy = null)
  325. {
  326. $builder = $this->createOneToOne($name, $targetEntity);
  327. if ($inversedBy) {
  328. $builder->inversedBy($inversedBy);
  329. }
  330. return $builder->build();
  331. }
  332. /**
  333. * Create ManyToMany Assocation Builder
  334. *
  335. * @param string $name
  336. * @param string $targetEntity
  337. * @return ManyToManyAssociationBuilder
  338. */
  339. public function createManyToMany($name, $targetEntity)
  340. {
  341. return new ManyToManyAssociationBuilder(
  342. $this,
  343. array(
  344. 'fieldName' => $name,
  345. 'targetEntity' => $targetEntity
  346. ),
  347. ClassMetadata::MANY_TO_MANY
  348. );
  349. }
  350. /**
  351. * Add a simple owning many to many assocation.
  352. *
  353. * @param string $name
  354. * @param string $targetEntity
  355. * @param string|null $inversedBy
  356. * @return ClassMetadataBuilder
  357. */
  358. public function addOwningManyToMany($name, $targetEntity, $inversedBy = null)
  359. {
  360. $builder = $this->createManyToMany($name, $targetEntity);
  361. if ($inversedBy) {
  362. $builder->inversedBy($inversedBy);
  363. }
  364. return $builder->build();
  365. }
  366. /**
  367. * Add a simple inverse many to many assocation.
  368. *
  369. * @param string $name
  370. * @param string $targetEntity
  371. * @param string $mappedBy
  372. * @return ClassMetadataBuilder
  373. */
  374. public function addInverseManyToMany($name, $targetEntity, $mappedBy)
  375. {
  376. $builder = $this->createManyToMany($name, $targetEntity);
  377. $builder->mappedBy($mappedBy);
  378. return $builder->build();
  379. }
  380. /**
  381. * Create a one to many assocation builder
  382. *
  383. * @param string $name
  384. * @param string $targetEntity
  385. * @return OneToManyAssociationBuilder
  386. */
  387. public function createOneToMany($name, $targetEntity)
  388. {
  389. return new OneToManyAssociationBuilder(
  390. $this,
  391. array(
  392. 'fieldName' => $name,
  393. 'targetEntity' => $targetEntity
  394. ),
  395. ClassMetadata::ONE_TO_MANY
  396. );
  397. }
  398. /**
  399. * Add simple OneToMany assocation.
  400. *
  401. * @param string $name
  402. * @param string $targetEntity
  403. * @param string $mappedBy
  404. * @return ClassMetadataBuilder
  405. */
  406. public function addOneToMany($name, $targetEntity, $mappedBy)
  407. {
  408. $builder = $this->createOneToMany($name, $targetEntity);
  409. $builder->mappedBy($mappedBy);
  410. return $builder->build();
  411. }
  412. }