Part of the EllisLab Network
   
2 of 80
2
[Deprecated] DMZ 1.5.3 (DataMapper OverZealous Edition)
Posted: 30 April 2009 07:56 PM   [ Ignore ]   [ # 11 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  848
Joined  10-08-2008

@NachoF
join_related:
Join fields from a related class.  In other words, I can include the name field of the model Group while querying my Users.  This allows you to get information from related models all in one query.  It’s really useful when querying data for a table, because you avoid running many queries to pull in all the data.

Join related includes the specified fields as <related_field>_<column_name>.  You specify which related model in the first parameter, and which columns using the second parameter.

include_join_fields:
This allows you to include fields that are stored on a join table.  In this case, you might want to track how many times a specific user has viewed a specific post, or something similar.  You related the user like normal, but you can include extra columns on the join table that you can query, view, and update using where_join_field, include_join_fields, and set_join_field respectively.

I had already create join_related when I added the join table methods, so the names are a little confusing.  I didn’t want to rename it, because it would break code (or require rewrites).

If I did rename it, I might rename it include_related_fields to make it consistent with the other one.  (I might even make the second parameter optional, although that is not very efficient.)

 Signature 

Phil DeJarnett
  OverZealous Creations, LLC
  ZESTYJOBS - Job Management Powered by CI/DMZ NOW LIVE

  DataMapper OverZealous Edition: Advanced ORM for CodeIgniter Discuss

Profile
 
 
Posted: 02 May 2009 11:40 AM   [ Ignore ]   [ # 12 ]  
Summer Student
Total Posts:  15
Joined  04-22-2009

Hi OverZealous,

I’m using DMZ now. Thanks for creating it!

Here is problem:

Category 1:
A

B

C


Category 2:
A

C

D


Category3:
C

D

B

I want to get the elements that appears in:
1) Both Category1 and Category3.
2) All categories.

How do I accomplish this?

Nadeem Qureshi

Profile
 
 
Posted: 02 May 2009 12:15 PM   [ Ignore ]   [ # 13 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  848
Joined  10-08-2008

You didn’t mention if items could appear in no category.  If they must appear in some category, then you can just do $elements->get();

The best way is to use where_in_related(), which is part of the core DataMapper.

$category1 = ...
$category2 = ...
$elements->where_in_related_category('id', array($category1->id, $category2->id));
$elements->distinct->get();

To get all items in all categories (assuming the above isn’t true), just do this:

$elements->where_related_category('id >', 0)->distinct()->get();

That should get all items where there is a related category.  Adding the distinct prevents DM from having to parse the same item multiple times.

 Signature 

Phil DeJarnett
  OverZealous Creations, LLC
  ZESTYJOBS - Job Management Powered by CI/DMZ NOW LIVE

  DataMapper OverZealous Edition: Advanced ORM for CodeIgniter Discuss

Profile
 
 
Posted: 02 May 2009 12:33 PM   [ Ignore ]   [ # 14 ]  
Summer Student
Total Posts:  15
Joined  04-22-2009

You’re awesome, Man! grin I’ll try that out!

What would I do if I wanted elements that had no category?

Nadeem

Profile
 
 
Posted: 02 May 2009 12:36 PM   [ Ignore ]   [ # 15 ]  
Summer Student
Total Posts:  15
Joined  04-22-2009

Also,

Is there difference between $element->save($category) and $category->save($element)?

Profile
 
 
Posted: 02 May 2009 06:22 PM   [ Ignore ]   [ # 16 ]  
Lab Assistant
RankRank
Total Posts:  178
Joined  04-02-2009
qureshi - 02 May 2009 12:36 PM

Also,

Is there difference between $element->save($category) and $category->save($element)?

I have been wondering about that one as well.

Profile
 
 
Posted: 02 May 2009 06:42 PM   [ Ignore ]   [ # 17 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  848
Joined  10-08-2008

There is no difference between $a->save($b) and $b->save($a).  They both require one query.

The only time you might choose one over the other is when saving a new item and related items at once, and then just for cleaner code.

As far as querying for elements with no category, that can be a little difficult. If you are dealing with $has_one relationships, then you do something like this:

$item->where('related_id', NULL)->get();

But if you are dealing with $has_many, that’s a little trickier.  I recommend, for efficiency, including a bit of hand-written code.  Something like this might work:

$element->where('(elements.id NOT IN (SELECT element_id FROM categories_elements))')->get();

That’s the traditional, SQL way of handling it.

Good Luck!

 Signature 

Phil DeJarnett
  OverZealous Creations, LLC
  ZESTYJOBS - Job Management Powered by CI/DMZ NOW LIVE

  DataMapper OverZealous Edition: Advanced ORM for CodeIgniter Discuss

Profile
 
 
Posted: 04 May 2009 03:00 AM   [ Ignore ]   [ # 18 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  212
Joined  06-23-2008

Quick Question: Does the new version provide “free house cleaning” for related object on a 1:1 or 1:n relationship?

I realized that it only deletes the relation and not the related record. Is that correct?

 Signature 

Tired of Forms? Check out Form Generation Library
Using Coda? Get the Coda Comments Plug-in

Profile
 
 
Posted: 04 May 2009 05:51 AM   [ Ignore ]   [ # 19 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  421
Joined  11-17-2008

I’m having a bit of a problem

Fatal error: Call to a member function get() on a non-object in D:\dev_stuff\pandm\be\app\controllers\dashboard.php on line 14

Controller

function index()
    
{
        $n
= new News();
        
$n->join_related('user', array('id', 'username'))->get();
        
$data['news'] = $n->all;

        
$this->template->write_view('content', 'news/view', $data, TRUE);
        
$this->template->render();

    
}

News Model

class News extends DataMapper {

    
var $table = 'news';
    var
$model = 'news';
    
    var
$has_one = array('user');
    
    
/**
     * Constructor
     *
     * Initialize DataMapper.
     */
    
    
function News()
    
{
        parent
::DataMapper();
    
}
}

User Model

class User extends DataMapper {

    
var $table = 'users';
    var
$model = 'user';
    
    var
$has_many = array('news');
    
    
/**
     * Constructor
     *
     * Initialize DataMapper.
     */
    
    
function User()
    
{
        parent
::DataMapper();
    
}
}

I’m not using joining tables as you said they are not needed so my tables look like:
User Table
- id
- username

News Table
- id
- user_id
- subject
- body

What am I missing? smile
Thanks in advance!!

 Signature 

- cody

Plugins: AJAX Pagination | AJAX Comments
Extensions: jQuery Quick Save (updated) | Goto Latest Comment
FieldTypes: MS Img Saver
MeanStudios.com

Profile
 
 
Posted: 04 May 2009 11:06 AM   [ Ignore ]   [ # 20 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  848
Joined  10-08-2008

@macigniter
No, just like the current DataMapper, DMZ never deletes existing objects unless explicitly requested.

However, there is an easy way to enable this.  My preferred technique is to add a method called delete_deep to my base extension object, which simply calls $this->delete().  Then, for objects that have one-way relationships, I override this method to delete related objects as needed.

Alternatively, just override delete like this:

function delete($object = '', $related_field = '') {
    
if( ! is_array($object) && empty($object)) {
        
// delete related objects
    
}
    $this
->delete($object, $related
}

Be aware that this leaves you with no way to delete an object without deleting those related objects.  Also, you could set up database integrity rules (foreign keys), which would be set up to automatically delete those objects.

@MeanStudios
That is a bug!  Congratulations, you get a stuffed bear! (just kidding)

For now, don’t chain off of join_related(), because it doesn’t return $this.  I’ll update DMZ.  (Amazing that I never caught that.)

 Signature 

Phil DeJarnett
  OverZealous Creations, LLC
  ZESTYJOBS - Job Management Powered by CI/DMZ NOW LIVE

  DataMapper OverZealous Edition: Advanced ORM for CodeIgniter Discuss

Profile
 
 
   
2 of 80
2
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 819, on March 11, 2010 10:15 AM
Total Registered Members: 119467 Total Logged-in Users: 25
Total Topics: 125732 Total Anonymous Users: 1
Total Replies: 661656 Total Guests: 360
Total Posts: 787388    
Members ( View Memberlist )