Phalcon:显示花哨的错误消息而不是标准异常

Phalcon: display fancy error message instead of standard exceptions

使用 InnoDb 并在数据库中设置 foreignkey。在 Phalcon 中还设置了虚拟键。由于 Foreign Key 设置,从 phalcon 中删除记录 时出现以下错误 ,并且子表中仍有数据。

My Objective is to display fancy error message to user when this error displayed.

显示错误:

SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (`invoice`.`invoice`, CONSTRAINT `Invoice.CustomerId` FOREIGN KEY (`CustomerId`) REFERENCES `customer` (`Id`))
#0 [internal function]: PDOStatement->execute()
#1 [internal function]: Phalcon\Db\Adapter\Pdo->executePrepared(Object(PDOStatement), Array, Array)
#2 [internal function]: Phalcon\Db\Adapter\Pdo->execute('DELETE FROM `in...', Array, Array)
#3 [internal function]: Phalcon\Db\Adapter->delete(Array, '`Id` = ?', Array, Array)
#4 C:\wamp\www\invoice\app\controllers\CustomerController.php(140): Phalcon\Mvc\Model->delete()
#5 [internal function]: CustomerController->deleteAction('3')
#6 [internal function]: Phalcon\Dispatcher->callActionMethod(Object(CustomerController), 'deleteAction', Array)
#7 [internal function]: Phalcon\Dispatcher->_dispatch()
#8 [internal function]: Phalcon\Dispatcher->dispatch()
#9 C:\wamp\www\invoice\public\index.php(42): Phalcon\Mvc\Application->handle()
#10 {main}

我想为用户显示的花式错误消息:

The customer cannot be deleted because other invoices are using it

这是我的模型:

<?php
    class Customer extends \Phalcon\Mvc\Model
    {
        public $id;
        public $name;
        public $street;
        public $city;
        public $country;
        public $postalCode;
        public $phone;
        public $mobile;
        public $fax;
        public $email;
        public $web;
        public function initialize()
        {
            $this->setSchema("invoice");
            $this->setSource("customer");
            $this->hasMany(
                'Id',
                'Invoice',
                'Id',
                [
                    'alias' => 'Invoice',
                    'foreignKey' => [
                        'message' => 'The customer cannot be deleted because other invoices are using it',
                    ]
                ]
            );
        }
        public static function find($parameters = null)
        {
            return parent::find($parameters);
        }
        public static function findFirst($parameters = null)
        {
            return parent::findFirst($parameters);
        }
        public function getSource()
        {
            return 'customer';
        }

    }
    ?>

完整代码在github中,以备参考。

你的关系写错了。

属于参数:

  • 第一个是当前模型中的属性(列),第二个是相关的 class,第三个是相关模型中的列

hasMany/hasOne 个参数:

  • 第一个是当前模型中的列,第二个是相关的 class,第三个是相关模型中的列

所以在你的情况下应该是 class 客户:

$this->hasMany(
    'id',
    'Invoice',
    'customerId',
    [
        'alias' => 'Invoices', // it's has many relations so better Invoices alias
        'foreignKey' => [
            'message' => 'The customer cannot be deleted because other invoices are using it',
        ]
    ]
);

在发票中 class:

$this->belongsTo('customerId', '\Customer', 'id', ['alias' => 'Customer']);

不确定仅此一项是否能解决您的问题。