Welcome Guest, Not a member yet? Register   Sign In
[Deprecated] DMZ 1.6.2 (DataMapper OverZealous Edition)

[eluser]OverZealous[/eluser]
@GregX999
First off, that error means you either have misconfigured your relationships] or have [url=http://www.overzealous.com/dmz/pages/troubleshooting.html#General.Plural.Controller]a naming issue. Also, please see the top of the Troubleshooting page, and make sure you check each numbered item.

Second, that query doesn't make sense. The query you ran would, if it didn't out-right error, always return 0 or 1, because you are querying the number of Products with an ID of $product_id. Also, you should never need to specify a table name in DMZ. That's usually a sign you are doing it wrong. ;-)

So, what are you trying to query?

E.g.:
Number of categories for a given product:
Code:
$count = $category->where_related('product', 'id', $product_id)->count();

Edit:
I forgot to include the cleaner alternative if you already have the $Product:
Code:
$count = $product->category->count();

[eluser]GregX999[/eluser]
Yeah, I guess maybe I'm not approaching what I want to be doing correctly.

I have a category (as an object) and I have a product_id (id field from product class) and I just want to do an "if" to see if the product is in that category (or if the category is assigned to the product - same thing from the "other direction").

Code:
if (category contains product) { do stuff }

[eluser]OverZealous[/eluser]
@GregX999

OH! Why didn't you say so. Then your query was basically correct:
Code:
$exists = ($category->product('id', $product_id)->count() > 0);

You just didn't need to specify the table name.

The other error, however, is not related to the query problem.

[eluser]GregX999[/eluser]
I originally didn't use the table name ("product.id") and I got the error, that's why I tried w/ it. I changed it back (to just "id") and I still get that same error.

Fatal error: Class name must be a valid object or a string in /.../.../.../application/libraries/datamapper.php on line 1730

Is a singular/plural issue causing the problem?

The product model is "Product".
In the category model, I have:
Code:
var $has_many = array('product');
(singular "product")

In the error, I'm unsure which "class name" it's referring to.

[eluser]OverZealous[/eluser]
@GregX999
Please see the message I wrote before, and check the troubleshooting page. I gave you several starting points there already.

[eluser]GregX999[/eluser]
Sorry about that - I lost track of that link as we flipped to page 46...

Anyway, I figured it out thanks to that troubleshooting page. I had the relation from the product back to the category setup wrong. So I fixed that, then just had to change my query to:

Code:
$cat->product->where('product_id', $product_id)->count();

That works, but it seems a bit messy directly accessing the join table (both relationships are has_many). If I used ('id', $product_id) I get an error saying the join table doesn't have an "id" field.

Thanks for the help!
Greg

[eluser]OverZealous[/eluser]
@GregX999
I know where the problem is. The Count method does some sneaky stuff to actually count the results, and doesn't necessarily include the full table.

I think I might add a is_related_to method in the future, so the underlying infrastructure is hidden.

It'll probably be in 1.7.1! :-)

[eluser]OverZealous[/eluser]
OK, the function is already written for the next release. Just needs documentation... :cheese:

[eluser]GregX999[/eluser]
Jeeze, you're fast!

[eluser]OverZealous[/eluser]
My secrets:
1) In-house email server (I get forum posts instantaneously via IMAP).

2) Never test what I write. :-P (Just kidding. Everything has some testing. How ever, I do rely on the users of DMZ to do the bulk of bug finding for me.)

3) I eat my own dogfood: I use DMZ in my own code, so features I would to use myself end up in there very quickly. ;-)




Theme © iAndrew 2016 - Forum software by © MyBB