1
votes

I have Kohana ORM/Mysql query problem. I hope you can help.

To start of, here's the diagram of tables:

Diagram

Here's my ORM definitions:

MEMBERS has many TOPICS through MEMBERS_TOPICS ARTICLES has many TOPICS though ARTICLES_TOPICS TOPICS has many MEMBERS though MEMBERS_TOPICS TOPICS has many ARTICLES though ARTICLES_TOPICS

Think of it as a mailing list where you have members that has chosen topics of articles in which topics are also assigned to particular topics.

I couldn't figure out how to make a single query so I can return joined results and send an email to individual members with articles they only chose through the topics they've chosen.

I hope to receive wisdom with mysql/kohana ninjas around. :D

2

2 Answers

1
votes

There is a with() method in ORM that will join your tables, provided you have relations setup correctly in your models. Below a small example:

Model:

public function find_all_orders()
{
   return $this->with('customer')->with('product')->find_all();
}

View:

foreach ($orders as $order)
{
   echo $order->product->name . ' ' . $order->customer->name;
}

The relations are:

  • order belongs to product, product has many orders
  • order belongs to customer, customer has many orders
0
votes

ORM does have a join() method: http://kohanaframework.org/3.2/guide/api/ORM#join

There's possibly a more elegant KO3 way to do what you want, but I'm not entirely familiar with KO3 ORM, perhaps someone else can suggest a better way.

[edit] Some further reading: