Guest User

Untitled

a guest
Jul 30th, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.22 KB | None | 0 0
  1. Symfony: sfWidgetFormDoctrineChoice: how to retrieve a specific field
  2. Tables:
  3.  
  4. Resources(submit_user,...);
  5. Users(id, username,...); primary key(id);
  6.  
  7. In 'Resources': foreign key(submit_user) references Users(id);
  8.  
  9. ResourcesForm
  10.  
  11. 'submit_user' => new sfWidgetFormDoctrineChoice(array('model' => 'Users', 'add_empty' => false)) //Alternatively, 'model' =>getRelatedModelName('Users')
  12.  
  13. 'submit_user' => new sfValidatorDoctrineChoice(array('model' => 'Users', 'required' => false))
  14.  
  15. Resources:
  16. connection: doctrine
  17. tableName: resources
  18. columns:
  19. id:
  20. type: integer(4)
  21. fixed: false
  22. unsigned: true
  23. primary: true
  24. autoincrement: false
  25. aggregation:
  26. type: integer(1)
  27. fixed: false
  28. unsigned: true
  29. primary: false
  30. notnull: false
  31. autoincrement: false
  32. version:
  33. type: string(50)
  34. fixed: false
  35. unsigned: false
  36. primary: false
  37. notnull: false
  38. autoincrement: false
  39. version_status:
  40. type: integer(1)
  41. fixed: false
  42. unsigned: true
  43. primary: false
  44. notnull: false
  45. autoincrement: false
  46. interactivity_type:
  47. type: integer(1)
  48. fixed: false
  49. unsigned: true
  50. primary: false
  51. notnull: false
  52. autoincrement: false
  53. interactivity_level:
  54. type: integer(1)
  55. fixed: false
  56. unsigned: true
  57. primary: false
  58. notnull: false
  59. autoincrement: false
  60. semantic_density:
  61. type: integer(1)
  62. fixed: false
  63. unsigned: true
  64. primary: false
  65. notnull: false
  66. autoincrement: false
  67. difficulty_level:
  68. type: integer(1)
  69. fixed: false
  70. unsigned: true
  71. primary: false
  72. notnull: false
  73. autoincrement: false
  74. learning_time:
  75. type: integer(4)
  76. fixed: false
  77. unsigned: true
  78. primary: false
  79. notnull: false
  80. autoincrement: false
  81. url:
  82. type: string(100)
  83. fixed: false
  84. unsigned: false
  85. primary: false
  86. notnull: false
  87. autoincrement: false
  88. submit_user:
  89. type: string(15)
  90. fixed: false
  91. unsigned: false
  92. primary: false
  93. default: ''
  94. notnull: true
  95. autoincrement: false
  96. submit_date:
  97. type: timestamp(25)
  98. fixed: false
  99. unsigned: false
  100. primary: false
  101. default: '0000-00-00 00:00:00'
  102. notnull: true
  103. autoincrement: false
  104. last_updated:
  105. type: timestamp(25)
  106. fixed: false
  107. unsigned: false
  108. primary: false
  109. notnull: false
  110. autoincrement: false
  111. active:
  112. type: integer(1)
  113. fixed: false
  114. unsigned: true
  115. primary: false
  116. default: '1'
  117. notnull: true
  118. autoincrement: false
  119. type:
  120. type: integer(1)
  121. fixed: false
  122. unsigned: true
  123. primary: false
  124. default: '1'
  125. notnull: false
  126. autoincrement: false
  127. catalogue_entry:
  128. type: string(250)
  129. fixed: false
  130. unsigned: false
  131. primary: false
  132. notnull: false
  133. autoincrement: false
  134. author:
  135. type: string(250)
  136. fixed: false
  137. unsigned: false
  138. primary: false
  139. notnull: false
  140. autoincrement: false
  141. publisher_producer:
  142. type: string()
  143. fixed: false
  144. unsigned: false
  145. primary: false
  146. notnull: false
  147. autoincrement: false
  148. funding_body:
  149. type: string(250)
  150. fixed: false
  151. unsigned: false
  152. primary: false
  153. notnull: false
  154. autoincrement: false
  155. size:
  156. type: integer(8)
  157. fixed: false
  158. unsigned: true
  159. primary: false
  160. notnull: false
  161. autoincrement: false
  162. duration:
  163. type: time(25)
  164. fixed: false
  165. unsigned: false
  166. primary: false
  167. notnull: false
  168. autoincrement: false
  169. cost:
  170. type: integer(1)
  171. fixed: false
  172. unsigned: false
  173. primary: false
  174. default: '0'
  175. notnull: true
  176. autoincrement: false
  177. copyright:
  178. type: integer(1)
  179. fixed: false
  180. unsigned: false
  181. primary: false
  182. default: '0'
  183. notnull: true
  184. autoincrement: false
  185. filename:
  186. type: string(250)
  187. fixed: false
  188. unsigned: false
  189. primary: false
  190. default: ''
  191. notnull: true
  192. autoincrement: false
  193. protect:
  194. type: integer(1)
  195. fixed: false
  196. unsigned: false
  197. primary: false
  198. default: '0'
  199. notnull: true
  200. autoincrement: false
  201. title:
  202. type: string(250)
  203. fixed: false
  204. unsigned: false
  205. primary: false
  206. notnull: false
  207. autoincrement: false
  208. stat_hits:
  209. type: integer(4)
  210. fixed: false
  211. unsigned: false
  212. primary: false
  213. notnull: true
  214. autoincrement: false
  215. keywords:
  216. type: string(250)
  217. fixed: false
  218. unsigned: false
  219. primary: false
  220. notnull: false
  221. autoincrement: false
  222. password:
  223. type: string(45)
  224. fixed: false
  225. unsigned: false
  226. primary: false
  227. notnull: false
  228. autoincrement: false
  229. relations:
  230. ResourceContext:
  231. local: id
  232. foreign: id
  233. type: many
  234. ResourceDescriptions:
  235. local: id
  236. foreign: id
  237. type: many
  238. ResourceEndUser:
  239. local: id
  240. foreign: id
  241. type: many
  242. ResourceLanguages:
  243. local: id
  244. foreign: id
  245. type: many
  246. ResourceMetadata:
  247. local: id
  248. foreign: id
  249. type: many
  250. ResourceRelations:
  251. local: id
  252. foreign: target
  253. type: many
  254. ResourceRelations_2:
  255. class: ResourceRelations
  256. local: id
  257. foreign: source
  258. type: many
  259. Users:
  260. local: submit_user
  261. foreign: username
  262. type: one
  263. Users:
  264. connection: doctrine
  265. tableName: users
  266. columns:
  267. id:
  268. type: integer(4)
  269. fixed: false
  270. unsigned: false
  271. primary: true
  272. autoincrement: true
  273. username:
  274. type: string(15)
  275. fixed: false
  276. unsigned: false
  277. primary: false
  278. notnull: true
  279. autoincrement: false
  280. role:
  281. type: string(12)
  282. fixed: false
  283. unsigned: false
  284. primary: false
  285. notnull: true
  286. autoincrement: false
  287. firstname:
  288. type: string(20)
  289. fixed: false
  290. unsigned: false
  291. primary: false
  292. notnull: true
  293. autoincrement: false
  294. middlename:
  295. type: string(20)
  296. fixed: false
  297. unsigned: false
  298. primary: false
  299. notnull: false
  300. autoincrement: false
  301. lastname:
  302. type: string(30)
  303. fixed: false
  304. unsigned: false
  305. primary: false
  306. notnull: true
  307. autoincrement: false
  308. email:
  309. type: string(100)
  310. fixed: false
  311. unsigned: false
  312. primary: false
  313. notnull: true
  314. autoincrement: false
  315. created:
  316. type: timestamp(25)
  317. fixed: false
  318. unsigned: false
  319. primary: false
  320. notnull: false
  321. autoincrement: false
  322. relations:
  323. Resources:
  324. local: username
  325. foreign: submit_user
  326. type: many
  327.  
  328. public function getUsernames(){
  329.  
  330. $query = Doctrine_Query::create()
  331. ->select('u.username')
  332. ->from('Users u')
  333. ;
  334.  
  335. return $query->execute();
  336. }
  337.  
  338. $choice = new sfWidgetFormDoctrineChoice(array('model' => 'Users', 'table_method' => 'getUsernames', 'order_by' => array('name', 'asc')));
  339. $choices = $choice->getChoices();
  340. $this->widgetSchema['submit_user'] = new sfWidgetFormChoice(array('choices' => $choices));
  341.  
  342. $this->validatorSchema['submit_user'] = new sfValidatorChoice(array('choices' => $choices));
  343.  
  344. $choice = new sfWidgetFormDoctrineChoice(array('model' => $model, 'table_method' => 'getUsernames', 'order_by' => array('name', 'asc')));
  345.  
  346. // Loads the data once
  347. $choices = $choice->getChoices();
  348.  
  349. // Lets set the keys the same as the value so when form is bound
  350. // it should pass valid test AND be able to insert/update accordindly
  351. $choices = array_combine($choices, $choices);
  352.  
  353. // Notice the switch from sf*FormDoctrineChoice to the default sf*FromChoice API calls
  354. // This is because we already have the data loaded, so no need to call on doctrine's
  355. // versions again
  356.  
  357. $this->widgetSchema['submit_user'] = new sfWidgetFormChoice(array('choices' => $choices));
  358.  
  359. // Normally this would have an array_keys() call, but aren't concerned with ids
  360. // we are concerned with user names, so it should be a straight pass through
  361. $this->validatorSchema['submit_user'] = new sfValidatorChoice(array('choices' => $choices));
Add Comment
Please, Sign In to add comment