[Dbix-class] DBIx::Class::Tree::NestedSet

linuxsupport lin.support at gmail.com
Sun Dec 19 10:35:09 GMT 2010


OK, I got it working, there was issue with quote.

Now I can get all the sub categories belongs to one category by following
 $schema->resultset('Cat')->search({name =3D> 'Cat1'})->next->descendants;

I have following relationship.

Cat.pm

__PACKAGE__->has_many("cat_user", "MegaKBM::Schema::Result::UserCat",{
"foreign.user_id" =3D> "self.id"},);
__PACKAGE__->many_to_many("c_users", "cat_user", "usercat");

UserCat.pm

__PACKAGE__->belongs_to("usercat","MegaKBM::Schema::Result::User",{ id =3D>
"user_id"},{ join_type =3D> "LEFT"} );
__PACKAGE__->belongs_to("cat","MegaKBM::Schema::Result::Cat",{ id =3D>
"user_id"},{ join_type =3D> "LEFT"} );

User.pm

__PACKAGE__->has_many("user_cat", "MegaKBM::Schema::Result::UserCat",{
"foreign.user_id" =3D> "self.id" });
__PACKAGE__->many_to_many(u_cat =3D> "user_cat" =3D> "cat");

How can I get all users belongs to one category.

If I use my $rs =3D $schema->resultset('Cat')->search({name =3D> 'Cat1'})->=
next;
$rs->c_users;
it works and give all users belongs to category Cat1.

If I use my $rs =3D $schema->resultset('Cat')->search({name =3D>
'Cat1'})->next->descendants;
$rs->c_users does not work because it has many sub categories.

Is it possible ? how?

Thanks

On Sat, Dec 18, 2010 at 7:57 PM, linuxsupport <lin.support at gmail.com> wrote:

> I have created the table as show in the example
> fields are id, parent_id, lft, rgt, level and name.
>
> Now I am adding the data as following.
>
> my $root =3D $schema->resultset('Cat')->create({name =3D> 'Cat'});
> $root->add_to_children({ name =3D> 'cat1'});
>
> if I call $root->children i get following error.
>
> Schema::Result::Cat::children(): DBI Exception: DBD::mysql::st execute
> failed: Table 'sysitm.cat me, cat parent' doesn't exist [for Statement
> "SELECT `me`.`id`, `me`.`parent_id`, `me`.`lft`, `me`.`rgt`, `me`.`level`,
> `me`.`name` FROM `cat me, cat parent` WHERE ( ( `parent`.`id` =3D ? AND (
> `me`.`parent_id` =3D ? AND me.lft > parent.lft AND me.rgt < parent.rgt AND
> me.level =3D parent.level + 1 ) ) ) ORDER BY `me`.`lft`" with ParamValues:
> 0=3D'3', 1=3D'0'] at ./mytest.pl line 31
>
> Basically, what I want is categories and their sub categories there can be
> multi level sub categories, see below.
>
> Cat1 -> subcat1->subcat2
> ------> subcat3 -> subcat4 -> subcat5
> Cat2 -> subcat1 -> subcat2 ->subcat3
> -------> subcat5 -> subcat6
> -------> subcat7
>
> Thanks
>
> On Sat, Dec 18, 2010 at 4:31 PM, David Schmidt <davewood at gmx.at> wrote:
>
>> On Sat, Dec 18, 2010 at 7:20 AM, linuxsupport <lin.support at gmail.com>
>> wrote:
>> > Hi,
>> >
>> > Can someone provide me a real example of Tree::NestedSet?
>> >
>> > Thanks
>> >
>> > _______________________________________________
>> > List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
>> > IRC: irc.perl.org#dbix-class
>> > SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
>> > Searchable Archive:
>> > http://www.grokbase.com/group/dbix-class@lists.scsys.co.uk
>> >
>>
>> Have you tried the example in the documentation? If yes and it failed
>> for you perhaps providing the error message would help.
>>
>> david
>>
>> _______________________________________________
>> List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
>> IRC: irc.perl.org#dbix-class
>> SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
>> Searchable Archive:
>> http://www.grokbase.com/group/dbix-class@lists.scsys.co.uk
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/dbix-class/attachments/20101219/348=
e4270/attachment.htm


More information about the DBIx-Class mailing list