Using model::find('count' throws a notice on empty result set #834

Closed
Speedrockracer opened this Issue Feb 24, 2013 · 9 comments

Comments

Projects
None yet
3 participants

See title

Model::find('count', array(
'conditions'=>array(...)
));

Notice: Undefined offset: 0 in /var/www/framework-master/libraries/lithium/data/source/Database.php on line 616

Contributor

jails commented Feb 24, 2013

Weird, works for me, are you using the last master ?

Owner

nateabele commented Feb 24, 2013

Yeah, we're gonna need more data...

list($record) = $this->read($query, $options)->data(); on line 616 returns null.

I am using a Postgres database maybe this has something to do with that? There is no problem when the table has rows.

Grtz

Contributor

jails commented Feb 24, 2013

Sorry, still works for me on:

  • last dev branch
  • psql (PostgreSQL) 9.1.8
  • PHP 5.4.11-1~precise+1
  • Ubuntu 12.04

It's weird becase $this->read($query, $options) is a RecordSet. If it wasn't ->data() should have fail. I don't understand how this can happen.

Any chance having the full context of this bug ?

Heya,

$this->read($query, $options)->data(); returns and empty array here. I will debug some tomorrow.

Software:
latest dev branch
psql 9.2.3
php 5.4
ubuntu 12.10

It happens with any table/model/query when the resultset is empty.
Need more infos?

Greetz

Contributor

jails commented Feb 24, 2013

Should contain array ('count' => 0), Maybe the SQL failed ?
Can you provide the generated SQL in your case ?

Ah I found the problem, I was using a group by clause in the query and the query realy didn't return any value.
The group by didn't behave as I was expecting at all. Sooo that would make it my bad -.-'

I can make the correct query with a DISTINCT clause in the count but a quick google tells me that it's not yet supported. Any ideas on that?

Exuses for posting without adequate testing!

Contributor

jails commented Feb 24, 2013

No worries.
Btw, I'm not sure to understand what you mean about the unsupported 'GROUP BY/DISTINCT' feature.
Indeed Model::find('count') doesn't support the GROUP BY clause out of the box, but if you have something like Galleries has many Images a simple:

$galleries = Galleries::find('all', array(
    'fields' => array('count("Images"."id") as count'),
    'group' => '"Galleries"."id"',
    'with' => array('Images')
));

should give you the number of images in each galleries with PosgreSQL..

Contributor

jails commented Apr 13, 2013

Seems the issue has diverged. Feel free to open a new issue with the new problem if this still an issue.

@jails jails closed this Apr 13, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment