ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

[Deprecated] DMZ 1.6.2 (DataMapper OverZealous Edition)

November 23, 2009 11:54pm

Subscribe [46]
  • #256 / Feb 12, 2010 12:37am

    Conerck

    15 posts

    Right, but Why is that a *must*. I get that regular inserts, selects, updates and deletes should clear the query but a Count… I don’t see the point.

    Anyway… Say I run the full query (which potentially costs alot of memory), what would Be a good way to get, say, entries 140 to 160 from the resultsset for the pagination?

  • #257 / Feb 12, 2010 12:47am

    OverZealous

    1030 posts

    It must because that’s the expected result.  If you do this:

    // set up query
    $count = $object->count();
    
    // Later…
    $object->where('fun', TRUE)->get();

    You will get unexpected results on the second query, because it contains everything in the first query.  DMZ clears the query data whenever a query is run on the database.

    If you want both the full query count, and want a subset for pagination, you can either put your query building into a method and run through it twice, or you can do this:

    $count = $object->get_clone();
    // force $db to be by-value
    $count->db = unserialize(serialize($count->db));
    $total = $count->count();

    The hack trick in the middle is to ensure that $db doesn’t get cleared by the count().  This is actually a bug, I just discovered, because in DMZ 1.6, get_clone() should be doing this automatically.  In the next version of DMZ, you will be able to skip that step (as it will be automatic).

    Also note that if you have already added items to the select or order_by statements, your count query may return unexpected results.

  • #258 / Feb 12, 2010 3:37am

    Oblique

    32 posts

    Has anybody here migrated from dedicated table to itfk? Shame on me, can’t figure out the query 8(

  • #259 / Feb 12, 2010 3:41am

    OverZealous

    1030 posts

    @Oblique

    If you are trying to shift values from a dedicated to an ITFK, and you already know your configuration, you could do something crappy but functional like this:

    Note: foos has the ITFK, bars_foos is the join, and bars is the other table

    UPDATE foos
    SET foos.bar_id = (SELECT bar_id FROM bars_foos WHERE bars_foos.foo_id = foos.id)

    I hate using subqueries unnecessarily, but sometimes they solve the problem quickest.

  • #260 / Feb 12, 2010 4:06am

    Oblique

    32 posts

    That worked ) you are overzealous indeed )

  • #261 / Feb 14, 2010 4:16pm

    tdktank59

    322 posts

    Hey

    So ive got a small issue, that ive noticed before… as well.

    When you try and save a single field it runs through the entire validation check and ends up error-ing out on fields that I don’t care about at that point.

    Is there a way we can either make this only check the passed in fields (best solution in my mind, and since then it only checks what we want it to.) Otherwise a way to define which validations to run for a specific insert.

    Because im having an issue where ive got a few fields, for some reason or another a field gets blanked out and I try and update it again and it says cannot update because some other field is required that is blanked out as well.

    Thanks.

  • #262 / Feb 15, 2010 8:04am

    Oblique

    32 posts

    ITFK instead of dedicated table in case of one-way advanced relationship

    We have User and Freightage. User can either create or carry freightage, depending on his role in system, so User can relate to Freightage either as creator or as carrier, but for User Freightage is just Freightage always, so, may i use itfk instead of table? I tried it like that, didn’t work:

    freightage.php:
    ...
        var $has_one = array
        (
             'creator' => array
            (
               'class'         => 'user',
               'other_field' => 'freightage'
            )
            ,'carrier' => array
            (
               'class'         => 'user',
               'other_field' => 'freightage'
            )
    ...
    user.php
    ...
    var $has_many = array
        (
            'freightage',
    ...

    gives me:

    DataMapper Error: ‘user’ is not a valid parent relationship for Freightage. Are your relationships configured correctly?

    Looks like relations like this are not met often, so it’s not supported. Just want to make sure

  • #263 / Feb 15, 2010 3:27pm

    OverZealous

    1030 posts

    @Oblique
    The error message is correct: your relationship is incorrectly defined.  Relationships need to be fully defined on both sides.  There is no way for DMZ to know how to assign the User in this context:

    $user->save($frieght);
    // OR
    $freight->save($user);

    That’s why you need to fully define the relationship on both sides.  If you want two different types of relationships, then you need two different relationships defined on both objects.  You can still build a single query from two different relationships, like this:

    $freight = new Freight();
    $freight->distinct()->where_related_carrier($user)->or_where_related_creator($user)->get();

    Alternatively, have you thought about having a single $has_many<->$has_many relationship and using join fields to define the type of relationship?  (See Using Join Fields in the manual).

    ————————————-

    @tdktank59
    Either you are not loading the object before you are saving it, or you are setting individual fields to NULL after loading it.  DMZ only checks and saves the columns that have not changed since the last get or save.

    ————————————-

    General Update:

    I have some updates to DMZ started, but I’ve been sick since last Thursday.  If I can get over this, I’ll got a new version of DMZ with a lot of performance-oriented changes and improvements out this week or next week.

  • #264 / Feb 15, 2010 3:54pm

    NachoF

    171 posts

    Hey Phil,

    could you please help me on authorization??
    Do you use any of the plugins?? which one specifically?? are they compatible with datamapper?
    if you do use one, do you still create the “User” model for datamapper? how about a “Role” model??
    Id appreciate your advice on this.

  • #265 / Feb 15, 2010 5:47pm

    OverZealous

    1030 posts

    @NachoF
    DMZ’s examples include a fully-functional authentication setup.  For me, authorization is application-specific, and I do not use any off-the-shelf libraries for it.

    I use a simple User belongs to a Group setup, with each group having a series of flags enabling or disabling functionality.  I also extended CI’s Session library to provide convenient methods for checking access (e.g., $this->system->can_view(‘action’)).

    Sorry I cannot be of more service than that.  I tend to write my own tools most of the time, because it’s often difficult to shoehorn someone else’s code into my app.  (Irony intended 😉)

  • #266 / Feb 15, 2010 10:00pm

    PoetaWD

    97 posts

    Hey man !

    Me again…  I have a very simple question… and I am pretty sure you already told me about this.. but… anyway… I will ask:

    I have two models, one is ‘Car’ and the other is ‘TypeofCar’

    The model Car is related to TypeofCar to tell if the car is FORD, or CHEV etc…

    My question is how should the model typeofcar be ? The car should be is has many or in has one ?

    var $has_one = array('typeofcar');    
        var $has_many = array();
    var $has_one = array('car');  <----HERE or    
        var $has_many = array('car');  <----HERE ????

    Thanks !

    Gabriel

  • #267 / Feb 15, 2010 10:12pm

    NachoF

    171 posts

    @NachoF
    DMZ’s examples include a fully-functional authentication setup.  For me, authorization is application-specific, and I do not use any off-the-shelf libraries for it.

    I use a simple User belongs to a Group setup, with each group having a series of flags enabling or disabling functionality.  I also extended CI’s Session library to provide convenient methods for checking access (e.g., $this->system->can_view(‘action’)).

    Sorry I cannot be of more service than that.  I tend to write my own tools most of the time, because it’s often difficult to shoehorn someone else’s code into my app.  (Irony intended 😉)

    So that means that for each method that requires some kind of authorization you have to write authorization logic on it???
    somethinf like

    function create_product()
    {
    if($this->system->can_view('action'))
    //continue writing create product logic
    else
    //redirect()?
    }

    ... I dont think I like that approach.

  • #268 / Feb 15, 2010 11:13pm

    Oblique

    32 posts

    @PoetaWD, let me answer this.

    TypeOfCar definitely has_many Car’s since, just think of that - if you want to query all the cars of particular Type and it’s related to Car as has_one - what car should dmz return??

  • #269 / Feb 15, 2010 11:18pm

    OverZealous

    1030 posts

    @PoetaWD
    Each car has one typeofcar, but each typeofcar has many cars.  Therefore, you put typeofcar in Car->has_one, and you put car in TypeOfCar->has_many.

    ————————————-

    @NachoF
    Well, it’s not quite that bad.  My session method automatically handles returning the error, so it’s just this:

    function create_product() {
        $this->session->can_create('product');
        // continue on
    }

    I don’t necessarily recommend my technique for someone else.  I needed very fine grained controls (for example, a user might be able to view and edit, but not add or delete a specific item), so there’s really no practical way around checking before each controller function.  (I also handle other checks, like verifying that the user is logged in, in the controller’s constructor.)

    I also use extensions to DataMapper to handle a lot of the more general checks (for example, I have methods that process the results of a form, and do the auth checks internally based on $model).  My extended DMZ class also handles the authorization lookups on every get, to prevent users from seeing another user’s data.

    You could probably use CodeIgniter’s hooks to handle a more general-purpose authorization technique.

  • #270 / Feb 15, 2010 11:39pm

    someoneinomaha

    14 posts

    I’m struggling with modeling a relationship with a project I’m working on.

    The models are: band, musician, instrument

    Bands have multiple musicians

    Musicians have multiple bands and multiple instruments

    That’s all pretty straightforward, but I also need to keep track of what instruments a musician has for a particular band. So in a sense, I guess, bands have multiple instruments via the musicians.

    In the tables, I was going to add instrument_id to the bands_musicians linking table, but I need a musician to be able to have multiple instruments for a band, so I was thinking it would need to go in the musicians_instruments table.

    Would anyone have advice on how to model this with DataMapper OverZealous?

    Thanks for your time.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases