Skip to content

Instantly share code, notes, and snippets.

@dsyph3r
Created March 21, 2011 20:19
Show Gist options
  • Save dsyph3r/880141 to your computer and use it in GitHub Desktop.
Save dsyph3r/880141 to your computer and use it in GitHub Desktop.
Blog: Symfony and Doctrine: Behaviors
public function retrieveByKiTableProxy($ki) {
$table = $this->getInvoker()->getTable();
return Doctrine_Query::create()
->from($table->getComponentName() . ' t')
->where('t.ki = ?', $ki)
->fetchOne();
}
DeviceType:
actAs: { Timestampable: ~, Kiable: ~ }
class KiableListener extends Doctrine_Record_Listener {
public function preSave(Doctrine_Event $event) {
// Get the object that invoke the listener
$invoker = $event->getInvoker();
if ($invoker->isNew() && !strlen($invoker->getKi())) {
// Get the name
$name = $invoker->getName();
// Slug the name for the ki (replace spaces with _, remove special chars, etc)
$ki = Slugify::makeSlug($name);
$invoker->setKi($ki);
}
}
}
class Kiable extends Doctrine_Template {
public function setTableDefinition() {
// Add the field
$this->_table->setColumn('ki', 'string', 50, array('notnull' => true, 'unique' => true), true);
// Add the listener
$this->addListener(new KiableListener());
}
public function setup() {
}
}
Doctrine::getTable('FooTable')->retrieveByKi('the_ki');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment