AFAIK, , join
. , getName
. join
Traversable
glue
.
, , .
__toString
User
__toString
, .
, toString
,
namespace Acme\FooBundle\Entity;
class User
{
public function __toString()
{
return $this->getName();
}
}
join
{{ entity.users|join(' - ') }}
, , .
: , getUsers
PersistentCollection
, , array_map
$users = $entity->getUsers();
$usernames = $users->map(function(User $user) {
return $user->getName();
});
return $this->render('AcmeFooBundle:My:foo.html.twig', array(
'entity' => $entity,
'usernames' => $usernames
);
{{ usernames|join(' - ') }}
TWIG
Twig Extension. joinBy
, join
, .
, , ,
services.yml
acme_foo.tools_extension:
class: Acme\FooBundle\Twig\ToolsExtension
tags:
- { name: twig.extension }
@Acme\FooBundle\Twig\ToolsExtension
namespace Acme\FooBundle\Twig;
use Twig_Extension, Twig_Filter_Method;
use InvalidArgumentException, UnexpectedValueException;
use Traversable;
class ToolsExtension extends Twig_Extension
{
public function getName()
{
return 'acme_foo_tools_extension';
}
public function getFilters()
{
return array(
'joinBy' => new Twig_Filter_Method($this, 'joinBy')
);
}
public function joinBy($data, $value, $join = null)
{
if (!is_array($data) && !($data instanceof Traversable)) {
throw new InvalidArgumentException(sprintf(
"Expected array or instance of Traversable for ToolsExtension::joinBy, got %s",
gettype($data)
));
}
$formatted = array();
foreach ($data as $row) {
$formatted[] = $this->getInput($row, $value);
}
return implode($formatted, $join);
}
protected function getInput($row, $find)
{
if (is_array($row) && array_key_exists($find, $row)) {
return $row[$find];
}
if (is_object($row)) {
if (isset($row->$find)) {
return $row->$find;
}
if (method_exists($row, $find)) {
return $row->$find();
}
foreach (array('get%s', 'is%s') as $indic) {
$method = sprintf($indic, $find);
if (method_exists($row, $method)) {
return $row->$method();
}
}
if (method_exists($row, $method)) {
return $row->$method();
}
}
throw new UnexpectedValueException(sprintf(
"Could not find any method to resolve \"%s\" for %s",
$find,
is_array($row)
? 'Array'
: sprintf('Object(%s)', get_class($row))
));
}
}
,
{{ entity.users|joinBy('name', ' - ') }}
{{ entity.users|joinBy('getName', ' - ') }}