GetArrayResult() With Many-To-Many Joins

Viewed 28

I'm joining a table using ManyToMany realationsships

$query = $this->createQueryBuilder('f');
$query->select("f.value value, f.description description, f.optionsValue 
                o.code optionCode, l.code localeCode")
                    ->leftJoin('f.locales', 'l')
                    ->leftJoin('f.options', 'o')
                     ....other joins

return $query->getQuery()->getArrayResult();
....
/**
 * @ORM\ManyToMany(targetEntity="Locale")
 * @ORM\JoinTable(name="w_file_values_locales",
 *  joinColumns={@ORM\JoinColumn(name="file_value_id", referencedColumnName="id")},
 *  inverseJoinColumns={@ORM\JoinColumn(name="locale_id", referencedColumnName="id")}
 * )
 * @var ArrayCollection $locales
 */
private $locales;

/**
 * @ORM\ManyToMany(targetEntity="Option")
 * @ORM\JoinTable(name="w_file_values_options",
 *  joinColumns={@ORM\JoinColumn(name="file_value_id", referencedColumnName="id")},
 *  inverseJoinColumns={@ORM\JoinColumn(name="option_id", referencedColumnName="id")}
 * )
 * @var ArrayCollection $options
 */
private $options;
....

The problem is that the resulting Array doesn't return localeCode and optionCode as an Array of codes (strings) but returns only a string representing the first value of the 2 joined tables.

1 Answers

Not 100% certain what you are trying to do, but here is my guess.

You want the locales that are part of that main entity (referenced with f) in the output as an array in a field called localeCode and similar with options. Since it is an array and can be multiple I'd call the field localeCodes for output, or just locales as it is already called. If that's the case the following select portion should do the trick, while keeping the joins you have as well.

$query->select("f.value value, f.description description, f.options optionCode, f.locales localeCode")
Related