CodeIgniter Forums
DataMapper ORM v1.8.0 - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22)
+--- Thread: DataMapper ORM v1.8.0 (/showthread.php?tid=37531)



DataMapper ORM v1.8.0 - El Forum - 05-22-2011

[eluser]WanWizard[/eluser]
I would do something like
Code:
class Category extends DataMapper {
    public $has_one = array(
        'parent_category' => array(
            'class' => 'category',
            'other_field' => 'category',
        ),
    );
    public $has_many = array(
        'category' => array(
            'other_field' => 'parent_category',
        )
    );
}

Note, this would require a foreign key called "parent_category_id".


DataMapper ORM v1.8.0 - El Forum - 05-23-2011

[eluser]Spir[/eluser]
[quote author="WanWizard" date="1306106069"]I would do something like
Code:
class Category extends DataMapper {
    public $has_one = array(
        'parent_category' => array(
            'class' => 'category',
            'other_field' => 'category',
        ),
    );
    public $has_many = array(
        'category' => array(
            'other_field' => 'parent_category',
        )
    );
}

Note, this would require a foreign key called "parent_category_id".[/quote]
So you mean this :
Code:
CREATE  TABLE IF NOT EXISTS `categories` (
  `id` INT NOT NULL ,
  `name` VARCHAR(45) NULL ,
  `parent_category_id` INT NULL ,
  PRIMARY KEY (`id`) );


Instead of what I previously said :
Code:
CREATE  TABLE IF NOT EXISTS `categories` (
  `id` INT NOT NULL ,
  `name` VARCHAR(45) NULL ,
  `id_parent` INT NULL ,
  PRIMARY KEY (`id`) );



DataMapper ORM v1.8.0 - El Forum - 05-23-2011

[eluser]WanWizard[/eluser]
From the top of my head: yes. I'm not in a position to test it at the moment... (and don't forget the AUTOINCREMENT in the 'id' column).


DataMapper ORM v1.8.0 - El Forum - 05-23-2011

[eluser]Spir[/eluser]
Great I'll try that. Many thanks for your time.


DataMapper ORM v1.8.0 - El Forum - 05-23-2011

[eluser]Julio Fagundes[/eluser]
My cache (simplecache) is not being created.

Here my config:

config/datamapper.php
Code:
$config['production_cache'] = 'datamapper/cache'; // THIS CACHE IS OK.
$config['extensions_path'] = 'datamapper';
$config['extensions'] = array('simplecache'); // THIS NOT


controller/clientes.php
Code:
$r = new Cliente();
        $r->include_related('pessoa_fisica');
        $r->include_related('transportadora');
        $r->include_related('metodo_pagamento');
        $r->include_related('vendedor');
        $r->include_related('empresa_menutencao');
        $r->get_cached();

In output profiler, i see this large query every time.

*the folder is writable

Is there something wrong? or missing?


DataMapper ORM v1.8.0 - El Forum - 05-24-2011

[eluser]WanWizard[/eluser]
Simplecache is, as the name implies, is an extremely simple cache extension.

So simple, that it isn't more than basically a wrapper around CI's database caching methods, $this->db->cache_on() / $this->db->cache_off() to be exact.

This also means you need to configure CI properly to get this to work. As the manual states: the production_cache setting has nothing to do with query caching.


DataMapper ORM v1.8.0 - El Forum - 05-24-2011

[eluser]Julio Fagundes[/eluser]
Yes, I know the production cache is a particular cache to datamapper.

My question was if there is something wrong, because no file is created.

Thanks to reply, do you know what is the best way to do cache with datamapper?


DataMapper ORM v1.8.0 - El Forum - 05-24-2011

[eluser]WanWizard[/eluser]
As I said, you need to configure CI database caching, Datamapper just uses that. See http://ellislab.com/codeigniter/user-guide/database/caching.html


DataMapper ORM v1.8.0 - El Forum - 05-24-2011

[eluser]Julio Fagundes[/eluser]
Solved.

config/database.php
Code:
$db['default']['cachedir'] = ''; // needs to be a absolute path



DataMapper ORM v1.8.0 - El Forum - 05-26-2011

[eluser]Arministrator[/eluser]
I need some pointers on accessing relations. I did study the docs and examples, and I have a solution that kind of works, but I feel I overcomplicated and would really like to keep the code smart as possible. This is my first use of DataMapper.

these are the tables and relations i got (it's a blood bank app):

donators
donations
bloodypes
bloodtypes_donators
bloodtypes_donations
donations_donators

Donor model $has_one bloodtype, $has_many donation
Donation model $has_many donor, bloodtype
Blootype model $has_many donor, donation

I think my relations are ok, but check those too, please. I want to be able to read all the donations (without donors), all donations by bloodtype, all donations with all donors, and all donations by donor id.

So here's what I came up with, mostly following the example in DataMappers bugs app.

This instatiates a donator and gets all his donations (only created field), and gets the count of all donations related to that user. At least it's supposed to.


Code:
$donator = new Donator();
        $donator->include_related('donation', 'created');
        $donation = $donator->donation;
        $donation
            ->select_func('COUNT', '*', 'count')
            ->where_related('donator', 'id', '${parent}.id');
        $donator->select_subquery($donation, 'donation_count');
        $donator->get_iterated();
        
        // Pass $data
        $data['donator'] = $donator;

Thanks a lot.