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

[eluser]OverZealous[/eluser]
There is no special changes for in-table relationships! DMZ will automagically determine where the id is stored.

So, go ahead and use your first examples.

The only time you might want to specifically query 'item_id' is if you are doing something complicated like querying for items that don't have a relationship set.

Don't forget, you can also still use:
Code:
$cal1 = $office->calendar->get();
$cal2 = $user->edited_calendar->get(); // assuming the 'other_field' is 'edited_calendar'
$cal3 = $user->worker_calendar->get(); // assuming the 'other_field' is 'worker_calendar'

(If you are having trouble with your queries, make sure you have defined the relationships on both related models!)
#62

[eluser]OverZealous[/eluser]
@NachoF
I can't believe there are still hosts forcing PHP4 on people. ((shudder))

I am lucky that I have not had that issue, yet!
#63

[eluser]NachoF[/eluser]
.... not just php4... mysql 3.23??... according to CI's server requirements you need mysql 4 or higher.... whatever, if it doesnt work ill tell them to get a new webhost.... I hate how my country can be so third-wordly in some things.
#64

[eluser]warrennz[/eluser]
Issue ? Smile Helps Tongue

Categories N:1 Products

This works fine as one might expect
Code:
$c = new Category();
$c->get_by_id('9000');

$p = $c->product->where('enabled',1)->get();

How ever this returns an ambiguous error as the category table also has an 'enabled' field
Code:
$p = $c->product->where('enabled',1)->join_related('category',array('name'))->get();

Is there another way to do this? I would rather avoid writing the table name in every where() clause where this occurs Tongue

Cheers

EDIT

This outputed query is; (the table prefix is 'cl')

Quote:SELECT `cl_products`.*, `cl_categories`.`name` AS category_name FROM (`cl_products`) LEFT JOIN `cl_categories_products` as cl_categories_products ON `cl_products`.`id` = `cl_categories_products`.`product_id` LEFT JOIN `cl_categories` as cl_categories ON `cl_categories`.`id` = `cl_categories_products`.`category_id` WHERE `enabled` = '1' AND `cl_categories`.`id` = '1'
#65

[eluser]OverZealous[/eluser]
Well, it shouldn't be doing that. I'm not sure why it would, because the part of the code that adds a table adds it as long as there is a parent relationship, and the field part of the where doesn't include a "." or a "(".

If you want to force adding the table, there's three ways:

1) Try reversing the query order, which might force it to add:
Code:
$p = $c->product->join_related('category',array('name'))->where('enabled',1)->get();

2) Just type it in:
Code:
$c->product->where('cl_products.enabled',1)

3) If you want to do it "correctly", DMZ includes a function called add_table_name, which is what where uses:
Code:
$product = $c->product;
$product->where($product->add_table_name('enabled'), 1)

Which you choose between 2 and 3 depends on personal preference.
#66

[eluser]warrennz[/eluser]
Many thanks Over Zealous.

Will need to test #1 but for now #3 does just swimmingly Tongue

Thanks again
#67

[eluser]warrennz[/eluser]
Hey again

I find myself repeating quite a bit within models.

One example is when I turn a result set into a simple array('id'=>'name') for html dropdown boxes (selects)

I'm just trying to figure wheres the best place to store this functionality?

Currently I tend to store them in the models. Would it be better to store them in a helper instead, and pass that helper the $obj->all array as well as the field you wish to use as a display?

Would it be better to extend DMZ in someway (just for me) to add simple repetitive methods like this?

I do enjoy writing
Code:
$data['items'] = $c->where('enabled','1')->get()->droplist('name');

and then in the view
Code:
form_dropdown('items',$items);

Thoughts/suggestions/opinions please Smile

Thanks
#68

[eluser]OverZealous[/eluser]
I mentioned this before, and there was a lot of discussion on the regular DM.

I highly recommend that you create a subclass model of DataMapper. I call mine DataMapperExt. Then only subclass that, instead of DM directly.

Then you put all of your shared code in there. This allows you to easily make extensions to DM without worrying about upgrades.

Ex:
Code:
// Model -----------------------

class DataMapperExt extends DataMapper {
    function __construct() {
        // any startup code...
        parent::__construct();
        // any startup code...
    }

    function save_from_form() {
        foreach($this->fields as $f) {
            $CI =& get_instance();
            $v = $this->input->post($CI->input->post($f);
        }
    }
}

// Model -----------------------

class User extends DataMapperExt {
    function __construct() {
        parent::__construct();
    }
    // normal DM model
}

// In Controller ---------------

$user->save_from_form();

My personal rule is never repeat code more than once. The third time I find myself copying and pasting a block of code with only minor updates, it's time to abstract it and store it in a library.

Also, don't limit yourself to DataMapperExt. You might discover a whole set of models that share features with each other, but not with anything else. For these extend DataMapperExt with another class, then extend that, etc. For example, I have a Sortable model I use that handles storing an index in "sortorder", and automatically increments that value as new items are added. It also contains methods for rearranging those items.

One final note: I don't recommend storing all of the display methods inside the model. I handle that differently. If you place them in the model, you are muddying up the MVC structure. Instead, think about placing those in a Library (there's nothing to writing them). I have a pretty cool trick for my library, but it's somewhat technical. Basically, in my Ext class, I use the magic __get method to automatically load my library when needed:
Code:
__get($field) {
    if($field == 'e') {
        // storing in $this->e will prevent __get from being called again.
        // Html_Editor handles input fields.
        $this->e = new Html_Editor($this);
        return $this->e;
    } else if($field == 'v') {
        // storing in $this->v will prevent __get from being called again.
        // Html_Viewer handles formatting strings.
        $this->v = new Html_Viewer($this);
        return $this->v;
    }
    return parent::__get($field);
}

// usage
echo $this->e->dropdown('my_field');

(Actually, that is highly simplified, but it might give you a head start.)
#69

[eluser]J101[/eluser]
When using this library and relating two normal tables, are the primary keys also joined? In all the samples I see, I don't see where the primary keys are joined and I am curious as to how these tables relate. I'm sure that I'm not seeing something so if someone would like to give a hand, I'd be grateful.
#70

[eluser]OverZealous[/eluser]
What do you mean by "are the primary keys also joined"? DataMapper doesn't actually rely on any in-database relationship information, merely a properly configured and labeled database.

Please provide an example of your tables (names are enough, usually), and how you want them joined.

Also, please take a look at the original DataMapper examples, specifically the Getting Start and Database Schema sections.




Theme © iAndrew 2016 - Forum software by © MyBB