Skip to content

Instantly share code, notes, and snippets.

@phalcon
Created October 5, 2012 15:21
Show Gist options
  • Save phalcon/3840465 to your computer and use it in GitHub Desktop.
Save phalcon/3840465 to your computer and use it in GitHub Desktop.
//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));
}
//app/routes.php
$router = new \Phalcon\Mvc\Router();
$router->add("/people", "Users::index");
$router->add("/guy/{email}", "Users::view");
$router->handle();
$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;
});
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