4790

Access to many-to-many relation in array format, symfony2

Question:

in symfony we can access many-to-many relations with getter functions which return objects of ArrayCollection type. for example for getting Alex's students we can call $alex->getStudens(), then i have access to ale's studens object.

now my question is how i can access alex's students id's in array, for example by calling $alex->getStudentsIds() it returns {1,5,7,12,..} , which are his students's ids.

Answer1:

precisely how you wrote it, you add another function in the entity

public function getStudentsIds() { $students = $this->students; $studentIds = []; foreach($students as $student) { $studentIds[] = $student->getId(); } return $studentIds; }

Ideal solution would be to add such a method to a repository and have it query only for student ids for given object but this is the simpliest solution possible.

Recommend

  • ACTION_IMAGE_CAPTURE returns imagefile as Extra instead of Data
  • htaccess rewrite to remove query string
  • How to extract data between same html tags
  • django filer - ImportError: cannot import name mixins
  • How to create mentions for names like “@myname” using javascript?
  • Jquery hide first 12 elementes, show next 12 elements
  • else if statement in javascript not able to display validation message
  • How can I do some cleanup right before an Android app is quitting?
  • Linked tables and Slicer in excel
  • How to filter entities that are deleted using linq to entities
  • Creating a Multidimensional, Associative Array in VBScript
  • How to Cache Real-time Data?
  • How to use RequestBodyAdvice
  • Ajax Loaded meta Tags
  • Q promise. Difference between .when and .then
  • Xamarin Forms - UWP Fonts
  • How to make a tree having multiple type of nodes and each node can have multiple child nodes in java
  • Incrementing object id automatically JS constructor (static method and variable)
  • Does CUDA 5 support STL or THRUST inside the device code?
  • Arrow is showed instead of the material design version hamburger icon. Why doesn't syncState in
  • When should I choose bucket sort over other sorting algorithms?
  • Hazelcast - OperationTimeoutException
  • To display the title for the current loaction in map in iphone
  • Akka Routing: Reply's send to router ends up as dead letters
  • AT Commands to Send SMS not working in Windows 8.1
  • Arrays break string types in Julia
  • How to delete a row from a dynamic generate table using jquery?
  • json Serialization in asp
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • How do I configure my settings file to work with unit tests?
  • Change div Background jquery
  • How to stop GridView from loading again when I press back button?
  • How does Linux kernel interrupt the application?
  • Is it possible to post an object from jquery to bottle.py?
  • Busy indicator not showing up in wpf window [duplicate]
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • Why do underscore prefixed variables exist?