CodeIgniter Forums
DMZ 1.7.1 (DataMapper OverZealous Edition) - 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: DMZ 1.7.1 (DataMapper OverZealous Edition) (/showthread.php?tid=28550)



DMZ 1.7.1 (DataMapper OverZealous Edition) - El Forum - 01-01-2011

[eluser]WanWizard[/eluser]
Your reading all results into memory. Since every record is an object, this can consume lots of memory if you have a lot of records in the result set.

In general:
- if you need the results to iterate over them (like with a foreach), use get_iterated() instead of get() (see the manual).
- if you don't need all these individual results, rethink your query. Lots of rows in a result is an indication of a sub-optimal query.


DMZ 1.7.1 (DataMapper OverZealous Edition) - El Forum - 01-02-2011

[eluser]Guido Mallee[/eluser]
Strange thing is that there aren't many records in the result set. Anyway, seems like the problem occured because of the way i passed a DM object to a subview. I did:

Code:
<?php foreach ($page->block as $block): ?>
<?php $this->load->view('block', array($block)); ?>
<?php endforeach; ?>

I solved it by doing:

Code:
<?php foreach ($page->block as $block): ?>
<?php $this->load->view('block', $block); ?>
<?php endforeach; ?>

Seems to be working fine now.

I'm in doubt about something else though. Is there a more efficient way to write the following piece of code?

Code:
$data['page']->block->get();
foreach ($data['page']->block as $block)
{
    $block->block_type->get();
    $block->block_text->get();
}



DMZ 1.7.1 (DataMapper OverZealous Edition) - El Forum - 01-02-2011

[eluser]Guido Mallee[/eluser]
Another thing i'm about in doubt, is how to deal with extra attributes in a joining table. For example:

I have the table PAGES and the table BLOCKS. These tables have a many to many relationship. Now in the joining table BLOCKS_PAGES i have a attribute 'position' which defines the position of a block in a page.

How to easily get all blocks of a certain page, ordered by position, and how to add a block to a page defining the position?

Thanks!


DMZ 1.7.1 (DataMapper OverZealous Edition) - El Forum - 01-03-2011

[eluser]WanWizard[/eluser]
See the manual: http://datamapper.exitecms.org/pages/joinfields.html


DMZ 1.7.1 (DataMapper OverZealous Edition) - El Forum - 01-03-2011

[eluser]Guido Mallee[/eluser]
Thanks! I must have missed this article in the manual. Sorry for the inconvenience.

Any thoughts on the piece of code in my other post? It's like this: I have the tables A, B and C. A has many B. B has one C. How to load the complete resultset in an object? Do i have to loop over the resultset B, to get C for each result, as in my example? Or can it be done easier?

I tried to set auto_populate_has_many and auto_populate_has_one to TRUE in the DM config file. Though, this makes A automatically get all B's, but doesn't make B get C.

I couldn't find in the manual. Please forgive me if it's in there!


DMZ 1.7.1 (DataMapper OverZealous Edition) - El Forum - 01-03-2011

[eluser]WanWizard[/eluser]
The short answer is, you can't on a many-to-many. Since every record is an object, the number of objects in memory would explode.

Many to one's can be 'pulled' into the result using include_related(). You can use the $instantiate parameter to have it included as an object instead of adding the fields to the current object. Theoretically include_related could deal with has_many (there an ticket open on that), but you'll soon run into the aformentioned issue.


DMZ 1.7.1 (DataMapper OverZealous Edition) - El Forum - 01-03-2011

[eluser]Guido Mallee[/eluser]
I understood. Though, the way I do it in my example code is just as memory intensive as it would be if 'the easy way' would have been possible, isn't it?

Since the relation of my table BLOCKS to my table BLOCK_TEXTS actually is one-to-'one or zero' (one-to-many but never more than 1 result) I tried to use include_related. As you mentioned though, this didn't work since the relationship theoratically still is one-to-many. I couldn't find the ticket about using include_related with has_many (hate the search functionality on this forums, or is it just me?). Could you pass me the link, please? I think I'll give that a try since, like I said, the result will be only one row maximum, and therefore, one object maximum. No problem, right?

I finished porting one of my websites with CMS to DataMapper and I'm really enthusiastic about it! I think DataMapper is going to save me a lot of work in the future. Many thanks, WanWizard, for all your dedicated work on this great ORM, and of course, for your great support. One (hopefully) last question, though:

Remember I asked you about the joining field? I have a joining field 'position' in the joining table between the tables PAGES and BLOCKS. I managed to easily get all blocks from a page, ordered by position. To save a new block though, I had to get the max position of a block on a specific page first, which turned out to be quite hard. I ended up doing:

Code:
$page = new Page($page_id);
$page->block->include_join_fields()->get();
$position = 1;
foreach ($page->block as $b)
{
    if ($b->join_position > $position)
        $position = $b->join_position;
}
$position++;
            
$block->save($page);
$block->set_join_field($page, 'position', $position);

Where I'd much rather would like to do something like:
Code:
$page = new Page($page_id);
$page->block->select_max('position');
$page->block->include_join_fields()->get();
$position = $page->block->join_position + 1;
$block->save($page);
$block->set_join_field($page, 'position', $position)

This didn't work though. It's not a disaster to do it like in the first piece of code, although, getting the max position used to be one line of code for me before. Is there a way to refactor this code?

Thanks in advance and I'll try hard to leave you in peace after this one.


DMZ 1.7.1 (DataMapper OverZealous Edition) - El Forum - 01-04-2011

[eluser]Dennis Rasmussen[/eluser]
Love the library and been reading the docs several times now.

I'm having a "small" issue with the naming conventions and I'm wondering why noone else seems to have encountered this problem.

Basically, what do you guys do if you want a controller with the singular name of a table name?
An example would be /user/view/1 with a table name of users.
That would then require the model name to be the same as the controller name giving you an error - unless you write a custom model name and specify a table within the model. But if you do this then how do you setup relations? For the custom model name or?
Code:
$has_many = array('user_model');

I'm a bit lost on what to do here because I ended up having so many errors because of my custom model names to avoid class name collisions.

With Yorick's ORM you don't have this issue as he requires model suffixes (https://github.com/YorickPeterse/Awesome-Record), so am I missing something with DataMapper?


Second question:
Let's say I have a forum where you want to iterate through categories then forums within. How do you iterate like that with an ORM? Get the cateogory and foreach - then get the forums in the category and foreach?


DMZ 1.7.1 (DataMapper OverZealous Edition) - El Forum - 01-04-2011

[eluser]WanWizard[/eluser]
If the relation is one-to-one-or-zero, why define it as has_many? Include_related only works on has_one.

You can find Datamapper's issue tracker on http://bitbucket.org/wanwizard/datamapper/issues.

As for the join_field, I understand the issue. If you just want the highest position, you could try order_by('position', 'DESC')->limit(1) to get only that record.


DMZ 1.7.1 (DataMapper OverZealous Edition) - El Forum - 01-04-2011

[eluser]WanWizard[/eluser]
@Dennis,

If you want to deviate from the naming conventions, you can manually define the table name in the model. As long as you don't have any relationships, that will work.

If you do have relationships, you have to switch from simple definition (i.e. using just the model name) to the advanced form (using an array to define the relation). With the advanced form you are free to define the model name, and the column names that are used to define the relationship. See http://datamapper.exitecms.org/pages/advancedrelations.html for more information.

Maybe I should add a section to the manual about this specific issue.