Created
March 21, 2011 20:19
-
-
Save dsyph3r/880141 to your computer and use it in GitHub Desktop.
Blog: Symfony and Doctrine: Behaviors
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function retrieveByKiTableProxy($ki) { | |
$table = $this->getInvoker()->getTable(); | |
return Doctrine_Query::create() | |
->from($table->getComponentName() . ' t') | |
->where('t.ki = ?', $ki) | |
->fetchOne(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DeviceType: | |
actAs: { Timestampable: ~, Kiable: ~ } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Doctrine::getTable('FooTable')->retrieveByKi('the_ki'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment