红豆php按关系查找

发布于 2024-12-07 20:23:52 字数 516 浏览 0 评论 0原文

这似乎是一个简单的问题,但我却感到困惑。

如果我使用以下内容创建几个 bean:

list($product1, $product2, $product3) = R::dispense('product', 3);
...

以及使用以下内容创建几个类别:

list($cat1, $cat2) = R::dispense('category', 2);
...

然后将它们链接到:

$product1->sharedCategory[] = $cat1;
$product3->sharedCategory[] = $cat1;

然后如何查询与 $cat1 相关的所有产品?我应该拿回产品 1 和 3。

就像我说的,很简单。在 mysql 中这是一个显而易见的事情,所以总是可以只向红豆发送一个 sql 字符串,但是肯定有一种方法可以做到这一点。

谢谢。

This seems like a simple question, but I'm somehow baffled.

If I create a couple of beans with:

list($product1, $product2, $product3) = R::dispense('product', 3);
...

and a couple of categories with:

list($cat1, $cat2) = R::dispense('category', 2);
...

then link them with:

$product1->sharedCategory[] = $cat1;
$product3->sharedCategory[] = $cat1;

how do I then query all products that are related to $cat1? I should get back product 1 and 3.

Like I said, simple. In mysql this is a no-brainer, so could always just send red bean an sql string, but there must be a way of doing this built it, surely.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人心善变 2024-12-14 20:23:52

我认为情况正好相反(如果我说得不对,抱歉),因为从逻辑上讲,一个类别有很多产品,而不是您定义的一个产品有很多类别。

========编辑部分====================

我删除了这里发布的一个小源代码,因为手册 手册
超级简单,更好,并且非常有帮助,提供有关如何连接、关联、更新等的示例代码。我相信您会从中受益匪浅。如果本手册有任何困难,很乐意提供帮助。

==========附加信息=======================

这里我只是放了一些源代码,没有改变我的答案中的任何内容。试试这个代码,它可以按照你想要的方式工作!

<?PHP
echo '<pre>';

require('rb.php'); 
$toolbox = R::setup('mysql:host=localhost;dbname=my_ORM','root','');
$farm = R::dispense('building');

//create the product list
list($product1,$product2,$product3) = R::dispense('product',3);
//add attributes 
$product1->name='prod1';
$product2->name='prod2';
$product3->name='prod3';


//create a list of categories 
list($category1,$category2) = R::dispense('category',2);
//add attributes 
$category1->name='categ1';
$category2->name='categ2';

//the connect beans together
R::associate($category1,$product1);
R::associate($category1,$product2);
R::associate($category1,$product3);
R::associate($category2,$product3);

//then store
R::store($product1);
R::store($product2);
R::store($product3);
R::store($category1);
R::store($category2); 

//get id for category 1
$categId=R::getCell( " select  `id` from `category` where `name`='categ1'  ");
//get products for category 1
$results = 
R::getAll( "
SELECT  `product`.`id`,`product`.`name` 
FROM `product`  left JOIN `category_product`
on `category_product`.`product_id`= `product`.`id` 
where  `category_id`='".$categId."' ");
//display
print_r($results);

//get categories for product3
$prodId=R::getCell( " select  `id` from `product` where `name`='prod3'  ");
$results = 
R::getAll( "
SELECT  `category`.`id`,`category`.`name` 
FROM `category`  left JOIN `category_product`
on `category_product`.`category_id`= `category`.`id` 
where  `product_id`='".$prodId."' ");
print_r($results);

?>

I think it goes the other way around (sorry if I am not right) because logically a category has many products not a product many categories as you have defined it.

========Edited part====================

I deleted a small source posted here because the manual Manuall
is super easy and much better and very helpful with sample code on how to connect, make association, updates etc etc. I am sure you will benefit a lot from it. If there is any difficulty with the manual glad to help.

========== Additional Information =======================

Here I just put some source code, did not change anything in my answear. Try this code, it works both ways as you want it!

<?PHP
echo '<pre>';

require('rb.php'); 
$toolbox = R::setup('mysql:host=localhost;dbname=my_ORM','root','');
$farm = R::dispense('building');

//create the product list
list($product1,$product2,$product3) = R::dispense('product',3);
//add attributes 
$product1->name='prod1';
$product2->name='prod2';
$product3->name='prod3';


//create a list of categories 
list($category1,$category2) = R::dispense('category',2);
//add attributes 
$category1->name='categ1';
$category2->name='categ2';

//the connect beans together
R::associate($category1,$product1);
R::associate($category1,$product2);
R::associate($category1,$product3);
R::associate($category2,$product3);

//then store
R::store($product1);
R::store($product2);
R::store($product3);
R::store($category1);
R::store($category2); 

//get id for category 1
$categId=R::getCell( " select  `id` from `category` where `name`='categ1'  ");
//get products for category 1
$results = 
R::getAll( "
SELECT  `product`.`id`,`product`.`name` 
FROM `product`  left JOIN `category_product`
on `category_product`.`product_id`= `product`.`id` 
where  `category_id`='".$categId."' ");
//display
print_r($results);

//get categories for product3
$prodId=R::getCell( " select  `id` from `product` where `name`='prod3'  ");
$results = 
R::getAll( "
SELECT  `category`.`id`,`category`.`name` 
FROM `category`  left JOIN `category_product`
on `category_product`.`category_id`= `category`.`id` 
where  `product_id`='".$prodId."' ");
print_r($results);

?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文