0
Ich habe eine Artikelentität. Artikel hat Eigenschaftenansichten. Views-Tabelle sieht wie folgt aus:Symfonie. Zählen Zeilen für Entitätseigenschaft nach Entitäts-ID
id | Artikel_ID | ip
einzigartig (article_id, ip)
Jetzt mache ich folgendes, ich findAll Methode in ArticleRepository ändern. Ist es ein richtiger Weg oder ich kann es in Artikel-Entität tun?
Thx für jede Hilfe.
/**
* Class ArticleRepository
*
* @package FrontendBundle\Entity
*/
class ArticleRepository extends EntityRepository
{
/**
* @return array
*/
public function findAll()
{
if ($all = parent::findAll()) {
foreach ($all as $article) {
$article->setViews($this->getViews($article->getId()));
}
}
return $all;
}
/**
* Add view to article.
*
* @param int $id Article id.
* @param string $ip User clients ip
*/
public function addView($id, $ip)
{
try
{
$this->_em->getConnection()->insert('articles_views', [
'article_id' => $id,
'ip' => $ip
]);
}
catch(UniqueConstraintViolationException $e)
{
// do nothing
}
}
/**
* Return views for article.
* @param int $id
*
* @return mixed
* @throws NonUniqueResultException If the query result is not unique.
* @throws NoResultException If the query returned no result.
*/
public function getViews($id)
{
// here is custom sql query to get count rows from articles_views
}
Sie nicht mich verstehen. articles_views hat keine Entität, daher funktioniert QueryBuilder nicht. – Tapakan
Wie man Abfrage ich weiß. Ich möchte wissen, ob das ein richtiger Weg/symfony Weg ist? Vielleicht gibt es einen besseren Weg? – Tapakan