I’ve hit a small-ish problem while working with Yii framework.
To explain the situation:
I have to create a sales-promotion slider of sorts. Each promotion consists of a url to more information about the promotion and an image url. Each promotion has several localized versions of it’s image. For this purpose I’ve created 2 database tables –
First is the promotions table:
CREATE TABLE IF NOT EXISTS `promotions` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(100) NOT NULL,
`image` int(10) unsigned DEFAULT NULL,
`destination` varchar(200) NOT NULL,
`status` tinyint(3) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `image` (`image`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;
and second is the promotion images table:
CREATE TABLE IF NOT EXISTS `promoImages` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`pid` int(10) unsigned NOT NULL,
`language` varchar(2) NOT NULL,
`url` varchar(200) NOT NULL,
PRIMARY KEY (`id`),
KEY `language` (`language`),
KEY `mhm` (`pid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=10 ;
The idea with such database structure is that in each promotions table row in the field image it contains an identifier for the appropriate image, then in table promoImages the related column pid contains the unique promotion identifier. Each promotion can be localized in several undefined languages, so for each image that is related to a single promotion there can be several rows in promoImages, an example of this:
INSERT INTO `promoImages` (`id`, `pid`, `language`, `url`) VALUES
(1, 1, 'lv', '/path/to/image1_lv.png'),
(2, 2, 'lv', '/path/to/image2_lv.png'),
(3, 3, 'lv', '/path/to/image3_lv.png'),
(4, 1, 'en', '/path/to/image1_en.png'),
(5, 2, 'en', '/path/to/image2_en.png'),
(6, 3, 'en', '/path/to/image3_en.png'),
(7, 1, 'ru', '/path/to/image1_ru.png'),
(8, 2, 'ru', '/path/to/image2_ru.png'),
(9, 3, 'ru', '/path/to/image3_ru.png');
Now with that structure in mind I also created a foreign key in table promotions which generated along with Yii frameworks “Gii” tool created models with such relations:
Table promotions model relation
'promoImages' => array(self::HAS_MANY, 'PromoImages', 'pid'),
and table promoImages model relation
'p' => array(self::BELONGS_TO, 'Promotions', 'pid'),
And as far as I know, the relation is correct, single promotion can have multiple promoImages table rows and any promoImages row can only belong to single promotion, as I correct to believe this is true?
I hope I’ve explained the situation as thoroughly as possible, now onto the issue itself.
As I mentioned at top, from all that I really only need to retrieve 2 values, 1 from each table: destination from promotions and url from promoImages but as I’m selecting these values I need to keep in mind only selecting those promotions whose status is “1” meaning that the promotion is active. Also when selecting the promotion image I need to select the row from table promoImages which corresponds to both the promotions image column aswell as current chosen application language(Yii::app()->language).
So far the code I have is as follows:
The promotion widget controller
public function run() {
$l = Yii::app()->language;
$promos = Promotions::model()->with('promoImages')->findAll('status=:status AND promoImages.language=:language', array(
':status' => 1,
':language' => $l
));
$this->render('promo', array('promotions' => $promos));
}
the promotion widget view
foreach ($promotions as $promotion) {
// $image = $promotion->promoImages->url;
$dest = $promotion->destination;
echo "<div class='promo_hold'>";
echo "<a href='$dest'><img src='$image'></a>";
echo "</div>";
}
This gives me the 3 div’s I need for the 3 active promotions but with empty images, if I uncomment the $image definition I get the Trying to get property of non-object error and this is basically where I’m stuck, I’ve looked at Yii’s class reference but so far it hasn’t gotten me any help.
If I’m missing any required information please don’t hesitate to ask.
Thanks beforehand!
P.S. I hope my wall of text doesn’t actually scare away people who might have an answer :O
Okay so I found the answer to my question myself so I’ll add it as an answer here in-case someone else has a problem like this.
While trying all kinds of variations to get the field I need I noticed that
$promotion->promoImagesis actually an array containing single element, the object that contains all the information relating to selected promotion and language.So I came to such a solution for this problem, in widgets foreach loop I simply set:
And that is showing me the correct, language selected image address.