Loadsys

LoadStr - A Loadsys Blog

A Web Development Company Specializing In Standard-Based Web Design and the CakePHP Framework.

Posts Tagged ‘CakePHP’

Loadsys to Give Away Tech Books Twice a Month

Monday, August 30th, 2010

We have decided to start pushing education and different technologies by giving away educational material on every 1st and 15th of the month.  There is no catch, but be sure to read the directions below on how to enter.

Book topics that will be included in the giveaway are:

  • CakePHP
  • Ajax (in particular JQuery)
  • MySQL
  • No-SQL Databases (ie. Mongo, Cassandra, Couch DB)
  • PHP
  • Zend Framework
  • Project Management
  • (Possibly More)

To be included in the drawing for each part of the month, you simply need to post a tweet that mentions Loadsys (@loadsys) and tag it with CakePHP (#cakephp).  It would also be helpful to follow us so you can get notified when we draw the name.  We will only ask for more information privately when we need to ship out the book.

Although it won’t help you, clever or off-the-wall tweets  are perfectly fine.  Just make the mention and the tag.  Be sure to let all of your friends on Twitter know if they are interested in winning.

(Example Tweet:  @loadsys Please Please Please give me a #CakePHP book. )

Drawings will be done at Noon Central Standard Time (-5 GMT / -6 GMT).   That means tweets performed between 12:01 on the 1st through 11:59:59 on the 15th will be in the 15th drawing…for example.

Good Luck!

Automagically setting user ID of record creator and modifier in CakePHP 1.2

Friday, May 2nd, 2008

When designing a system for multiuser environment, it is always important to know who created and who modified a record. When using proper field names, such as created and modified, CakePHP automatically sets the proper dates values. We wanted to use the same principal for automatically setting user ID of who created and modified a record. We created a standard for ourself by naming the fields creator_id and modifier_id which goes together with Cake’s date fields created and modified.

All of our projects use as much of standard CakePHP libraries as possible. That’s why all of our CakePHP projects use AuthComponent for authentication.

The first thing we needed to do to was to create ability to access ID of a currently logged in user anywhere in the application. This was achieved by overloading the core AuthComponent:

controllers/components/loadsys_auth.php

<?php

uses('controller/components/auth');

class LoadsysAuthComponent extends AuthComponent {

        function initialize(&$controller) {
                ClassRegistry::addObject('LoadsysAuthComponent', $this);
                parent::initialize($controller);
        }

}

class LoadsysAuth {

        function &getInstance() {
                static $instance = array();
                if (!$instance) {
                        $instance[0] =& ClassRegistry::getObject('LoadsysAuthComponent');
                }
                return $instance[0];
        }

        static function getUser() {
                $_this =& LoadsysAuth::getInstance();
                return $_this->user();
        }

        static function getUserId() {
                $_this =& LoadsysAuth::getInstance();
                $user = $_this->user();
                return Set::extract($user, 'User.id');
        }

        static function getUserGroupId() {
                $_this =& LoadsysAuth::getInstance();
                $user = $_this->user();
                return Set::extract($user, 'User.user_group_id');
        }
}
?>

This structure will allow us to access user ID and user group ID from anywhere in the application by calling LoadsysAuth::getUserId() and LoadsysAuth::getUserGroupId(). This could be applied to many situations.

Now we need to include the component in our project:

app_controller.php

class AppController extends Controller {
	...
	var $components = array('LoadsysAuth');

	...
}

In order to set the creator_id and modifier_id for, we will have to put it in the base class:

app_model.php

<?php

class AppModel extends Model {
        function beforeSave() {
                $exists = $this->exists();
                if ( !$exists && $this->hasField('creator_id') && empty($this->data[$this->alias]['creator_id']) ) {
                        $this->data[$this->alias]['creator_id'] = LoadsysAuth::getUserId();
                }
                if ( $this->hasField('modifier_id') && empty($this->data[$this->alias]['modifier_id']) ) {
                        $this->data[$this->alias]['modifier_id'] = LoadsysAuth::getUserId();
                }
                return true;
        }
}

?>

In your application models, if you need to override the beforeSave callback, make sure you call the parent function:

Example:

create table articles (
 id int unsigned not null auto_increment,
 title varchar(255) not null,
 article varchar(255) not null,
 created datetime,
 creator_id int unsigned,
 modified datetime,
 modifier_id int unsigned,
 primary key(id)
);

models/article.php

<?php

class Article extends AppModel {
	...

	function beforeSave() {
		...
		return parent::beforeSave();
	}

	...
}

?>
Close
E-mail It