Welcome Guest, Not a member yet? Register   Sign In
DMZ 1.7.1 (DataMapper OverZealous Edition)

[eluser]tdktank59[/eluser]
Wow!

See what happens when I take a bit of a break from my personal development and just focus on my day job...
Now I am completely lost in what I is new Smile

From what I can tell (mind you I loved DMZ in the first place) I will love this version even more Smile

Now to go read the docs and start wrapping my head around this new version.

You have a beer fund that we can donate to? You have done great work and continue to.
So from at least me, but pretty sure many many others Thanks for the great work!

[eluser]rideearthtom[/eluser]
Having some problems with subqueries here.

I can't find anything wrong with this code:
Code:
$accepted_documents = new Document();
$accepted_documents->select('id')
    ->where_related('sme_profile', 'id', $this->sme_profile->id);
$documents = new Document();
$documents->where_not_in_subquery('id', $accepted_documents)
    ->get();
But it generates an SQL error:
Quote:Unknown column 'tdb_documents_subquery.id' in 'field list'

Am I missing something? I can't see any table alias 'tdb_documents_subquery' being specified in the subquery...

[eluser]rideearthtom[/eluser]
I've done a temporary fix by filling an array with the offending sub-query's results and adding where_not_in if count() > 0.

But it seems strange that other subquery types work OK.

Here's the full code, with the working subqueries, and with the broken one replaced with the fix above. The above post was a simplification of the problematic subquery. Maybe this is a DMZ problem, I don't know.

Code:
private function has_documents_to_accept()
    {
        $accepted_documents = new Document();
        $accepted_documents->where_related('sme_profile', 'id', $this->sme_profile->id)
            ->get();
        $ids_1 = array();
        foreach ($accepted_documents as $item)
        {
            $ids_1[] = $item->id;
        }
        $products_purchased = new Product();
        $products_purchased->select('id')
            ->where_related('purchase', 'sme_profile_id', $this->sme_profile->id);
        $join_projects_purchased = new Join_documents_locations_project();
        $join_projects_purchased->select('id')
            ->where_related('project/purchase', 'sme_profile_id', $this->sme_profile->id)
            ->where('location_id', $this->sme_profile->location_id);
        $documents = new Document();
        $documents->where_related_subquery('product', 'id', $products_purchased)
            ->or_where_related_subquery('join_documents_locations_project', 'project_id', $join_projects_purchased);
        if (count($ids_1) > 0)
        {
            $documents->where_not_in('id', $ids_1);
        }
        return $documents->count() > 0 ? TRUE : FALSE;
    }

[eluser]modano[/eluser]
Hi guys,

How do I go about removing something on an update, for example:
I create a new "user" and set first name and last name.
Later I change my mind and want to remove the last name, I only want the first name available in the database.
So I display the current first name and last name on a form in a view, and clear the text field "last name" and hit the submit button.


As DMZ will not validate empty fields on updates, but instead treating it as keep the value currently in the database/object, how do I in above example CLEAR the last name in the database?

regards,
modano

[eluser]bEz[/eluser]
Why not run a routine that will simply assign "NULL" (on-demand) for a particular {USER} without using a text field.

Code:
$someObject = new aModel($this->input->post('user_id');
if ( $someObject->exists() ) {
  $someObject->last_name = NULL;
  $someObject->save();
  .
  .   // log/notify results of save routine
  .

} else {
. . .  // log/notify bad user id
}

You may also need to remove validation ("required") from the last name field, or turn-off validation for this particular routine assignment...

[eluser]TheJim[/eluser]
@rideearthtom

I'm not sure of the exact source of your first error (though I suspect it's #1 below), but there are three things to get out of the way before digging too deeply:

1. You don't seem to be doing anything that actually requires sub-queries. Just use where_related, or_where_related, not_where_in, etc. for what you're doing.

2. In your code:
Code:
$documents->where_related_subquery('product', 'id', $products_purchased)
            ->or_where_related_subquery('join_documents_locations_project', 'project_id', $join_projects_purchased);

you're using $products_purchased and $join_projects_purchased in a scalar context. When you did:

Code:
$accepted_documents->where_related('sme_profile', 'id', $this->sme_profile->id)
            ->get();

you correctly used the id field from sme_profile, but in the first block, you're just using the objects themselves, which won't work. You did create an array of IDs for the $accepted_documents part of your code, so you seem to understand this in at least certain circumstances, but just realize that you can't just pass DMZ objects directly to the where functions.

3. Why query IDs and then pass them to another query when there are relationships to use directly? Why not just:

Code:
$documents = new Document();
        $documents->group_start() // Groups because of the "OR"
            ->where_related('product', 'id', $products_purchased) // Fix this and don't pass the object -- also notice not a sub-query
            ->or_where_related('join_documents_locations_project', 'project_id', $join_projects_purchased) // Fix this and don't pass the object -- also notice not a sub-query
            ->group_end()
            ->where_not_related('sme_profile', 'id', $this->sme_profile->id) // This should do the same thing as your $accepted_documents -> array of IDS -> call to where_not_in

That should take care of your $accepted_documents code. Looks like you could also combine the $products_purchased as a deep relationship query. Possibly the $join_projects_purchased part as well, although that looks like it might be better separated.

Regardless, there's a lot of room for simplification in your code.

[eluser]Liu Guoqing[/eluser]
O ! It's usefull!!

[eluser]rideearthtom[/eluser]
[quote author="TheJim" date="1271382257"]@rideearthtom

I'm not sure of the exact source of your first error (though I suspect it's #1 below), but there are three things to get out of the way before digging too deeply:
[/quote]

@TheJim

Thanks for your reply:

1. This was the first time I had found a need for subqueries. In particular, I needed an extra where() in the join_documents_locations_project query, which I could not find a way to replicate with joins (at least using DMZ - with raw SQL it would be pretty easy). Maybe there is functionality I'm unaware of.

2. This code does in fact work. DMZ's user guide contains examples in the same format - selecting a single column, then using it as a subquery. So there are no problems with that code.

3. I'd forgotten the grouping functions - thanks for pointing this out. where_not_related() does not exist! But I think you mean where_related('sme_profile', 'id !=', ...), which does work. We got there between us Wink

Code:
private function has_documents_to_accept()
    {
//        $accepted_documents = new Document();
//        $accepted_documents->where_related('sme_profile', 'id', $this->sme_profile->id)
//            ->get();
//        $ids_1 = array();
//        foreach ($accepted_documents as $item)
//        {
//            $ids_1[] = $item->id;
//        }
//        $products_purchased = new Product();
//        $products_purchased->select('id')
//            ->where_related('purchase', 'sme_profile_id', $this->sme_profile->id);
        $join_projects_purchased = new Join_documents_locations_project();
        $join_projects_purchased->select('id')
            ->where_related('project/purchase', 'sme_profile_id', $this->sme_profile->id)
            ->where('location_id', $this->sme_profile->location_id);
        $documents = new Document();
        $documents->group_start()
//            ->where_related_subquery('product', 'id', $products_purchased)
            ->where_related('product/purchase', 'sme_profile_id', $this->sme_profile->id)
            ->or_where_related_subquery('join_documents_locations_project', 'project_id', $join_projects_purchased)
            ->group_end()
            ->where_related('sme_profile', 'id !=', $this->sme_profile->id);
//        if (count($ids_1) > 0)
//        {
//            $documents->where_not_in('id', $ids_1);
//        }
        return $documents->count() > 0 ? TRUE : FALSE;
    }

As far as I can see, the original problem I posted about where_not_in_related_subquery() not working properly still stands.

[eluser]TheJim[/eluser]
Well, that's what I get for writing a rushed reply.

[eluser]rideearthtom[/eluser]
[quote author="TheJim" date="1271409310"]Well, that's what I get for writing a rushed reply.[/quote]
Don't get the impression I'm not grateful - thanks to your (rushed?) reply I cut 4 queries to 2 and removed half the lines of code from the function! Just wanted to clarify the points above and include corrections for the benefit of other people who may find it useful.

Smile




Theme © iAndrew 2016 - Forum software by © MyBB