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

时间:2017-08-28 13:43:06

标签: php mysql foreign-keys innodb phalcon

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

  

我的目标是在出现此错误时向用户显示奇特的错误消息   显示。

显示错误:

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}

想要为用户显示的花哨错误消息:

  

由于其他发票正在使用

,因此无法删除客户

这是我的模型:

<?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中找到,以供参考。

1 个答案:

答案 0 :(得分:0)

你写错了关系。

belongsTo参数:

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

hasMany / hasOne参数:

  • 首先是当前模型中的列,第二个是相关类,第三个是相关模型中的列

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

$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',
        ]
    ]
);

在Invoice类中:

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

不确定这是否会解决您的问题。