Welcome Guest, Not a member yet? Register   Sign In
[Deprecated] DMZ 1.5.3 (DataMapper OverZealous Edition)
#11

[eluser]warrennz[/eluser]
OH rite! Ofcourse! Over looked that a bit

Thanks again OverZealous Smile
#12

[eluser]OverZealous[/eluser]
@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.)
#13

[eluser]qureshi[/eluser]
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
#14

[eluser]OverZealous[/eluser]
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.

Code:
$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:
Code:
$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.
#15

[eluser]qureshi[/eluser]
You're awesome, Man! :-) I'll try that out!

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

Nadeem
#16

[eluser]qureshi[/eluser]
Also,

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

[eluser]NachoF[/eluser]
[quote author="qureshi" date="1241307380"]Also,

Is there difference between $element->save($category) and $category->save($element)?[/quote]

I have been wondering about that one as well.
#18

[eluser]OverZealous[/eluser]
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:
Code:
$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:

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

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

Good Luck!
#19

[eluser]macigniter[/eluser]
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?
#20

[eluser]MeanStudios[/eluser]
I'm having a bit of a problem
Code:
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
Code:
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
Code:
class News extends DataMapper {

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

User Model
Code:
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!!




Theme © iAndrew 2016 - Forum software by © MyBB