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]
  • #181 / Jan 19, 2010 7:58am

    Oblique

    32 posts

    include_related_count deep relationship

    $user->include_related_count('user_profile/driver', 'drivers')

    gives me

    Table ‘DB.drivers_users’ doesn’t exist

    while i assumed that dmz would query user_profiles_drivers for that count

    what exactly do i not understand here? user guide says deep relations are available for this method

  • #182 / Jan 19, 2010 2:47pm

    chadbob

    11 posts

    Datamapper has made my job a breeze recently so I guess its time I gave something back!

    I’ve been working on my own CodeIgniter Table and Form libraries: GoodForm and BadTable, for a while now since most of my work is based around CMS web apps.

    Both libraries have been designed to work standalone but I have also included DMZ extension files to add their functionality to DM Models.

    If any one is interested please download a copy from my site and have a play… There will be bugs, but as i’m currently developing with both systems I should be able to provide fixes pretty quickly.

    PM me if you have any questions.

    Jim


    This looks great, would you mind adding a simple sample with a basic model and generation/post?  The documentation between the normal operation and the DMZ version seems disparate.  Thanks!

  • #183 / Jan 19, 2010 2:52pm

    12vunion

    36 posts

    include_related_count deep relationship

    $user->include_related_count('user_profile/driver', 'drivers')

    gives me

    Table ‘DB.drivers_users’ doesn’t exist

    while i assumed that dmz would query user_profiles_drivers for that count

    what exactly do i not understand here? user guide says deep relations are available for this method

    It looks for table joins alphabetically. If you had two models, user_profile and driver, it would look for their tables (user_profiles, drivers) and combine them alphabetically to make a join table. Thus, your join table should be drivers_user_profiles.

    Table Naming Rules

  • #184 / Jan 19, 2010 3:04pm

    Oblique

    32 posts

    @12vunion:
    looks like i posted too little data to concider:

    i have models user, user_profile, driver, all with tables properly named and everything else works fine.
    What does not work is counting deep relation

    I consider that DMZ is supposed to work like that here:

    $user = new User($id);
    
    //here we count drivers related to user_profile
    //that is related to our $user
    
    $user->include_related_count('user_profile/driver', 'drivers');
    
    echo $user->drivers;
  • #185 / Jan 19, 2010 6:38pm

    Jack Scott

    18 posts

    DMZ 1.6.2 appears to mangle custom queries that contain data that looks like a table name.

    I have a section of code that looks up domain names from a database table using a custom WHERE clause.  It looks something like:

    $domain = 'www.my.example.com';
    $this->load->model('domain');
    $this->domain->where("'$domain' LIKE CONCAT('%', REPLACE(name, '_', '\\_'))");
    $this->domain->order_by('CHAR_LENGTH(name) DESC');
    $this->domain->get();

    Datamapper mangles the WHERE clause into something that looks like:

    WHERE `www`.`my`.`example`.`com` LIKE

    I have a similar problem in another section of code with a custom ORDER clause.  It looks something like:

    $ids = array(1, 5, 20, 3, 14, 16);
    $this->property->order_by('FIELD(properties.id, ' . implode(',', $ids) . ' )');

    This should produce an ORDER BY clause that looks like:

    ORDER BY FIELD(properties.id, 1,5,20,3,14,16)

    But instead, I get:

    ORDER BY FIELD(properties.id, `1`,`5`,`20`,`3`,`14`,`16`)

    Is there a way to turn off special processing in datamapper, or specify a query in a way that datamapper doesn’t rewrite?  Can the datamapper methods be updated to recognize when they’re called with single arguments and turn off table rewriting?

  • #186 / Jan 19, 2010 6:52pm

    OverZealous

    1030 posts

    @Jack Scott
    DMZ uses CodeIgniter’s ActiveRecord classes for handling the queries.  It is CI that mangles queries.

    However, DMZ has new methods to help build functions with arguments without escaping.  Please see SQL Functions in the manual for more information.

    You can also manually disable escaping setting the third parameter to FALSE, just like in ActiveRecord.

    @Oblique
    There’s no way we can help you with so little information.  My guess is that you have a misconfigured relationship.

    Why don’t you take some time and look at the generated queries.  See what the query looks like, and determine what it should look like.  If we at least knew what was wrong, we might be able to help.

  • #187 / Jan 20, 2010 2:11pm

    Jack Scott

    18 posts

    Thanks for your quick reply.  Adding the extra field to where() fixed the issue I was having.  The where() function now looks like:

    $this->EE->rem4_domain->where("$domain LIKE CONCAT('%', REPLACE(name, '_', '\\_'))", NULL, FALSE);

    I’m using EE2 Public Beta on this project, so I ran into another unexpected interaction.  CI as configured in EE2 uses a database table prefix, so it ends up rewriting the WHERE clause as:

    WHERE 'www.my.exp_example.com' LIKE

    I didn’t see an easy way around this, so I ended up writing the query manually using the query() method.

    I never could get order_by() to work for another query.  My order_by statement looks like:

    $ids = array(1, 5, 20, 3, 14, 16);
    $this->property->order_by('FIELD(properties.id, ' . implode(',', $ids) . ' )');

    No matter what I did, I couldn’t get CI to leave the array of ids alone.  I ended up writing a quick and dirty DMZ extension to handle order by field expressions, using some tricks I found in DMZ’s source.  The order_by statement is now much simpler.  It looks like:

    $this->property->order_by_field('properties.id', $ids);

    Here’s the extension.  It works for me, maybe it’ll work for you.

    <?php
    
    # DMZ extension adds order_by_field method
    # usage:
    #    $dmz->order_by_field($fieldname, $array, $escape = TRUE)
    # creates ORDER BY FIELD('fieldname', $array_1, $array_2, ... $array_n)
    # escapes array entries if $escape is set
    # assumes DMZ 1.6.0 or greater
    
    # written by Jack Scott <[email protected]> Jan 20 2010
    # copyright (C) 2010 eMarketSouth LLC
    # released under the same license as DataMapper Overzealous Edition
    # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
    # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
    # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
    # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
    # DEALINGS IN THE SOFTWARE.
    
    class DMZ_order_field {
        
        function order_by_field($object, $field, $array, $escape = TRUE)
        {
            if (! is_array($array))
            {
                # $array not an array, return now
                return $object;
            }
            
            $db =& $object->db;
            
            # need to escape array contents?
            if ($escape)
            {
                $array_escape = array_map(array($db, 'escape'), $array);
            }
            else
            {
                $array_escape =& $array;
            }
            
            # generate the ORDER clause, pass it to $db object
            $order = 'FIELD(' . $object->add_table_name($field) . ', ' . implode(', ', $array_escape) . ')';
            
            # CI protect_identifiers mangles the ORDER clause - turn it off
            $protect = $db->_protect_identifiers;
            $db->_protect_identifiers = FALSE;
            $db->order_by($order);
            $db->_protect_identifiers = $protect;
            
            return $object;
        }
            
    }
    /* End of file order_field.php */
  • #188 / Jan 20, 2010 4:05pm

    Muser

    78 posts

    I am using the HTML Form extension in a medium/large project and saving some troubles I have got, I think it might be a wonderful extension.

    For me it is difficult to use DMZ without using this extension. If you decide to separate this extension from the DMZ latest version zip archive, please I think you should consider to include in the zip file an example of adding, editing and deleting objects and relations without using this extension, to become a best practice reference.

    If I am able to correct some bug for HTML Form extension I’ll notify the community.

    Thank you!

    To All: (Regarding the HTML Form extension)

    I understand that the HTML form extension is very useful to many people.  However, I do not have the time to maintain it or support it.

    I would like to find someone who is willing to take over maintenance and support for this extension.  It needs some work, and I just barely have enough time to keep up with the work on the core DMZ library.

    If you are interested in maintaining it, please let me know (either through a PM or right here).  Just to be clear, everything I have for this extension is already included in the full DMZ download (including the documentation), so if you are interested, feel free to look at the source directly.

    I would be willing to include someone else’s work with the full download, or pull the HTML extension out of the core download, and link to it somewhere else.  The latter is probably preferable, since I won’t necessarily upgrade DMZ on the same schedule as the HTML form extension.

  • #189 / Jan 21, 2010 6:32am

    cube1893

    18 posts

    Hi guys,

    I want to use DMZ with transactions and tried to run the following code on a MySQL database with InnoDB:

    $f = new File();
        $f->file_name = 'test';
            
        $f->trans_begin();
        $f->save();
        $f->trans_rollback();
            
        // Show all errors
        echo $f->error->string;
        
        // Or just show the transaction error
        echo $f->error->transaction;

    It’s just for testing, but the rollback doesn’t work! $config[‘auto_transaction’] is set to FALSE .
    Thanks for any help!

  • #190 / Jan 21, 2010 6:41am

    OverZealous

    1030 posts

    You aren’t testing the save() method.  Are you sure the save is working correctly?

    Transactions have worked for many people.  It is most likely a database problem.

  • #191 / Jan 21, 2010 7:08am

    cube1893

    18 posts

    I know that I’m not testing the save() method. For testing the transactions, I want to rollback the transaction even if the object saving was successfull.

    One more thing: When I var_dump $f->trans_status(), it returns NULL.

  • #192 / Jan 21, 2010 5:33pm

    Hi,
      I want to update a multiple relationship (without first deleting and then saving). I receive a multiple checkbox from $_POST, like this:

    In model
      class Language .... $has_many = array(“country”) ...}
      class Country ... $has_many = array(“language”) ...}

    In view
    <input type=“checkbox” name=“countries[]” value=“x” />

    In controller
      What have I to do in the controller?

    Thanks

  • #193 / Jan 21, 2010 8:49pm

    OverZealous

    1030 posts

    You can see examples of how to save relationships by looking at the source to the Array extension.

    It isn’t really that complicated, however.  You just query all of the currently related items, then loop through twice.  Finally, save the new items.

    Loop once to remove any items that are not in the new list.
    Loop again to determine what IDs are new.

    Then, look up each new item, and save it in an array back to the parent object.

    Don’t forget you can save a lot of time by calling select(‘id’) before getting any relationship, since you only care about relating the items (not writing them out).

  • #194 / Jan 24, 2010 1:28am

    tdktank59

    322 posts

    Hey OverZealous.

    So got a quick question. May be covered in the docs but not sure.

    So ive got users and normaly they all will have roles. But new users will not.
    So I need to be able to pull all users that do not have a role.

    Users <- Many to Many -> Roles

    Ill be working on a work around where I will loop through the users and ones without roles ill pass on to the view but would be sweet if there was a way dmz could do this if it exists!

  • #195 / Jan 24, 2010 1:51am

    OverZealous

    1030 posts

    It might not be in the docs, but usually it is something like this:

    $u = new User();
    $u->where_related_role('id', NULL);
    $u->get();

    Give it a shot, let me know if it doesn’t work for you.  You might have to play around with it, and look a the query generated.

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

ExpressionEngine News!

#eecms, #events, #releases