Re: $obj->delete doesn't remove from the DB

[prev] [thread] [next] [Date index for 2004/06/18]

From: Yuval Kogman
Subject: Re: $obj->delete doesn't remove from the DB
Date: 17:14 on 18 Jun 2004
--KuLpqunXa7jZSBt+
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Fri, Jun 18, 2004 at 12:00:18 -0400, William McKee wrote:
> On Fri, Jun 18, 2004 at 05:48:08PM +0300, Yuval Kogman wrote:

> Hi Yuval,
>=20
> It's hard to suggest a better way if you don't show us the way you are
> using.

Okay. It's long, and attached at the bottom.

> I suspect you have CDBI setup incorrectly which is often my problem.
> Have you looked at the ComplexManyToMany example in the cookbook
> section on the Wiki[1]? That may help you better understand how to do
> m2m relationships using CDBI.

I don't see anything in your example that has to do with my problem,
even now that I read it carefully instead of skimming like before.

Along with my "solution", I attached the setup that uses it, so that you
may get a better Idea of what i'm doing [wrong].

Here goes...

The code I use creates closures like the add_to foo ones are inserted to
the class in question's symbol table. It currently does not take into
account the "regular" has_many relationships, but I'm not worried about
that yet. You call it as

$object->remove_from_second_objects($a_second_object);

This is what I hacked into CDBI::Relationship::HasMany.pm:

sub _method_remove_from {
        my $self     =3D shift;
        my $accessor =3D $self->accessor;
        return sub {
                my ($self, $data) =3D @_;
                my $class =3D ref $self
                        or return $self->_croak("add_to_$accessor called as=
 class method");
                ref $data
                        or return $self->_croak("add_to_$accessor not calle=
d on objects");

                my $meta =3D (ref $self)->meta_info(has_many =3D> $accessor=
);
                my $f_class =3D $meta->foreign_class;
                my $self_f_key =3D $meta->args->{foreign_key};
                warn(map { $_->[0] } values %{ $f_class->__hasa_rels });

                $meta =3D $f_class->__hasa_rels; # CDBIR::HasA should maint=
ain meta_info too.
                (my $data_f_key) =3D grep { $meta->{$_}[0] eq ref $data } k=
eys %{ $meta };
                my $link =3D $f_class->retrieve($data_f_key =3D> $data->id,=
 $self_f_key =3D> $self->id);
                warn(join " ", ref($link), $link->db, $link->machine);
                $link->delete;
				# link is CDBI::Deleted::object...
        };
}


The project has a gazillion classes, so I'm only displayign the relevant
ones. The schema is used under Maypole (it's been diced up too, but much
less cleanly at the moment). it's for a test suite scheduler. Some of
the data is the machines which are being tested, and what database apps
are installed on them:

__PACKAGE__->setup( "dbi:mysql:hrtest", "hrtest" ); # uses CDBI::Loader

HrTesting::MachineHasDb->has_a('db' =3D> 'HrTesting::Db');
HrTesting::MachineHasDb->has_a('machine' =3D> 'HrTesting::Machine');

HrTesting::Machine->has_many('dbs' =3D> [ 'HrTesting::MachineHasDb' =3D> 'd=
b' ]);

The SQL tables in question:

mysql> describe machine;
+-------------+------------------+------+-----+---------+----------------+
| Field       | Type             | Null | Key | Default | Extra          |
+-------------+------------------+------+-----+---------+----------------+
| id          | int(10) unsigned |      | PRI | NULL    | auto_increment |
| platform    | int(10) unsigned |      | MUL | 0       |                |
| hostname    | varchar(20)      | YES  | MUL | NULL    |                |
| incarnation | int(10) unsigned | YES  |     | NULL    |                |
| ram         | int(10) unsigned | YES  |     | NULL    |                |
| bogomips    | int(10) unsigned | YES  |     | NULL    |                |
+-------------+------------------+------+-----+---------+----------------+
6 rows in set (0.01 sec)

mysql> describe db;
+-------------+------------------+------+-----+---------+----------------+
| Field       | Type             | Null | Key | Default | Extra          |
+-------------+------------------+------+-----+---------+----------------+
| id          | int(10) unsigned |      | PRI | NULL    | auto_increment |
| name        | varchar(100)     | YES  | MUL | NULL    |                |
| version_num | int(10) unsigned | YES  |     | NULL    |                |
| version     | varchar(20)      | YES  |     | NULL    |                |
+-------------+------------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

mysql> describe machine_has_db;
+---------+------------------+------+-----+---------+-------+
| Field   | Type             | Null | Key | Default | Extra |
+---------+------------------+------+-----+---------+-------+
| machine | int(10) unsigned |      | PRI | 0       |       |
| db      | int(10) unsigned |      | PRI | 0       |       |
+---------+------------------+------+-----+---------+-------+
2 rows in set (0.00 sec)


--=20
 ()  Yuval Kogman <nothingmuch@xxxxxxxx.xxx> 0xEBD27418  perl hacker &
 /\  kung foo master: /me throws bonsai kittens: neeyah!!!!!!!!!!!!!!!!


--KuLpqunXa7jZSBt+
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (Darwin)

iD8DBQFA0yLxVCwRwOvSdBgRAvtMAJ9LtTA1xGd9yEgbGSbgeRyPAjbBDwCfWXjz
ZWI5SKTpP4lmsGYOHfo4w9I=
=YTp5
-----END PGP SIGNATURE-----

--KuLpqunXa7jZSBt+--

$obj->delete doesn't remove from the DB
Yuval Kogman 14:48 on 18 Jun 2004

Re: $obj->delete doesn't remove from the DB
William McKee 16:00 on 18 Jun 2004

Re: $obj->delete doesn't remove from the DB
Yuval Kogman 17:14 on 18 Jun 2004

Re: $obj->delete doesn't remove from the DB
Tony Bowden 16:41 on 18 Jun 2004

Re: $obj->delete doesn't remove from the DB
Yuval Kogman 17:19 on 18 Jun 2004

Re: $obj->delete doesn't remove from the DB
Yuval Kogman 20:59 on 19 Jun 2004

Re: $obj->delete doesn't remove from the DB
Yuval Kogman 21:18 on 19 Jun 2004

Re: $obj->delete doesn't remove from the DB
Yuval Kogman 21:32 on 19 Jun 2004

Re: $obj->delete doesn't remove from the DB
Tony Bowden 21:48 on 19 Jun 2004

Generated at 11:34 on 01 Dec 2004 by mariachi v0.52