Apperantly, Doctrine stores all the queries it makes in the variable that made it, in my case
protected $queryBuilder;
Thats why it displays so many rows. I executed the displayComment function before DisplaySIngle Post and again, the single post ones had too many rows, since it memorized the executed stuff from displayComment
So, what I did was to mimic the behaviour of $queryBuilder into a method inside the class like this:
public function newQuery()
{
return $this->queryBuilder = $this->db->createQueryBuilder();
}
So every time we call $query_Builder, it gets $this->db->createQueryBuilder(); brand new, like this:
public function displayComment()
{
$row = [];
$query = $this->newQuery()
->select('*')
->from('reactions')
->where('topic_id = '.$this->topicId)
->execute();
// echo $sql=$query->getSQL();
while($row = $query->fetch(PDO::FETCH_ASSOC))
{
$row2[] = $row['id'];
}
var_dump( $row2 );
}
And the whole Posts.php class:
<?php
class Posts
{
protected $db;
protected $queryBuilder;
protected $topicId = 1;
public function __construct($db)
{
$this->db = $db;
}
public function newQuery()
{
return $this->queryBuilder = $this->db->createQueryBuilder();
}
public function listTopics()
{
$query = $this->queryBuilder
->select('posts_id, posts_title, posts_date, author_id')
->from('posts')
->where('posts_categories_id = 1')
->execute();
return $query->fetchAll();
}
public function countComments()
{
$query = $this->queryBuilder
->select('comments_id')
->from('comments')
->where('comments_topic_id = 1')
->execute();
return $query->fetchAll();
}
public function displaySingleTopic()
{
$query = $this->newQuery()
->select('*')
->from('posts')
->where('posts_id = '.$_GET['id'])
->execute();
return $query->fetchAll();
}
public function insertComment($comment_data)
{
// extract($comment_data);
$user_id = 1;
$query = $this->newQuery()
->insert('reactions')
->values(
array(
'comment' => '?',
'user_id' => '?',
'topic_id' => '?',
'date_posted' => "STRFTIME('%s','now')"
)
)
->setParameter(0, $comment)
->setParameter(1, $user_id)
->setParameter(2, $this->topicId)
->execute();
}
public function displayComment()
{
$row = [];
$query = $this->newQuery()
->select('*')
->from('reactions')
->where('topic_id = '.$this->topicId)
->execute();
// echo $sql=$query->getSQL();
while($row = $query->fetch(PDO::FETCH_ASSOC))
{
$row2[] = $row['id'];
}
var_dump( $row2 );
}
}
Remember people, always use a fresh variable for multiple queries if you are using the QueryBuilder of Doctrine DBAL.
We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.