Skip to content

Instantly share code, notes, and snippets.

@aur1mas
Created May 5, 2012 05:03
Show Gist options
  • Save aur1mas/2599923 to your computer and use it in GitHub Desktop.
Save aur1mas/2599923 to your computer and use it in GitHub Desktop.
example how not to use Active Record pattern
<?php
/**
* user model
*
* @package User
* @author aur1mas <aur1mas@devnet.lt>
*/
class Model_User extends Base_User
{
/**
* returns user primary key
*
* @return int
* @author aur1mas <aur1mas@devnet.lt>
*/
public function getId()
{
return $this->_id;
}
/**
* finds user by its username
*
* @param string $username
* @return Model_User
* @author aur1mas <aur1mas@devnet.lt>
*/
public function findByUsername($username)
{
return Doctrine::getTable("Model_User")->findOneByUsername((string)$username);
}
/**
* returns user screen name
*
* @return string
* @author aur1mas <aur1mas@devnet.lt>
*/
public function getScreeName()
{
return $this->_first_name . " " . $this->_last_name;
}
/**
* user authentication
*
* @param string $username
* @param string $password
* @throws Exception
* @return Model_User
*/
public static function authenticate($username, $password)
{
$user = Doctrine::getTable('Model_User')->findOneByEmail($username);
if ($user instanceof Model_User && $user->password === sha1($password . $this->_salt)) {
return $user;
}
throw new Zend_Controller_Action_Exception("Wrong credentials");
}
}
<?php
/**
* view helper which creates user screename
*
* @package App_View
* @author aur1mas <aur1mas@devnet.lt>
*/
class App_View_Helper_ScreeName extends Zend_View_Helper_Abstract
{
/**
* returns user screen name
*
* @param Model_User $user
* @return string
* @author aur1mas <aur1mas@devnet.lt>
*/
public function screeName(Model_User $user)
{
return $user->first_name . " " . $user->last_name;
}
}
<?php
/**
* service to work with user data
*
* @package User
* @author aur1mas <aur1mas@devnet.lt>
*/
class Service_User
{
/**
* finds user by its username
*
* @param string $username
* @return Model_User
* @author aur1mas <aur1mas@devnet.lt>
*/
public function findByUsername($username)
{
return Doctrine::getTable("Model_User")->findOneByUsername((string)$username);
}
/**
* user authentication
*
* @param string $username
* @param string $password
* @throws Zend_Controller_Action_Exception
* @return Model_User
*/
public static function authenticate($username, $password)
{
$user = Doctrine::getTable('Model_User')->findOneByEmail($username);
if ($user instanceof Model_User && $user->password === sha1($password . $this->_salt)) {
return $user;
}
throw new Zend_Controller_Action_Exception("Wrong credentials");
}
}
<?php
/**
* user model
*
* @package User
* @author aur1mas <aur1mas@devnet.lt>
*/
class Model_User extends Base_User
{
/**
* returns user primary key
*
* @return int
* @author aur1mas <aur1mas@devnet.lt>
*/
public function getId()
{
return $this->_id;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment