er for "model" events $eventsManager->attach('collection', function($event, $robot) {
if ($event->getType() == 'beforeSave') {
if ($robot->name == 'Scooby Doo') {
echo "Scooby Doo isn't a robot!";
return false;
}
}
return true;
});
$robot = new Robots();
$robot->setEventsManager($eventsManager);
$robot->name = 'Scooby Doo';
$robot->year = 1969;
$robot->save();
上面的例子中EventsManager仅在对象和监听器(匿名函数)之间扮演了一个桥接器的角色。如果我们想在创建应用时使用同一个EventsManager,我们需要把这个EventsManager对象设置到
collectionManager服务中:
//Registering the collectionManager service
$di->set('collectionManager', function() {
$eventsManager = new Phalcon\Events\Manager();
// Attach an anonymous function as a listener for "model" events
$eventsManager->attach('collection', function($event, $model) {
if (get_class($model) == 'Robots') {
if ($event->getType() == 'beforeSave') {
if ($model->name == 'Scooby Doo') {
echo "Scooby Doo isn't a robot!";
return false;
}
}
}
return true;
});
// Setting a default EventsManager
$modelsManager = new Phalcon\Mvc\Collection\Manager();
$modelsManager->setEventsManager($eventsManager);
return $modelsManager;
}, true);
实现业务规则(Implementing a Business Rule)?
当插入或更新删除等执行时,模型会检查上面表格中列出的方法是否存在。我们建议定义模型里的验证方法以避免业务逻辑暴露出来。下面的例子中实现了在保存或更新时对年份的验证,年份不能小于0年:
class Robots extends \Phalcon\Mvc\Collection
{
public function beforeSave()
{
if ($this->year < 0) {
echo "Year cannot be smaller than zero!";
return false;
}
}
}
在响应某些事件时返回了false则会停止当前的操作。 如果事实响应未返回任何值, Phalcon\Mvc\Collection 会假定返回了true值。
验证数据完整性(Validating Data Integrity)?
Phalcon\Mvc\Collection 提供了若干个事件用于验证数据和实现业务逻辑。特定的事件中我们可以调用内建的验证器 Phalcon提供了一些验证器可以用在此阶段的验证上。
下面的例子中展示了如何使用:
use Phalcon\Mvc\Model\Validator\InclusionIn,
Phalcon\Mvc\Model\Validator\Numericality;
class Robots extends \Phalcon\Mvc\Collection
{
public function validation()
{
$this->validate(new InclusionIn(
array(
"field" => "type",
"message" => "Type must be: mechanical or virtual",
"domain" => array("Mechanical", "Virtual")
)
));
$this->validate(new Numericality(
array(
"field" => "price",
"message" => "Price must be numeric"
)
));
return $this->validationHasFailed() != true;
}
}
上面的例子使用了内建的”InclusionIn”验证器。这个验证器检查了字段的类型是否在指定的范围内。如果值不在范围内即验证失败会返回false. 下面支持的内验证器:
除了内建的验证器外,我们还可以创建自己的验证器:
class UrlValidator extends \Phalcon\Mvc\Collection\Validator
{
public function validate($model)
{
$field = $this->getOption('field');
$value = $model->$field;
$filtered = filter_var($value, FILTER_VALIDATE_URL);
if (!$filtered) {
$this->appendMessage("The URL is invalid", $field, "UrlValidator");
return false;
}
return true;
}
}
添加验证器到模型:
class Customers extends \Phalcon\Mvc\Collection
{
public function validation()
{
$this->validate(new UrlValidator(array(
"field" => "url",
)));
if ($this->validationHasFailed() == true) {
return false;
}
}
}
创建验证器的目的即是使之在多个模型间重复利用以实现代码重用。验证器可简单如下:
class Robots extends \Phalcon\Mvc\Collection
{
public function validation()
{
if ($this->type == "Old") {
$message = new Phalcon\Mvc\Model\Message(
"Sorry, old robots are not allowed anymore",
"type",
"MyType"
);
$this->appendMessage($message);
return false;
}
return true;
}
}
删除记录(Deleting Records)?
Phalcon\Mvc\Collection::delete()方法用来删除记录条目。我们可以如下使用:
$robot = Robots::findFirst();
if ($robot != false) {
if ($robot->delete() == false) {
echo "Sorry, we can't delete the robot right now: \n";
foreach ($robot->getMessages() as $message)