Documentation blues
[prev]
[thread]
[next]
[Date index for 2005/06/21]
G'day everyone again,
I'm having some trouble making Class::DBI work the same way as the documentation
says it should.
The documentation says (regarding columns)
'Essential' are the minimal set of columns needed to load and use the
object. ... It will automatically be set to 'All' if you don't set
it yourself. ...
I have:
package Staff::Details;
use strict;
use base 'Staff::DBI';
# Declare the name of our table
__PACKAGE__->table( 'Staff' );
# Declare our primary key field(s)
__PACKAGE__->columns(Primary => 'StaffID');
# Declare our columns
__PACKAGE__->columns(All => qw/StaffID FirstName LastName Address City
State Position Wage/);
Yet when I write
print "essential columns: ", join (" ", Staff::Details->_essential), "\n";
I get:
essential columns: staffid
Which seems to match what I'm getting out of the database prior to accessing any
other field name. The documentation (as quoted above) says that since I don't
set the Essential columns, I should get everything.
-------------------
The documentation also says:
If you wish to have fields that act like columns in every other way, but
that don't actually exist in the database (and thus will not persist),
you can declare them as part of a column group of 'TEMP'.
.... (earlier )
Music::Artist->set_sql(most_cds => qq{
SELECT artist.id, COUNT(cd.id) AS cds
FROM artist, cd
WHERE artist.id = cd.artist
GROUP BY artist.id
ORDER BY cds DESC
LIMIT 10
});
my @artists = Music::Artist->search_most_cds();
If you also need to access the 'cds' value returned from this query, the
best approach is to declare 'cds' to be a TEMP column.
My class above continues with:
# Create a transient column on for each object
__PACKAGE__->columns(TEMP => qw/numprojects/);
# Declare our relationships to the other tables/classes
__PACKAGE__->has_many(
projects => 'Staff::Projects',
{ order_by => 'allocation' }
);
# Create a search_by_num_projects which returns staff and number of
# projects
__PACKAGE__->set_sql(by_num_projects => "
SELECT s.StaffID, count(ProjectName) as numprojects
FROM Staff s, Projects p
WHERE p.StaffID = s.StaffID
GROUP BY s.StaffID
ORDER BY numprojects DESC
");
Careful examination of my query above and the query in the documentation should
show that I'm trying to do exactly the same kind of thing. Yet even though I've
declared my column (numprojects) with TEMP I can't seem to get my number of
projects out.
my @staff = Staff::Details->search_by_num_projects();
print Dumper \@staff;
yields
$VAR1 = [
bless( {
'staffid' => '12345'
}, 'Staff::Details' ),
bless( {
'staffid' => '12346'
}, 'Staff::Details' ),
bless( {
'staffid' => '12351'
}, 'Staff::Details' ),
bless( {
'staffid' => '12347'
}, 'Staff::Details' ),
bless( {
'staffid' => '12350'
}, 'Staff::Details' )
];
and
my $staff = Staff::Details->search_by_num_projects()->first();
print Dumper $staff;
yields
$VAR1 = \bless( {
'staffid' => '12345'
}, 'Staff::Details' );
Furthermore:
my $staff = Staff::Details->search_by_num_projects()->first();
$staff->firstname();
print Dumper $staff;
yields
$VAR1 = \bless( {
'state' => 'Vic',
'lastname' => 'Sprat',
'city' => 'Melbourne',
'staffid' => '12345',
'position' => 'Devel',
'address' => '2 Pine Rd',
'firstname' => 'Jack',
'wage' => '80'
}, 'Staff::Details' );
which shows that my TEMP column just isn't coming out.
The same query in the database yields:
mysql> SELECT s.StaffID, count(ProjectName) as NumProjects
-> FROM Staff s, Projects p
-> WHERE p.StaffID = s.StaffID
-> GROUP BY s.StaffID
-> ORDER BY NumProjects DESC
-> ;
+---------+-------------+
| StaffID | NumProjects |
+---------+-------------+
| 12345 | 2 |
| 12346 | 2 |
| 12351 | 2 |
| 12347 | 1 |
| 12350 | 1 |
+---------+-------------+
5 rows in set (0.00 sec)
Have I made an error or have I stumbled upon a problem in the code or
documentation? If it is a problem in the documentation I'm happy to write a
patch if you let me know the correct behaviour. If it is an error in the code
I'm happy to try to provide test cases and have a go at patching it as well.
I'm using the standard Class::DBI 0.96 from CPAN.
All the best,
Jacinta
--
("`-''-/").___..--''"`-._ | Jacinta Richardson |
`6_ 6 ) `-. ( ).`-.__.`) | Perl Training Australia |
(_Y_.)' ._ ) `._ `. ``-..-' | +61 3 9354 6001 |
_..`--'_..-_/ /--'_.' ,' | contact@xxxxxxxxxxxx.xxx.xx |
(il),-'' (li),' ((!.-' | www.perltraining.com.au |
|
Documentation blues
Jacinta Richardson 12:14 on 21 Jun 2005
|