如何从 phalcon 中的数据库中检索相似的标签数据?

How to retrieve similar tags data from db in phalcon?

我是 Phalcon 的新手。我决定将 Phalcon php 作为 Codeigniter 的替代 php 框架。我正在实现一个带有标签的博客。我首先将标签值插入到数据库的单个列中。我遵循以下标签插件示例:https://github.com/mfiels/tagsly/blob/master/index.html 它将多个值插入到单个列中,例如 "php,jquery,asp,html,css".

现在我只是像这样从 db 中检索值到 volt:

[controller]

$bloger = $this->modelsManager->executeQuery("SELECT * FROM Blogs ORDER BY Blogs.datetime DESC");
$this->view->setVar('blogs', $bloger);

[volt]        

<?php 
$blogTags = array(); 
$blogTags = $bloger->tags;
$tags = explode(',',$blogTags); 
foreach($tags as $taged){ ?>
<a class="tags" href="blog/tag/<?php echo($taged); ?>">
<?php echo($taged); ?> <span>[ 0 ]</span></a>
<?php } ?>

现在 link 就像:"localhost/demo/blog/tag/php""localhost/demo/blog/tag/jquery" 我的问题是如何从数据库中检索每个标签相关的数据?

我正在尝试这样查询:

[控制器]

public function tagAction($taged)
{
$tags = Blogs::findBytags($taged);
$tagData = array();
$tagData = explode(',', $tags->tags);
$similar = Blogs::find(["tags LIKE :title:","bind"=> ["title"=>'%'.$tagData.'%'],"order" => "datetime DESC limit 5"]);
$this->view->setVar('tagged', $similar);
$this->view->pick('blog/tagline');
}

[伏]

{% for similar in tagged %}
{{tagged.btitle}}
{% endfor %}

但未按预期呈现。我如何检索匹配数据?

您可以遍历所有当前标签并将它们一一添加到您的查询中。 在循环期间,您还创建了绑定元素数组。

[controller] 
...
$currenttags =  explode(',', $blog->tags);

$query          = Blogs::query();
$bindParameters = [];

for($i = 0; $i < count($currenttags); $i++) {
   $query->orWhere('tags LIKE :tag' . $i . ':');
   $bindParameters['tag' . $i] = '%' . $currenttags[$i] . '%';
}

$query->bind($bindParameters);
$similar = $query->execute();

$this->view->setVar('datas', $similar); 

我的期望是,当用户访问 post 的详细视图时,我想在该页面上显示 post related/similar 另一个 post。相似度由它的标签决定,现在我是这样算的:

[controller]    

public function showfullAction($id)
{
$blog = Blogs::findFirstByid($id); 
$this->view->setVar('detail', $blog);
$currenttags =  explode(',',$blog->tags);

I want to make Loop Throw....
$dataCount = count($currenttags);
$tags1 = $currenttags[0];
$tags1 = $currenttags[0];
$tags2 = $currenttags[1];
$tags3 = $currenttags[2];
$tags4 = $currenttags[3];
$tags5 = $currenttags[4];
$tags6 = $currenttags[5];
$tags7 = $currenttags[6];
$tags8 = $currenttags[7];
$tags9 = $currenttags[8];

if($dataCount == '1')
{
$similar = $this->modelsManager->executeQuery("SELECT Blogs.* FROM Blogs WHERE Blogs.tags LIKE '%$tags1%'");
}
elseif($dataCount == '2')
{
$similar = $this->modelsManager->executeQuery("SELECT Blogs.* FROM Blogs WHERE Blogs.tags LIKE '%$tags1%' or Blogs.tags LIKE '%$tags2%'");
}
elseif($dataCount == '3')
{
$similar = $this->modelsManager->executeQuery("SELECT Blogs.* FROM Blogs WHERE Blogs.tags LIKE '%$tags1%' or Blogs.tags LIKE '%$tags2%' or Blogs.tags LIKE '%$tags3%'");
}


 $this->view->setVar('datas', $similar); 

等等...

[View]

{% for similar in datas %}
{{link_to('blog/showfull/'~similar.id,similar.btitle,'class':'cats')}}  
{% endfor %}

现在它按预期工作但是有没有另一种简单的小简单方法可以做到这一点?请!谢谢蒂莫西