Problem with using count method

138 views
Skip to first unread message

Miloš Brecher

unread,
Sep 12, 2015, 6:53:09 PM9/12/15
to NotORM
I try to use count in the same way as example on the page http://www.notorm.com/ - just on the bottom:

foreach ($db->application() as $application) {
    // get count of each application's tags
   echo $application->application_tag()->count("*") . "\n";


I have tried this code exactly according to the example:

foreach($DB->run() as $run)
echo $run->semester()->count('*');

and I have got a fatal error:


Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'run_id' in 'field list'' in W:\tanecniastra\libs\notorm\NotORM\Result.php:161 Stack trace: #0 W:\tanecniastra\libs\notorm\NotORM\Result.php(161): PDOStatement->execute(Array) #1 W:\tanecniastra\libs\notorm\NotORM\MultiResult.php(93): NotORM_Result->query('SELECT COUNT(*)...', Array) #2 W:\tanecniastra\libs\notorm\NotORM\Result.php(595): NotORM_MultiResult->aggregation('COUNT(*)') #3 W:\tanecniastra\libs\notorm\NotORM\MultiResult.php(105): NotORM_Result->count('*') #4 W:\tanecniastra\templates\semesterMenu.php(13): NotORM_MultiResult->count('*') #5 W:\tanecniastra\templates\@layout.php(70) : eval()'d code(4): get_semesterMenu(Object(NotORM)) #6 W:\tanecniastra\templates\@layout.php(70): eval() #7 W:\tanecniastra\libs\global\boot.php(60): include('W:\\tanecniastra...') #8 W:\tanecniastra\index.php(50): include('W:\\tanecniastra...') #9 W:\_localhost\modul.php(57): include('W:\\tanecniastra...') #10 W:\index.p inW:\tanecniastra\libs\notorm\NotORM\Result.php on line 161

But this code works:

foreach($DB->run() as $run)
echo $run->semester->count('*');

The difference is in  $run->semester->count('*') x  $run->semester()->count('*')

Where is the problem ? Is the example code on http://www.notorm.com wrong or do I have something in a bad way ?

Thanks for any comment.





Allysson David

unread,
Sep 12, 2015, 7:04:11 PM9/12/15
to not...@googlegroups.com
The answer lies in your tables' layout, unless they changed something and I'm not aware.
Want to share your tables/columns/relationships and what you wanted to do?

PS: Doesn't echo $run->semester->count('*'); always return either 0 or 1?

Miloš Brecher

unread,
Sep 13, 2015, 7:30:15 AM9/13/15
to NotORM
I enclose structure of the database tables printed in Adminer:




Dne neděle 13. září 2015 0:53:09 UTC+2 Miloš Brecher napsal(a):

Allysson David

unread,
Sep 13, 2015, 7:59:55 AM9/13/15
to not...@googlegroups.com
There we go. First, when you download NotORM you'll get the tests directory, inside it there will be software.sql file with the examples' tables.

The examples are working with this layout:

Table: tag
Columns: id, name

Table: application
Columns: id, author_id, maintainer_id, title, web, slogan

Table: application_tag
Columns: application_id, tag_id

This mean that the relationship between application and tag is n:n.

When you see this code:
foreach ($software->application() as $application) {
$count = $application->application_tag()->count("*");

It means that, for each row of the table application, find all rows from application_tag referencing that row of application and count.
The SQL is:
SELECT * FROM $application_tag WHERE application_id = $application['id']

Meanwhile your tables' relationship is 1:n. Each run row should have only 1 semester row, but each semester row could have n run rows. If you wish to reproduce the examples behavior, you'd need to execute this code instead:
foreach($DB->semester() as $semester)
echo $semester->run()->count('*');

Running the code above should count the number of run rows referencing each semester row.
Trying to run that other code would only work if you had run_id on your semester table.

I hope this was helpful.

Allysson David

unread,
Sep 13, 2015, 8:01:23 AM9/13/15
to not...@googlegroups.com
PS: Fixing the SQL I mentioned earlier:
SELECT * FROM application_tag WHERE application_id = $application['id']

Miloš Brecher

unread,
Sep 13, 2015, 5:44:24 PM9/13/15
to NotORM
David thanks very much!

Dne neděle 13. září 2015 14:01:23 UTC+2 Allysson David napsal(a):
Reply all
Reply to author
Forward
0 new messages