10 my $cd = DBICTest::CD->retrieve(4);
11 my ($artist) = $cd->search_related('artist');
12 is($artist->name, 'Random Boy Band', 'has_a search_related ok');
14 # has_many test with an order_by clause defined
15 $artist = DBICTest::Artist->retrieve(1);
16 is( ($artist->search_related('cds'))[1]->title, 'Spoonful of bees', 'has_many search_related with order_by ok' );
18 # search_related with additional abstract query
19 my @cds = $artist->search_related('cds', { title => { like => '%of%' } } );
20 is( $cds[1]->title, 'Forkful of bees', 'search_related with abstract query ok' );
22 # creating a related object
23 $artist->create_related( 'cds', {
27 is( ($artist->search_related('cds'))[3]->title, 'Big Flop', 'create_related ok' );
30 is( $artist->count_related('cds'), 4, 'count_related ok' );
33 my $track = DBICTest::Track->create( {
37 title => 'Hidden Track'
39 $track->set_from_related( cd => $cd );
40 is( $track->cd, 4, 'set_from_related ok' );
42 # update_from_related, the same as set_from_related, but it calls update afterwards
43 $track = DBICTest::Track->create( {
47 title => 'Hidden Track'
49 $track->update_from_related( cd => $cd );
50 is( (DBICTest::Track->search( cd => 4, position => 99 ))[0]->cd, 4, 'update_from_related ok' );
52 # find_or_create_related with an existing record
53 $cd = $artist->find_or_create_related( 'cds', { title => 'Big Flop' } );
54 is( $cd->year, 2005, 'find_or_create_related on existing record ok' );
56 # find_or_create_related creating a new record
57 $cd = $artist->find_or_create_related( 'cds', {
58 title => 'Greatest Hits',
61 is( $cd->title, 'Greatest Hits', 'find_or_create_related new record ok' );
62 my @cds = $artist->search_related('cds');
63 is( ($artist->search_related('cds'))[4]->title, 'Greatest Hits', 'find_or_create_related new record search ok' );
66 skip 'Need to add delete_related', 1;
68 ($cd) = DBICTest::CD->search( title => 'Greatest Hits' );
69 $artist->delete_related( cds => $cd );
70 is( DBICTest::CD->search( title => 'Greatest Hits' ), undef, 'delete_related ok' );
73 # try to add a bogus relationship using the wrong cols
75 $artist->add_relationship(
76 tracks => 'DBICTest::Track',
77 { 'foreign.cd' => 'self.cdid' }
80 like($@, qr/Unknown column/, 'failed when creating a rel with invalid key, ok');
82 # another bogus relationship using no join condition
84 $artist->add_relationship( tracks => 'DBICTest::Track' );
86 like($@, qr/join condition/, 'failed when creating a rel without join condition, ok');