self::STAT

Hallo ich möchte gerne in der Detailansicht einer View mir die Anzahl einer anderen Tabelle anzeigen lassen.

Genauer gesagt, möcht ich die Anzahl der Bilder und Videos zu eine bestimmten Produkt (man kann zu einem Produkt Artikel schreiben, mit entweder einem Video oder Bild dazu).

DB-Struktur sieht grob von den verknüpften Tabellen so aus:


product <- likes

        <- article <- image

                   <- video

das Model sieht so aus:


<?php

public function relations()

	{

		return array(

			'articles' => array(self::HAS_MANY, 'Article', 'productid'),

			'likes' => array(self::HAS_MANY, 'Likes', 'productid'),

            'likes_count' => array(self::STAT, 'Likes', 'likes(id,productid)'),

?>

der Controller so:


<?php

	public function actionView()

	{

        $likes=Products::model()->with('likes_count')->findAll();

?>

und so gebe ich es aus:


<?php echo count($model->likes); ?>

Eine direkte Ausgabe der Daten aus der Tabelle Videos oder Images ist im übrigen möglich:




<?php foreach($model->articles as $foreignobj) {


	printf(CHtml::link('<img src="http://img.youtube.com/vi/'.$foreignobj->video->youtube_id.'/2.jpg" alt="'.$foreignobj->title.'" />', array('article/view', 'id' => $foreignobj->id)));


} ?>



Wer kann mir da weiterhelfen? Die Suche war leider recht erfolglos.

Hier noch ein paar Infos:

ich möchte die Anzahl der Ergebnisse aus der Tabelle ideo und image, wenn ich ein bestimmtes Produkt aufrufe. Einen count() über die likes oder die Artikel ist kein Problem, nur aus den anderem beiden Tabellen nicht (FKs zu articles).

Die Relations im Model sind alle korrekt definiert.

Product:

‘articles’ => array(self::HAS_MANY, ‘Article’, ‘productid’),

‘likes’ => array(self::HAS_MANY, ‘Likes’, ‘productid’),

Article:

‘product’ => array(self::BELONGS_TO, ‘Products’, ‘productid’),

‘image’ => array(self::BELONGS_TO, ‘Images’, ‘imageid’),

‘video’ => array(self::BELONGS_TO, ‘Videos’, ‘videoid’),

Image:

‘articles’ => array(self::HAS_MANY, ‘Article’, ‘imageid’),

Video:

‘articles’ => array(self::HAS_MANY, ‘Article’, ‘videoid’),