How to use getReferencedTable()

Notice: This thread is very old.
dfx413
Member | 9
+
0
-

Hello folks!
I came across a problem with \Nette\Database which is probably caused by my misunderstanding of how to use it properly.

I've got table client_action_product which points at tables client_action and product using foreign keys named product_id and client_action_id. I am trying to access the product table using this query:

foreach ($this->getActions()->where('cancelled IS NULL') as $action) {
	if ($action->related('client_action_product')->getReferencedTable('product','product_id')->where('type LIKE ?','%whatever%')->count() > 0) {
		return true;
	}

There is no problem getting to the client_action_product table but I am not able to get past the getReferencedTable call. It does not return anything and I don't know why.

I tried to change the column parameter of that method to id but the result is the same. I expected to have Selection which I could further work with but I still get nothing.

Getting a single row from product using

$action->related('client_action_product')->fetch()->ref('product','product_id')->toArray()

works without problems but this way I would have to iterate over the related products using another foreach and check the condition everytime which looks to me as less elegant and slower (not that the speed matters much in this particular case).

If anyone could give me some advice I would be thankful.

Last edited by dfx413 (2012-04-08 01:00)

dfx413
Member | 9
+
0
-

Nobody…?

hrach
Member | 1834
+
0
-

Well, at first, getReferencedTable is internal, you should not use it.

From your piece of code I don't exactly understand what you want to do. So? After that we can for sure find the best Nette\Database solution.

petr.pavel
Member | 533
+
0
-

I haven't been using NDB for some time (switched to NotORM) but here are my five cents:

  • Is the final SQL query what you'd expect? You can see by switching on the debug panel. If the SQL query is wrong, please send both what you want it to be and what it is now.
  • In NotORM (and I suspect in NDB too) you can get joined fields into the SELECT clause by using dots and colons. E.g. $db->table('client_action')->select('client_action_product:product.name'); Colons are for 1:many, dots for many:1. This probably isn't what you'd want to use in your loop but anyway. You might want to use it as a workaround.