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

[eluser]j0nxiest[/eluser]
I didn't quite get from the documentation how to work with related items with the from_array method. I tried to do like this:

The form:
Code:
<?php
$usergroup = new Usergroup();
$usergroup->where('group_name','user')->get();
?>
<label for="username">Username: &lt;input type="text" name="username" id="username" class="text" value="&lt;?=set_value('username', $user-&gt;username); ?&gt;" /></label><br/>
&lt;input type="hidden" name="usergroup" value="&lt;?=set_value('usergroup', $usergroup-&gt;id)?&gt;" />

The handler:
Code:
$user = new User();
$user->from_array($_POST);
$user->save();

But it complaints that the usergroup relation is not set, what am i doing wrong? The usergroup var is POSTed correctly.

[eluser]Spir[/eluser]
[quote author="j0nxiest" date="1275317111"]I didn't quite get from the documentation how to work with related items with the from_array method. I tried to do like this:

The form:
Code:
&lt;?php
$usergroup = new Usergroup();
$usergroup->where('group_name','user')->get();
?&gt;
<label for="username">Username: &lt;input type="text" name="username" id="username" class="text" value="&lt;?=set_value('username', $user-&gt;username); ?&gt;" /></label><br/>
&lt;input type="hidden" name="usergroup" value="&lt;?=set_value('usergroup', $usergroup-&gt;id)?&gt;" />

The handler:
Code:
$user = new User();
$user->from_array($_POST);
$user->save();

But it complaints that the usergroup relation is not set, what am i doing wrong? The usergroup var is POSTed correctly.[/quote]
I'm not sure since I never used this and I discover this functionnality with your post but I think your solution is in the second parameter of the from_array function → from_array($data, $fields, $save)

The field usergroup should be called usergroup_id?
Why adding this hidden value if you won't update it?
http://www.overzealous.com/dmz/pages/ext...array.html

Also you could have specified TRUE then no need to call save().

[eluser]Wazzu[/eluser]
<b>Count, group and field</b>
Hi all. I have a doubt trying to make a simple query with dmz:

I want to make this query, so I get a 2 colums result, with the age and the number of users with that age: "SELECT age, count(age) FROM users GROUP BY age"

But I don't know how to use count() correctly with group.
This won't work:

$u = new User();
$u->select('age');
$u->group_by('age');
$u->count();

How should I make this? Thanks in advance.

[eluser]OverZealous[/eluser]
@Wazzu
Count is not the same as SQL count. It's a special function that returns the number of results directly.

I think to get what you want, you'll need to use select_func():
Code:
...
$u->select_func('COUNT', '@age', 'number');
...
$u->get();
echo $u->age;
echo $u->number;

Even if there was a function, it would be called select_count. Please see the manual for the purpose of count.

Update: Corrected typo - the method is select_func, not select_funct.

[eluser]Wazzu[/eluser]
[quote author="OverZealous" date="1275335363"]@Wazzu
Count is not the same as SQL count. It's a special function that returns the number of results directly.

I think to get what you want, you'll need to use select_func():
Code:
...
$u->select_funct('COUNT', '@age', 'number');
...
$u->get();
echo $u->age;
echo $u->number;

Even if there was a function, it would be called select_count. Please see the manual for the purpose of count.[/quote]
Thanks, Phil, I'll give a try. I didn't know 'select_funct' method (I still haven't found it in the user guide)

The 'other' way I found was using 2 selects:
Code:
$u->select('age');
$u->select('count(age) as `numrows`');
$u->group_by('age');
$u->get();

Thanks very much for your answer and your sample

[eluser]OverZealous[/eluser]
@Wazzu

Ooops! That's supposed to be select_func - no 'T'!!

It's under SQL Functions in the manual.

[eluser]Benedikt[/eluser]
I have a small problem and I can't think of what I am doing wrong (there must be something though):

Code:
$u = new User();
$u->get_by_id( 1 );

$h = new House();
$h->number = 9;
$h->street = "Lonely Road";

$h->save( $u );

So far so good. If I now create a second house it will UPDATE the relationship between House and User instead if INSERT a new one.

What am I doing wrong? Any idea?

[eluser]OverZealous[/eluser]
@Benedikt
The second time you save the House, it's already been saved, and therefore has an ID. As far as DMZ is concerned, if you change the values, and save it again, it's updating an existing object.

Therefore, you have two options:

• Either create a new House each time, or
• Use the clear method to clear the values and prepare the house object to be saved as a new value.

The difference is minor. In theory, creating a new object should take more resources than clear it, but unless you are doing a large loop, the actual performance difference is probably close to zero. I think it reads better to use new, because it is clearer what you are doing.

[eluser]Benedikt[/eluser]
Well, there is not a loop. By "create another one" I mean u fill out a form and click "Create". Then a house is created.

When I fill out the form again and create a new one it will create a new house but it will not add another relationship-entry. It will just update the existing relationship:

"UPDATE jn_house_user SET user_id = 1 WHERE user_id = 1" or similar.

I tried "$h->save_as_new( $u )" but it wont create a relationship, it will just create a new House-Object.

[eluser]j0nxiest[/eluser]
Hi,

I'm having a problem with the transactions, can't get them to work. I'm trying to do this:
Code:
$user = new User();
$user->username = $this->input->post('username');
$user->trans_begin();
$user->save();
var_dump($user->trans_status()); // This is NULL, even though the validations fail
if($user->trans_status() === false){
    $user->trans_rollback();
}else {
    echo 'everything ok';
    $user->trans_commit();
}




Theme © iAndrew 2016 - Forum software by © MyBB