phalcon 笔记(不定期更新)

1. model 初始化配置

public function initialize()
	{
		$this->useDynamicUpdate(true); // 动态更新字段,非全部更新
	}

2.数据库配置,记录查询sql

$di->setShared('db', function () use ($config,$di) {
    $dbConfig = $config->database->toArray();
    $adapter = $dbConfig['adapter'];
    unset($dbConfig['adapter']);

    $class = 'Phalcon\Db\Adapter\Pdo\\' . $adapter;
    /** @var Phalcon\Db\Adapter\Pdo\Mysql $connection */
    $connection =  new $class($dbConfig);
    /** @var Phalcon\Events\Manager $eventsManager */
    $eventsManager = $di->get("eventsManager");
    $logger = new \Phalcon\Logger\Adapter\File(__DIR__."/../logs/debugs.log");
    $eventsManager->attach('db', function($event, $connection) use ($logger) {
        if ($event->getType() == 'beforeQuery') {
            $logger->log($connection->getSQLStatement(), \Phalcon\Logger::INFO);
        }
        if ($event->getType() == 'beforeSave') {
            $logger->log($connection->getSQLStatement(), \Phalcon\Logger::INFO);
        }
    });
    $connection->setEventsManager($eventsManager);

    return $connection;
});

2.禁用或启用特性
https://docs.phalconphp.com/en/latest/reference/models.html#disabling-enabling-features
phalcon的orm有坑,如果数据库某个字段为null,那么update,save的时候会报错,因为phalcon默认检测字段是否Not null。
应该在model将此特性关闭。将notNullValidations设置为false

 

   public function initialize()
    {
        $this->setup(['notNullValidations'=>FALSE]);
    }