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]
  • #451 / Mar 15, 2010 3:08pm

    OverZealous

    1030 posts

    @GregX999
    First off, that error means you either have misconfigured your relationships] or have [url=http://www.overzealous.com/dmz/pages/troubleshooting.html#General.Plural.Controller]a naming issue.  Also, please see the top of the Troubleshooting page, and make sure you check each numbered item.

    Second, that query doesn’t make sense.  The query you ran would, if it didn’t out-right error, always return 0 or 1, because you are querying the number of Products with an ID of $product_id.  Also, you should never need to specify a table name in DMZ.  That’s usually a sign you are doing it wrong. 😉

    So, what are you trying to query?

    E.g.:
    Number of categories for a given product:

    $count = $category->where_related('product', 'id', $product_id)->count();

    Edit:
    I forgot to include the cleaner alternative if you already have the $Product:

    $count = $product->category->count();
  • #452 / Mar 15, 2010 3:20pm

    GregX999

    39 posts

    Yeah, I guess maybe I’m not approaching what I want to be doing correctly.

    I have a category (as an object) and I have a product_id (id field from product class) and I just want to do an “if” to see if the product is in that category (or if the category is assigned to the product - same thing from the “other direction”).

    if (category contains product) { do stuff }
  • #453 / Mar 15, 2010 3:22pm

    OverZealous

    1030 posts

    @GregX999

    OH!  Why didn’t you say so.  Then your query was basically correct:

    $exists = ($category->product('id', $product_id)->count() > 0);

    You just didn’t need to specify the table name.

    The other error, however, is not related to the query problem.

  • #454 / Mar 15, 2010 3:37pm

    GregX999

    39 posts

    I originally didn’t use the table name (“product.id”) and I got the error, that’s why I tried w/ it. I changed it back (to just “id”) and I still get that same error.

    Fatal error: Class name must be a valid object or a string in /.../.../.../application/libraries/datamapper.php on line 1730

    Is a singular/plural issue causing the problem?

    The product model is “Product”.
    In the category model, I have:

    var $has_many = array('product');

    (singular “product”)

    In the error, I’m unsure which “class name” it’s referring to.

  • #455 / Mar 15, 2010 3:38pm

    OverZealous

    1030 posts

    @GregX999
    Please see the message I wrote before, and check the troubleshooting page.  I gave you several starting points there already.

  • #456 / Mar 15, 2010 4:03pm

    GregX999

    39 posts

    Sorry about that - I lost track of that link as we flipped to page 46…

    Anyway, I figured it out thanks to that troubleshooting page. I had the relation from the product back to the category setup wrong. So I fixed that, then just had to change my query to:

    $cat->product->where('product_id', $product_id)->count();

    That works, but it seems a bit messy directly accessing the join table (both relationships are has_many). If I used (‘id’, $product_id) I get an error saying the join table doesn’t have an “id” field.

    Thanks for the help!
    Greg

  • #457 / Mar 15, 2010 4:22pm

    OverZealous

    1030 posts

    @GregX999
    I know where the problem is.  The Count method does some sneaky stuff to actually count the results, and doesn’t necessarily include the full table.

    I think I might add a is_related_to method in the future, so the underlying infrastructure is hidden.

    It’ll probably be in 1.7.1!  😊

  • #458 / Mar 15, 2010 4:40pm

    OverZealous

    1030 posts

    OK, the function is already written for the next release.  Just needs documentation…  :cheese:

  • #459 / Mar 15, 2010 6:04pm

    GregX999

    39 posts

    Jeeze, you’re fast!

  • #460 / Mar 15, 2010 6:13pm

    OverZealous

    1030 posts

    My secrets:
    1) In-house email server (I get forum posts instantaneously via IMAP).

    2) Never test what I write. 😛 (Just kidding.  Everything has some testing.  How ever, I do rely on the users of DMZ to do the bulk of bug finding for me.)

    3) I eat my own dogfood: I use DMZ in my own code, so features I would to use myself end up in there very quickly.  😉

  • #461 / Mar 17, 2010 6:54am

    wandschrank

    6 posts

    Hello DMZ-Users,
    i have made a quick/small checkbox-multicolumn hack for the “htmlform”-extension.

    $form_fields = array(
                    'id',
                    'tag' => array(
                        'input_separator' => '',
                        'checkbox_cols' => 3
                    )
                );

    ‘checkbox_cols’ is the new feature.

    The changes in the htmlforms.php are in the _checkbox function:

    // renders one or more checkboxes or radio buttons
    function _checkbox($type, $id, $value, $options, $sub_id = '', $label = '')
    {
        if(isset($options['value']))
        {
            $value = $options['value'];
            unset($options['value']);
        }
        // if there is a list in options, render our multiple checkboxes.
        if(isset($options['list']))
        {
            $list = $options['list'];
            unset($options['list']);
            $ret = '';
            if( ! is_array($value))
            {
                if(is_null($value) || $value === FALSE || $value === '')
                {
                    $value = array();
                }
                else
                {
                    $value = array($value);
                }
            }
    
            $sep = '<br>';
            if(isset($options['input_separator']))
            {
                $sep = $options['input_separator'];
                unset($options['input_separator']);
            }
    
            $col = 0; // Standard 1 Column
            if(isset($options['checkbox_cols']))
            {
                $col = $options['checkbox_cols']-1;
                unset($options['checkbox_cols']);
            }
    
            $num = 0;
            foreach($list as $k => $v)
            {
                if( ! empty($ret))
                {
                    // put each node on one line.
                    $ret .= $sep;
                }
    
                if($col!=0){
                    if($num==0){
                        $ret .= '<tr><td>'.$this->_checkbox($type, $id, $value, $options, $k, $v).'</td>';
                        $num++;
                    }elseif ($num%$col == 0){
                        $ret .= '<td>'.$this->_checkbox($type, $id, $value, $options, $k, $v).'</td></tr>';
                        $num=0;
                    }else {
                        $ret .= '<td>'.$this->_checkbox($type, $id, $value, $options, $k, $v).'</td>';
                        $num++;
                    }
                }else{
                    $ret .= $this->_checkbox($type, $id, $value, $options, $k, $v);
                }
    
            }
            return '<table class="cb">'.$ret.'</table>';
        }
        else
        {
            // just render the single checkbox.
            $node_id = $id;
            if( ! empty($sub_id))
            {
                // there are multiple nodes with this id, append the sub_id
                $node_id .= '_' . $sub_id;
                $field_value = $sub_id;
            }
            else
            {
                // sub_id is the same as the node's id
                $sub_id = $id;
                $field_value = '1';
            }
            $name = $id;
            if(is_array($value))
            {
                // allow for multiple results
                $name .= '[]';
            }
    
            // node attributes
            $a = $options + array(
                'type' => $type,
                'id' => $node_id,
                'name' => $name,
                'value' => $field_value
            );
            // if checked wasn't overridden
            if( ! isset($a['checked']))
            {
                // determine if this is a multiple checkbox or not.
                $checked = $value;
                if(is_array($checked))
                {
                    $checked = in_array($sub_id, $value);
                }
                if($checked)
                {
                    $a['checked'] = 'checked';
                }
            }
            $ret = $this->_render_node('input', $a);
            if( ! empty($label))
            {
                $ret .= ' ' . $this->_render_node('label', array('for' => $node_id), $label);
            }
            return $ret;
        }
    }

    Greetings

  • #462 / Apr 13, 2010 2:19am

    Alface

    41 posts

    Can anyone take a look on my unanswered question? it is here http://ellislab.com/forums/viewreply/695462/

  • #463 / May 02, 2010 1:28pm

    Wazzu

    27 posts

    Delete several rows

    I want to delete all students wich are 17 years old.
    I can’t get it with this simple code. It just deletes one by one, instead of deleting all rows. What’s wrong?

    //Set age
    $age = 17;
    
    // Get student
    $u = new Student();
    $u->where('age', $age)->get();
    
    // Delete user
    $u->delete();
  • #464 / May 02, 2010 11:15pm

    OverZealous

    1030 posts

    Wazzu
    Delete only acts on the top-level item.  Search the manual for delete_all - this is what you need.

  • #465 / May 03, 2010 3:09am

    Wazzu

    27 posts

    Ok, I see it clearly: http://www.overzealous.com/dmz/pages/deleteall.html
    Thanks very much

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

ExpressionEngine News!

#eecms, #events, #releases