CodeIgniter Forums
DMZ 1.7.1 (DataMapper OverZealous Edition) - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22)
+--- Thread: DMZ 1.7.1 (DataMapper OverZealous Edition) (/showthread.php?tid=28550)



DMZ 1.7.1 (DataMapper OverZealous Edition) - El Forum - 06-28-2010

[eluser]lexusgs430[/eluser]
cool, will do, thanks for the help

[quote author="OverZealous" date="1277768463"]@lexusgs430
First, you are using the method wrong. Read the docs again. The first parameter is the list of IDs to exclude. The second parameter is the column to use DISTINCT on.

Before submitting a question, you should look at the queries being generated. There's some good tips for debugging in the troubleshooting guide, at the top. They walk you through some basic debugging steps. You probably would have easily seen that the query being generated was not what you expected.[/quote]


DMZ 1.7.1 (DataMapper OverZealous Edition) - El Forum - 06-29-2010

[eluser]Tom Vogt[/eluser]
I'm trying to use count(*) and group_by together and can't figure out how to do that using DMZ.

In SQL, what I want to do is roughly this:
Code:
SELECT count(*), some_field, AVG(other_field) FROM table WHERE group_id = 1 GROUP BY some_field;

now I came as far as
Code:
$this->table->group_by('some_field')->select('some_field')->select_avg('other_field')->get();
which gives me everything but the count. Then I thought doing

Code:
foreach ($this->table as $x) {
   $count = $x->count();
}

would work, but it gives me the total numbe of results, not the individual count for each row. Someone got a better idea for the DMZ equivalent of the SQL I posted above?


DMZ 1.7.1 (DataMapper OverZealous Edition) - El Forum - 06-30-2010

[eluser]OverZealous[/eluser]
@Tom Vogt
CodeIgniter's ActiveRecord (and therefore DMZ) is missing select_count(). So there's no super-easy way to do this. However, there is a fairly simple way:
Code:
$object
    ->select_func('count', '*', 'count')
    ->select('some_field')
    ->select_avg('other_field')
    ->group_by('some_field')
    ->get();

This should get you what you want, although I haven't tested it. I have it on the list to add select_count to DMZ.


DMZ 1.7.1 (DataMapper OverZealous Edition) - El Forum - 06-30-2010

[eluser]Tom Vogt[/eluser]
Does exactly what I want, fantastic. I read about select_func() but didn't realize I could use it for count(*). Thanks a lot!


DMZ 1.7.1 (DataMapper OverZealous Edition) - El Forum - 06-30-2010

[eluser]anveo[/eluser]
Is there a way I can access the list of returned objects via an index number? i.e.

Code:
$users = new User();
$users->get();

$first = $users[0];

I see DataMapper implements IteratorAggregate, and defines the 'key' function, but I can't seem to get it working. I am subclassing DataMapper as DataMapperExt, and calling the parent __construct, but I see DataMapper does both __construct and DataMapper() constructor. Might that have something to do with it?


DMZ 1.7.1 (DataMapper OverZealous Edition) - El Forum - 06-30-2010

[eluser]jpi[/eluser]
I am not sure, but maybe try :
Code:
$users = new User();
$users->get();

$first = $users->all[0];



DMZ 1.7.1 (DataMapper OverZealous Edition) - El Forum - 06-30-2010

[eluser]OverZealous[/eluser]
@anveo
jpi is correct. When I decided that the iterator method was going to be the default way to access results, I removed a lot of the references to the ->all array in the docs.

I need to put them back in on the basic get page, so that they are easier to find.

Basically, a non-get_iterated get() sets the results on the ->all array. The iterator aggregate simply looks at this array. If you use get_iterated, however, some magic is done to prevent the objects from being configured unless they are needed.

(Thanks, jpi, for providing support!)


DMZ 1.7.1 (DataMapper OverZealous Edition) - El Forum - 06-30-2010

[eluser]anveo[/eluser]
[quote author="jpi" date="1277945655"]I am not sure, but maybe try :
Code:
$users = new User();
$users->get();

$first = $users->all[0];
[/quote]

Thanks! Works great.


DMZ 1.7.1 (DataMapper OverZealous Edition) - El Forum - 06-30-2010

[eluser]NachoF[/eluser]
[quote author="OverZealous" date="1274603997"]@NachoF

I don't know what to tell you. If you set the field to an empty string, then DMZ has to assume you want to save it. An empty string is not a NULL, and when inserting new data, DMZ only excludes NULLs from the query.

The best solution is to not set fields you want to be NULL to an empty string.
[/quote]

But how exactly do I do that??.. there is a bunch of text fields in my form.. and the user should be able to write on whichever he chooses to... Basically, those fields should be numeric but NOT required.

I would like to keep my controller action as clean as possible (which is why im using your array extension and In only doing)
Code:
$object->from_array($_POST);
if($object->save())
{
redirect ("home");
}

could chainging the data type in the database to character varying but keeping the numeric validation rule in datamapper be a good approach?


DMZ 1.7.1 (DataMapper OverZealous Edition) - El Forum - 06-30-2010

[eluser]OverZealous[/eluser]
@NachoF
DMZ provides all of the tools - but you still have to make the application. Like I said, there's no way for DMZ to know the difference between "empty string that is supposed to be NULL" and "empty string that is supposed to be an empty string".

If you want to accept values that need to be modified, use validation rules. That's what they are there for.

In my case, for different reasons, I added a null_on_empty rule to my base class. It looks something like this:
Code:
function _null_on_empty($field) {
    if(empty($this->{$field})) {
        $this->{$field} = NULL;
    }
}

You can also make that an extension.