symfony - Symfony2 - Need help on the following error when querying for tags -
having following error in doctrine query when trying find specific tag selected user.
[semantical error] line 0, col 78 near 'tag blog.tags': error: class acme\demobundle\entity\blog has no association named tags
can tell what's wrong query? (trying query tag selected in side bar brings posts related tag)
repository
public function getpostsbytags($tags) { $qb = $this->createquerybuilder('b'); $qb->select('b') ->join('b.tags', 'tag') ->where('b.tags ?', '%'.$tags.'%'); return $qb->getquery()->getresult(); }
blog entity
/** * @var string * * @orm\column(name="tags", type="text") */ private $tags; /** * set tags * * @param string $tags * @return blog */ public function settags($tags) { $this->tags = $tags; return $this; } /** * tags * * @return string */ public function gettags() { return $this->tags; }
controller
/** * @route("/tag/{tag}", name="acmedemobundle_tag") * @template("acmedemobundle:page:tag.html.twig") */ public function tagaction($tag = null) { $em = $this->getdoctrine()->getmanager(); $tags = $em->getrepository('acmedemobundle:blog') ->getpostsbytags($tag); if (!$tags) { throw $this->createnotfoundexception('unable find blog posts'); } return array( 'tags' => $tags, ); }
sidebar twig
<p class="tags"> {% tag, weight in tags %} <span class="weight-{{ weight }}"><a href="{{ path('acmedemobundle_tag', { 'tag': tag }) }}">{{ tag }}</a></span> {% else %} <p>there no tags</p> {% endfor %} </p>
tag results twig
{% block body %} {% tag in tags %} <article class="result"> <div class="date"><time datetime="{{ tag.created|date('c') }}">{{ tag.created|date('l, f j, y') }}</time></div> <header> <h2><a href="{{ path('acmedemobundle_show', { 'id': tag.id, 'slug': tag.slug }) }}">{{ tag.title }}</a></h2> </header> <img src="{{ asset(['images/', tag.image]|join) }}" /> <div class="snippet"> <p>{{ tag.blog|truncate(250, true) }}</p> <p class="continue"><a href="{{ path('acmedemobundle_show', { 'id': tag.id, 'slug': tag.slug }) }}">more...</a></p> </div> <footer class="meta"> <p>comments: -</p> <p>posted <span class="highlight">{{tag.author}}</span> @ {{ tag.created|date('h:ia') }}</p> <p>tags: <span class="highlight">{{ tag.tags }}</span></p> </footer> </article> {% else %} <p>there no blog entries health&fitness blog</p> {% endfor %} {% endblock %}
updated solution: repository query (no blogs found)
public function getpostsbytags($tags) { $query = $this->createquerybuilder('b') ->where('b.tags = :tags') ->setparameter('tags', $tags); return $query->getquery()->getresult(); }
updated solution: controller using query (no blogs found)
public function tagaction(tags=null) { $em = $this->getdoctrine()->getmanager(); $repository = $em->getrepository('acmedemobundle:blog'); $tags = $repository->createquerybuilder('b') ->where('b.tags = :tags') ->setparameter('tags', $tags) ->getquery() ->getresult(); return array( 'tags' => $tags, ); }
change getpostsbytags
function to:
$repository = $this->getdoctrine() ->getrepository('acmedemobundle:blog'); $query = $repository->createquerybuilder('b') ->where('b.tags = :tags') ->setparameter('tags', $tags) ->getquery(); return $query->getresult();
Comments
Post a Comment