Wednesday, April 14, 2010

CakePHP single table inheritance implementation using class inheritance

If you have a couple of document types that you don't want to create individual db tables for you can put them all in a 'documents', have an abstract Document model class and create those document types as model classes each extending the Document class. You just need in your documents table a column named like the TYPE_COLUMN constant declared in this class.
Disclaimer: I borrowed most of the code form the ExtendableBehavior (http://bakery.cakephp.org/articles/view/extendablebehavior) but wrote the idea as an abstract class rather than a Behavior. Some people on IRC are already waiting for this to break in some unknown ways so use it at your own risk!
I will post updates if it breaks :)

Update: well it still works fine. In case you didn't catch the trick, u need to put the 'type' column in the documents table!

abstract class Document extends AppModel {
 
 const TYPE_COLUMN = 'type';
 
 var $useTable = 'documents';
 
 function beforeFind ($queryData=null) {
  if (!isset($queryData['conditions'])) {
   $queryData['conditions'] = array();
  }
  if (is_string($queryData['conditions'])) {
   if (strlen(trim($queryData['conditions']))) {
    $queryData['conditions'] = "({$queryData['conditions']}) AND ";
   }
   $queryData['conditions'] .= $this->alias. '.'. self::TYPE_COLUMN. ' = '. $this->alias;
  } elseif (is_array($queryData['conditions'])) { 
   if (!isset($queryData['conditions'][$this->alias.'.'. self::TYPE_COLUMN])) {
    $queryData['conditions'][$this->alias. '.'. self::TYPE_COLUMN] = array(); 
   }
   $queryData['conditions'][$this->alias. '.'. self::TYPE_COLUMN] = $this->alias;
  }
  
  return $queryData;
 }
 
 function beforeSave () {
  if (array_key_exists(self::TYPE_COLUMN, $this->_schema)) { 
            if (!isset($this->data[$this->alias])) {
                $this->data[$this->alias] = array();
            } 
            $this->data[$this->alias][self::TYPE_COLUMN] = $this->alias; 
        } 
        return true;
 }
}

Friday, April 09, 2010

New iPhone4 SDK TOS

"Applications that link to Documented APIs through an intermediary translation or compatibility layer or tool are prohibited"
iPhone OS4 SDK Terms Of Service
"We are looking into the new SDK language. We continue to develop Packager for iPhone OS which will debut in Flash."
"iPhone applications built with Flash Platform tools are compiled into standard, native iPhone executable packages and no runtime interpreter is necessary to run the application. Over 30 Applications built using the Flash Packager for iPhone have already been accepted in the iPhone app store so we’re confident that our method fits within the rules of the iPhone App Store."
Jeremy Clark, Adobe 
"What a horseshit maneuver by Apple."
Hacker News