NDB komplikovany where cez spojovaciu tabulku
Upozornění: Tohle vlákno je hodně staré a informace nemusí být platné pro současné Nette.
- duskohu
- Člen | 778
Caute neviete mi poradit mam tabulku product a tabulku relatedproduct, len neviem ako mam nacitat relacne produkty k produktu tak ze chcem vybrat len tie relacne produkty ktore maju status 1, nevie niekto poradit?
// robim klasicky select jedneho produktu
$this->productEntity = $this->productRepository->find($productId);
// nasledne vyberiem z relatedproduct produkty ktore k nemu patria na zaklade FK
$relatedProductList = $this->productEntity->related('relatedproduct', 'product_id');
// tu by som mal mat where ktory zabezpeci aby som nacital len tie relacne produkty ktore maju status 1
$relatedProductList->where('product.status',1);
a v sablone prechadzam cez foreach a nasledne pomocou ref sa dostanem k produktom ktore su relacne k mojmu produktu
<li n:foreach="$relatedProductList as $relatedProduct">
{var $product = $relatedProduct->ref('product','relatedProductId')}
{$product->name}
</li>
CREATE TABLE IF NOT EXISTS `product` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` text CHARACTER SET utf8 NOT NULL,
`status` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`),
);
CREATE TABLE IF NOT EXISTS `relatedproduct` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`product_id` int(10) unsigned NOT NULL,
`relatedProductId` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`),
KEY `product_id` (`product_id`),
KEY `relatedProduct_id` (`relatedProductId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=69 ;
ALTER TABLE `relatedproduct`
ADD CONSTRAINT `relatedproduct_ibfk_3` FOREIGN KEY (`product_id`) REFERENCES `product` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `relatedproduct_ibfk_4` FOREIGN KEY (`relatedProductId`) REFERENCES `product` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
Editoval duskohu (23. 7. 2013 10:03)
- duskohu
- Člen | 778
@hrach bol to moj preklep, Teraz to je sice bez erroru, ale vracia mi to aj tie relacne produkty ktore maju status 0. takze to nemam dobre.
Edit: da mi to tento select:
SELECT `relatedproduct`.`id`, `relatedproduct`.`product_id`, `relatedproduct`.`relatedProductId`
FROM `relatedproduct`
INNER JOIN `product` ON `relatedproduct`.`product_id` = `product`.`id`
WHERE (`relatedproduct`.`product_id` IN (63)) AND (`product`.`status` = 1)
Editoval duskohu (16. 7. 2013 11:16)