[Jifty-commit] r3908 - in Jifty-DBI/trunk: . lib/Jifty/DBI
jifty-commit at lists.jifty.org
jifty-commit at lists.jifty.org
Thu Aug 16 10:02:54 EDT 2007
Author: sterling
Date: Thu Aug 16 10:02:48 2007
New Revision: 3908
Modified:
Jifty-DBI/trunk/ (props changed)
Jifty-DBI/trunk/lib/Jifty/DBI/Record.pm
Jifty-DBI/trunk/lib/Jifty/DBI/Schema.pm
Jifty-DBI/trunk/t/11schema_records.t
Log:
r8684 at dynpc145: andrew | 2007-08-16 08:55:32 -0500
Added support for virtual record columns.
Modified: Jifty-DBI/trunk/lib/Jifty/DBI/Record.pm
==============================================================================
--- Jifty-DBI/trunk/lib/Jifty/DBI/Record.pm (original)
+++ Jifty-DBI/trunk/lib/Jifty/DBI/Record.pm Thu Aug 16 10:02:48 2007
@@ -259,11 +259,20 @@
if ( $column->readable ) {
if ( UNIVERSAL::isa( $column->refers_to, "Jifty::DBI::Record" ) )
{
- $subref = sub {
- if ( @_ > 1 ) { Carp::carp "Value passed to column accessor. You probably want to use the mutator." }
- $_[0]->_to_record( $column_name,
- $_[0]->__value($column_name) );
- };
+ if ($column->virtual) {
+ $subref = sub {
+ if ( @_ > 1 ) { Carp::carp "Value passed to column accessor. You probably want to use the mutator." }
+ $_[0]->_to_record( $column_name,
+ $_[0]->id );
+ };
+ }
+ else {
+ $subref = sub {
+ if ( @_ > 1 ) { Carp::carp "Value passed to column accessor. You probably want to use the mutator." }
+ $_[0]->_to_record( $column_name,
+ $_[0]->__value($column_name) );
+ };
+ }
} elsif (
UNIVERSAL::isa(
$column->refers_to, "Jifty::DBI::Collection"
Modified: Jifty-DBI/trunk/lib/Jifty/DBI/Schema.pm
==============================================================================
--- Jifty-DBI/trunk/lib/Jifty/DBI/Schema.pm (original)
+++ Jifty-DBI/trunk/lib/Jifty/DBI/Schema.pm Thu Aug 16 10:02:48 2007
@@ -346,6 +346,9 @@
# A one-to-one or one-to-many relationship is requested
if ( UNIVERSAL::isa( $refclass, 'Jifty::DBI::Record' ) ) {
+ # Make this a virtual record reference if they set by
+ $column->virtual(1) if defined $column->by and $column->by ne 'id';
+
# Handle *_id reference columns specially
if ( $name =~ /(.*)_id$/ ) {
my $aliased_as = $1;
Modified: Jifty-DBI/trunk/t/11schema_records.t
==============================================================================
--- Jifty-DBI/trunk/t/11schema_records.t (original)
+++ Jifty-DBI/trunk/t/11schema_records.t Thu Aug 16 10:02:48 2007
@@ -9,7 +9,7 @@
BEGIN { require "t/utils.pl" }
our (@available_drivers);
-use constant TESTS_PER_DRIVER => 67;
+use constant TESTS_PER_DRIVER => 74;
my $total = scalar(@available_drivers) * TESTS_PER_DRIVER;
plan tests => $total;
@@ -193,8 +193,20 @@
is($ph->phone, '7890');
is($phone_collection->next, undef);
}
+
+ is($emp->favorite_color->id, undef, 'emp 1 has no favorite color yet');
+ is($emp2->favorite_color->id, undef, 'emp 2 has no favorite color yet');
-
+ my $color = TestApp::Color->new( handle => $handle );
+ my $c_id = $color->create( employee => $emp, color => 'magenta' );
+ ok($c_id, "Got an id for the new color: $c_id");
+ $color->load($c_id);
+ is($color->id, $c_id);
+ is($emp->favorite_color->id, $c_id, 'emp fave id matches color id');
+ $emp->favorite_color->set_color('cyan');
+ is($emp->favorite_color->color, 'cyan', 'changed color to cyan');
+ $color->load($c_id);
+ is($color->color, 'cyan', 'changed the original too');
cleanup_schema( 'TestApp', $handle );
disconnect_handle( $handle );
@@ -216,8 +228,14 @@
id integer primary key,
employee integer NOT NULL,
phone varchar(18)
-) }
-]
+)
+}, q{
+CREATE table colors (
+ id integer primary key,
+ employee integer NOT NULL,
+ color varchar(8)
+)
+} ]
}
sub schema_mysql {
@@ -232,6 +250,12 @@
employee integer NOT NULL,
phone varchar(18)
)
+}, q{
+CREATE TEMPORARY table colors (
+ id integer AUTO_INCREMENT primary key,
+ employee integer NOT NULL,
+ color varchar(8)
+)
} ]
}
@@ -247,7 +271,13 @@
employee integer references employees(id),
phone varchar
)
-} ]
+}, q{
+CREATE TEMPORARY table colors (
+ id serial PRIMARY KEY,
+ employee integer references employees(id),
+ color varchar
+)
+}]
}
package TestApp::PhoneCollection;
@@ -259,6 +289,9 @@
return $tab;
}
+package TestApp::Color;
+use base qw/Jifty::DBI::Record/;
+
package TestApp::Employee;
use base qw/Jifty::DBI::Record/;
@@ -267,6 +300,7 @@
use Jifty::DBI::Record schema {
column name => type is 'varchar';
column phones => references TestApp::PhoneCollection by 'employee';
+ column favorite_color => references TestApp::Color by 'employee';
}
}
@@ -287,5 +321,19 @@
}
}
+package TestApp::Color;
+
+BEGIN {
+ use Jifty::DBI::Schema;
+ use Jifty::DBI::Record schema{
+ column employee => references TestApp::Employee;
+ column color =>
+ type is 'varchar',
+ valid_values are qw/
+ white cyan magenta
+ yellow green red black
+ /; # don't like CGA? too bad
+ }
+}
1;
More information about the Jifty-commit
mailing list