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

[eluser]Mirage[/eluser]
On the include_join_fields() method:

Wouldn't it be possible to keep the name of the join field (without the 'join_' prefix) if there is no ambiguity with a field from the relation? Seems to me that the relation table is queried once anyway to get it's column names. So the query builder could thus actually be smart about adding the 'join_' prefix.

Alternatively, perhaps let the prefix be passed as a parameter and allow that to be '' ?

Thanks,
-m
#42

[eluser]OverZealous[/eluser]
@Mirage

I really don't think it is worth the time and extra code for that. A join field is specifically one that does not exist on either object in the relationship, but on the relationship itself. It needs to have something that distinguishes it from the object it is attached to.

You might be able to put something together using a Get Rule that copies a join field into a normal field for your application, however.
#43

[eluser]BrianDHall[/eluser]
OK, here's my issue and how I think I'll handle it:

User can have many cart_items, and each cart_item can have many listings. I want to get all listings related to all cart items, and tried this:

Code:
$user->cart_item->get();
$user->cart_item->listing->get()

Now this works if I wanted to get all listings related to one cart_item, but I want all listings for all cart items.

The complication here is I can't simply ask for all listings related to one user, as there are listings related to a given user that are not cart_items.

Now what I intend to do to fix this is get the cart items related to a user, and then use a foreach on $user->cart_item->all, and then inside that loop I'll get the related listings for a given cart_item, and then I'll add references to those listings into an array I'll build up so that when the loop is done I have all the listings I'm interested in collected in one place.

So that should work, but it just seems I've either made things overly hard on myself with design (this application wasn't originally going to have a shopping cart, just is getting tacked on in the last few days), or I'm missing a whizbang feature of DMZ and don't know it.
#44

[eluser]cube1893[/eluser]
Hi guys,

i have a method which adds a new "course" to my database. A course has a start date and an end date. The end date can be NULL. Both fields are defined as DATE in MySQL, end_date has the 'is_null' option checked.

Everything works fine, but if the user doesn't enter an end_date, the system writes an empty DATE string(0000-00-00) into the row. What do I have to change in order to get the NULL into the column?

Code:
function add()
{
        $c = new Course();
        $related = $c->from_array($_POST, array(
            'course_type',
            'start_date',
            'end_date',
            'season'
        ));

        if($c->save($related))
        {
            $this->index();
        }
        else
        {
            echo $c->error->string;            
        }

}

I appriciate any solutions!
#45

[eluser]Mirage[/eluser]
I'd say you need to check the end_date before you use the from_array method. If the 'end_date' field is empty, set it to NULL instead of the empty string or remove the key altogether.

If setting to NULL works you could probably also add a rule (empty_to_null) in the course model that does this for you transparently.
#46

[eluser]OverZealous[/eluser]
@BrianDHall

If all you want is the items - not the carts they are related to - you can do this:

Code:
$listings = new Listing();
$listings->where_related('cart_item/user', $user)->get();

This gets all listings related through a cart item to a user. This assumes that there is only one user per cart_item, and one cart_item per listing. If you have multiple in either case, then you need to add a distinct into your query.

If you also want the (singly-related) cart_item, you can do that in one query as well, like so:
Code:
$listings = new Listing();
$listings->include_related('cart_item', '*', TRUE, TRUE);
$listings->where_related('cart_item/user', $user)->get();

foreach($listings as $listing) {
    echo $listing->cart_item->id;
}

Both functions are detailed on Get (Advanced).
#47

[eluser]OverZealous[/eluser]
@cube1893

Even simpler than Mirage's suggestion, I think you have the default value for the column set to 0. You should check the table settings (in MySQL) and make sure the default value is NULL.
#48

[eluser]cube1893[/eluser]
@Mirage
Thanks for your solution, it works!

@OverZealous
Thanks, but the default value is set to NULL. If I insert the data via phpmyadmin, it works. It's a pity, because I would be nice if DMZ would convert empty input fields to NULL, if this ist set in the table settings.
#49

[eluser]Mirage[/eluser]
Something else I'm grappling with on my many-to-many relations.

As you know I've worked up this magnificent MPTT extension. On simple relations all that's needed to use it is a lft and rgt column in the table. I'm trying to find a way to make this extension work on a join table.

Since join tables don't create their own objects, it's not clear to me if or how this is possible at all.

I could see creating an independent model that works against the join table perhaps. But it's unclear to me how I would reference objects in the relation.

Basically the task is to keep items in the join table in a fixed order.

Ideas?
-m
#50

[eluser]OverZealous[/eluser]
[quote author="cube1893" date="1260577009"]It's a pity, because I would be nice if DMZ would convert empty input fields to NULL, if this ist set in the table settings.[/quote]

There's no way for DMZ to know what the default value is on a column. So, it sets the column to whatever you ask it to. (And '' is not the same as NULL, since NULL is a special data construct.)


@Mirage
By definition, a join table is not a model or an object. It's just the link between two objects. That's why you can't create an object out of it.

I don't see any way to implement the MPTT structure via the internal relationship code. I'm not even 100% sure what the benefit would be.




Theme © iAndrew 2016 - Forum software by © MyBB