Skip to content

Instantly share code, notes, and snippets.

@samsonasik
Created June 30, 2013 15:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save samsonasik/5895710 to your computer and use it in GitHub Desktop.
Save samsonasik/5895710 to your computer and use it in GitHub Desktop.
join table strategy ?
Join Table scenario Proposal
----------------------------
We have two table named *Album* and *Track* that joined at some function at *Album* table model or *Track* table model.
We can't make a standarditation of where join should comes, because the answer is 'depend' of the case.
so, we need a better scenario that unique, simple, and standardize at this project.
1. create an Album class model
class Album
{
public $id;
public $artist;
public $title;
}
2. create a Track class model
class Track
{
public $id;
public $title;
public $album_id;
}
wow, we have same columns!!! we need to make a function to get other(s) :
class Album
implements ServiceLocatorAwareInterface
{
public $id;
public $artist;
public $title;
public function getTrackById($id)
{
$track = $this->getServiceLocator()
->get('My\Model\TrackTable')
->getTableGateway()
->select(array('album_id' => $id));
return $track;
}
public function setServiceLocator(){ /*...*/}
public function getServiceLocator(){/* ... */}
}
so we can do this when echo-ing join :
foreach($album->getTrackById($album->id) as $row)
{
echo $row->id; //this is a track id
}
OR if we just need a current track id, we can do
$album->getTrackById($album->id)->current()->id;
The problem with it is both *Album* and *Track* model is not registered at serviceManager which can inject ServiceLocatorAwareInterface, for auto invokabling it,
we need to make an 'interface' that can be utilized to 'initialized' the service, for example :
interface TableModelInterface
{
}
just it!, and we apply to Album/Track class :
class Album implements ServiceLocatorAwareInterface, TableModelInterface { ... }
and we can create abstract_factories that auto registering service when not registered :
class CommonModelAbstractFactory implements AbstractFactoryInterface
{
public function canCreateServiceWithName(ServiceLocatorInterface $locator, $name, $requestedName)
{
return (bool) (class_exists($requestedName) && $requestedName instanceof \TableModelInterface);
}
public function createServiceWithName(ServiceLocatorInterface $locator, $name, $requestedName)
{
$class = $requestedName;
return new $class;
}
}
how ?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment