Archive of December 2008

22 Dec

My PHP code snippets at GitHub

These functions were mostly written by me for a current project but I also have compiled some stuff from other devs and open source repos.

You’ll find mostly PHP here but I’ll also keep Ant tasks, JS and notes here.

Courier,
AppleGothic,
Arial,
STHeiti TC”,
“Hiragino Kaku Gothic ProN”,
“Courier New”,
Zapfino,
“Arial Unicode MS”,
STHeiti SC”,
“American Typewriter”,
Helvetica,
“Marker Felt”,
“Helvetica Neue”,
“DB LCD Temp”,
Verdana,
“Times New Roman”,
Georgia,
STHeiti J”,
“Arial Rounded MT Bold”,
“Trebuchet MS”,
STHeiti K”

— Font families available to the iPhone by default
19 Dec

Link it up! Pulling in Traffic via Social Sites

Place content on Flickr & YouTube

  • Post about the content on your Website
  • Bookmark with Bit.ly
  • Comment on Twitter using that bookmark
  • Twitter gets placed on Facebook (via Twitter app) & FriendFeed
  • Link back to your website on the Flickr & YouTube pages
  • Track your link traffic on Bit.ly and site traffic with Google Analytics

18 Dec

Symfony 1.1 Time/Code Saver using Model->fromArray()

I found a pretty nice time and code saver in the fromArray() method. Currently, we’re translating our arrays from the form’s POST data into what works for the models’ fromArray() method.

Here’s an example:

$pet_txt->fromArray(array(
	“PetInfoId”   => $params[‘pet_info_id’],
	“VetInfo”     => $params[‘vet_info’],
	“SpecialCare” => $params[‘special_care’],
 ));

Apparently there’s an easier way:

$pet_txt->fromArray($params, BasePeer::TYPE_FIELDNAME);

What that does is use the underscored field_name style rather than the default CamelCase TYPE_PHPNAME when inserting the new variables. Please note that fromArray() will only insert the parts of the array that match the model (ie. ‘password’ won’t get set on the Pet model). It will also overwrite anything you’ve set previously.

If you need to set other keys that are not in the array, you can use the Model->setFieldName() methods, or you can use Model->setByName(‘pet_id’, $params[‘pet_id’], BasePeer::TYPE_FIELDNAME).

Here’s the other options available:

/**
 * phpname type
 * e.g. ‘AuthorId’
 */
const TYPE_PHPNAME = ‘phpName’;
/** * column (peer) name type * e.g. ‘book.AUTHOR_ID’ */
const TYPE_COLNAME = ‘colName’;
/** * column fieldname type * e.g. ‘author_id’ */
const TYPE_FIELDNAME = ‘fieldName’;
/** * num type * simply the numerical array index, e.g. 4 */
const TYPE_NUM = ‘num’;

Using Symfony 1.1's Error Levels Properly

Here’s how to log a message in your action (using the INFO=6 level):

$this->logMessage(‘account_id: ‘.$account->getAccountId(),‘INFO’);

And elsewhere (using the CRIT=2 level):

sfContext::getInstance()-&gt;getLogger()-&gt;log(&#8216;rsync returned error code (&#8217;.$return.&#8217;)&#8217;, &#8216;<span class="caps">CRIT</span>&#8217;);
sfContext::getInstance()-&gt;getLogger()-&gt;log(debug($output,0,1),&#8216;<span class="caps">CRIT</span>&#8217;); // for Arrays
sfContext::getInstance()-&gt;getLogger()-&gt;warning(&#8216;we should look at why this is happening&#8217;); // using the <span class="caps">WARNING</span>=4 level

Here’s the error levels that symfony gives us, ERR=3 being the default:

const EMERG   = 0; // System is unusable
const ALERT   = 1; // Immediate action required
const CRIT    = 2; // Critical conditions
const ERR     = 3; // Error conditions
const WARNING = 4; // Warning conditions
const NOTICE  = 5; // Normal but significant
const INFO    = 6; // Informational
const DEBUG   = 7; // Debug-level messages

I wrote a new logException method for us to have a standard way to log and deal with Exceptions. This should hopefully end up being a standard way to deal with Exceptions across the project. The dUtils::logException() method sets the message at the ERR=3 level by default. You can change that with the second argument.

class dUtils{
  public static function exceptionToString($e){
    return $e->getFile().” on line no. “.$e->getLine().” “.$e->getMessage().“nn”.$e->getTraceAsString();
  }
  public static function logException($e,$error=sfLogger::ERR){
    sfContext::getInstance()->getLogger()->log(dUtils::exceptionToString($e),$error);
  }
}

try {
  $sf_secure_user->save();
} catch (PropelException $e) { // problem with save dUtils::logException($e); $this->getUser()->setFlash(‘error’, ‘Account not created. Please try again.’); return sfView::SUCCESS;
}

And here’s how to set the logging level in factories.yml. Please be aware that the default level for all logs is set at the lowest level, DEBUG=7. On production, the level is set at ERR=3. This means that in the log file, we’ll see those errors above level DEBUG=7 or ERR=3, respectively.

  logger:
    class: sfAggregateLogger
    param:
      level: debug
      loggers:
        sf_web_debug:
          class: sfWebDebugLogger
          param:
            level: debug
            condition:      SF_WEB_DEBUG
            xdebug_logging: true
        sf_file_debug:
          class: sfFileLogger
          param:
            level: debug
            file: SF_LOG_DIR/%SF_APP%_%SF_ENVIRONMENT%.log
6 Dec
Next → Page 1 of 2