User Profile

User
 cornernote
Location
 Australia
URL
 http://mrphp.com.au

Recent Articles

unbindAll

Here is a simple way to unbind all models. Just put this function into your app_model.php
  • Published by cornernote 12/10/06 - 18:04
  • 20827 views
  • 5 comments

Maxlength using Database Field Lengths

I wanted to have a maxlength="something" in my forms, but I needed the value to come from the database at least during development. When it goes live the data will cache so it will still be fast.
  • Published by cornernote 11/27/06 - 00:34
  • 8118 views
  • 1 comment

Recent Comments

Posted 23/12/2007 07:00pm
I have some questions about defining the upload folder used in these 2 functions.

function __getFolder(&$model, $record) {
extract($this->settings[$model->name]);
return $baseDir .'/'. Inflector::camelize($model->name) .'/'. $record[$model->primaryKey] . '/';
}
function __getFullFolder(&$model, $field) {
extract($this->settings[$model->name]);
return WWW_ROOT . IMAGES_URL. $baseDir .DS. Inflector::camelize($model->name) .DS. $model->id .DS;
}


Where do $baseDir and IMAGES_URL get defined?
Posted 23/12/2007 06:44pm
Would be great to move the GD stuff out of __resize() and put them into __resize_gd2() and then create __resize_imagemagick().

This would allow the use of the far superior imagemagick over gd, and would also be easily extend with other image lib functions if needed.

p.s. Awesome code, thanks!
Posted 27/12/2006 06:33am
The snippet above was written by Othman ouahbi (aka: CraZyLeGs)

The original can be downloaded from here:
http://othy.wordpress.com/2006/06/03/unbind-all-associations-except-some/
Posted 27/12/2006 06:18am
I found another one. This one looks better. I can't remember who made it but I thought I'd post it here. If your the author please feel free to post a comment and take the credit. My main point is that it wasn't me. :)

Model Class:

<?php 
  
function unbindAll($params = array())
  {
    foreach(
$this->__associations as $ass)
    {
      if(!empty(
$this->{$ass}))
      {
        
$this->__backAssociation[$ass] = $this->{$ass};
        if(isset(
$params[$ass]))
        {
          foreach(
$this->{$ass} as $model => $detail)
          {
            if(!
in_array($model,$params[$ass]))
            {
              
$this->__backAssociation array_merge($this->__backAssociation$this->{$ass});
              unset(
$this->{$ass}[$model]);
            }
          }
        }else
        {
          
$this->__backAssociation array_merge($this->__backAssociation$this->{$ass});
          
$this->{$ass} = array();
        }

      }
    }
    return 
true;
  }
?>
Posted 19/12/2006 04:19am
Put this in the helper.

Then use it like this:

View Template:


if($othAuth->hasPermission('items/delete')){ ... }

Helper Class:

<?php 
  
function hasPermission($val,$arg='name')
  {
    
$perms $this->permission($arg);
    if (
in_array('*',$perms))
    {
      return 
true;
    }
    if (
in_array($val,$perms))
    {
      return 
true;
    }

    
$vals explode('/',$val);
    
$val '';
    for (
$i=0$i<count($vals); $i++)
    {
      if (
$i)
      {
        
$val .= '/';
      }
      
$val .= $vals[$i];
      if (
in_array($val,$perms))
      {
        return 
true;
      }
    }
    return 
false;
  }
?>