Created
October 5, 2012 15:21
-
-
Save phalcon/3840465 to your computer and use it in GitHub Desktop.
This file contains 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
//app/controllers/UserController.php | |
{ | |
//By its integer primary key | |
$user = User::findFirst(7) | |
//By other primary key | |
$user = User::findFirst("code=7"); | |
} | |
{ | |
$this->db->query('SELECT * FROM users WHERE id=?', array(7)); | |
} |
This file contains 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
//app/routes.php | |
$router = new \Phalcon\Mvc\Router(); | |
$router->add("/people", "Users::index"); | |
$router->add("/guy/{email}", "Users::view"); | |
$router->handle(); | |
This file contains 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
$eventsManager = new \Phalcon\Events\Manager(); | |
$di->set('db', function() use ($config, $eventsManager){ | |
$eventManager->attach('db', function($event, $connection) { | |
if ($event->getType() == 'afterQuery') { | |
echo $connection->getSQLStatement(); | |
} | |
}); | |
$connection = new \Phalcon\Db\Adapter\Pdo\Mysql(array( | |
"host" => $config->database->host, | |
"username" => $config->database->username, | |
"password" => $config->database->password, | |
"dbname" => $config->database->name | |
)); | |
$connection->setEventsManager($eventsManager); | |
return $connection; | |
}); |
This file contains 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
use Phalcon\Mvc\Model\Validator\Uniqueness; | |
use Phalcon\Mvc\Model\Validator\Email as EmailValidator; | |
class Users extends Phalcon\Mvc\Model | |
{ | |
public function validation() | |
{ | |
$this->validate(new Uniqueness(array( | |
"field" => "email", | |
"message" => "This email is already used." | |
))); | |
$this->validate(new EmailValidator(array( | |
'field' => 'email', | |
"message" => "Enter a valid email." | |
))); | |
return $this->validationHasFailed() != true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment