[Dbix-class] Inherited search method,
current_source_alias and add_to_ for many_to_many.
Dmitry Latin
dim0xff at gmail.com
Thu Apr 4 07:10:38 GMT 2013
Hello!
A little overview.
I have 1 table (users), and 3 result classes: AbstractUser, User and Admin.
Table has column role (which defines who user is - Admin or User). And I use
inflate_result in AbstractUser result class to "transform" it to Admin or User.
I want to call $schema->resultset('Admin')->search->all to retrieve all admins from
table 'users' (...WHERE role = 'ADMIN'). And the same for users (to retrieve users).
So I created ResultSet class for AbstractUser. Also ResultSets for User and Admin
(which are inherited from ResultSet::AbstractUser) are created too.
In ResultSet::AbstractUser I overwrite search method and added accessor role.
Here in search method I add $self->current_source_alias .'.role' = $self->role to
search condition. And then call next::method.
In ResultSet::Admin and ResultSet::User I overwrite search method to set role
accessor only and calling next::method.
It seems that search works. But I got "user.role" condition twice.:
SELECT user.id, user.name, user.role FROM admin_users me JOIN users user ON
user.id = me.user_id WHERE ( ( user.role = ? AND ( user.role = ? AND me.admin_id
= ? ) ) ): 'USER', 'USER', '1'
The next.
When I try $admin->add_to_user({...}), I got the error. So the error is that
current_source_alias 'me' and current_source_alias is 'user' at the same time:
$admin->add_to_users( { name => 'User #1', role => 'USER', } );
=====>
SELECT user.id, user.name, user.role FROM users user WHERE ( ( ( user.name = ?
AND user.role = ? ) AND me.role = ? ) ): 'User #1', 'USER', 'USER'
Where am I wrong?
Or is there easier method to do the same?
Thank you!
PS: example is here http://kino.dim0xff.com/dbx/db_test.tar.gz
--
//wbr, Dmitry L.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/dbix-class/attachments/20130404/2be62a81/attachment-0001.htm
More information about the DBIx-Class
mailing list