Answer the question
In order to leave comments, you need to log in
What is the correct way to work with doctrine collections in symfony2?
I am implementing tags on the site and now I came across a problem (more precisely a question) that I would like to clarify.
There is a Post entity in which there is such a thing:
public function getTags()
{
return $this->tags ?: $this->tags = new ArrayCollection();
}
Tags: Vasya, Friends, Family
foreach ($entity->getTags()->getValues() as $tag) {
print_r($tag->getName());
}
<div class="news-tags">
{% if entity.tags.values %}
<span class="font-bold" >{{ 'news.post.tags'|trans({}, 'NewsBundle') }}:</span>
{% else %}
{{ 'news.post.no_tags'|trans({}, 'NewsBundle') }}
{% endif %}
<ul class="tags-list">
{% if entity.tags.values %}
{% for tag in entity.tags.values %}
<li><a href="{{ url('news_tag', {'tag':tag.name} )}}">{{ tag.name }}</a></li>
{% endfor %}
{% endif %}
</ul>
</div>
Answer the question
In order to leave comments, you need to log in
first of all it's better
public function __construct()
{
$this->tags = new ArrayCollection();
}
{% if entity.tags|length %}
Теги есть!
{% endif %}
{% for tag in entity.tags %}
{% if not loop.first %}, {%endif %}
{% tag.name %}
{% endfor %}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question