Php 完整性约束冲突:重复条目'*****';对于键';初级';
我正在使用zend框架进行一个项目,我正在设计一个简单的注册表。我在phpmyadmin中创建了一个mysql数据库,列“tel”(电话号码)作为主键。在浏览器上测试表单时,我遇到以下问题 提交时,我收到错误 而细节实际上是保存的 我假设zend框架强制使用“id”作为主键,但我的表上没有id。那么,我如何验证或设置主键呢? 或任何其他解决方案,谢谢 模型文件Php 完整性约束冲突:重复条目'*****';对于键';初级';,php,zend-framework3,Php,Zend Framework3,我正在使用zend框架进行一个项目,我正在设计一个简单的注册表。我在phpmyadmin中创建了一个mysql数据库,列“tel”(电话号码)作为主键。在浏览器上测试表单时,我遇到以下问题 提交时,我收到错误 而细节实际上是保存的 我假设zend框架强制使用“id”作为主键,但我的表上没有id。那么,我如何验证或设置主键呢? 或任何其他解决方案,谢谢 模型文件 <?php namespace Registration\Model; class Registration {
<?php
namespace Registration\Model;
class Registration
{
public $fullName;
public $userName;
public $email;
public $tel;
public $gender;
public $password;
public $confirmPassword;
public function exchangeArray(array $data)
{
$this->fullName = ($data['fullName']) ? $data['fullName'] : null;
$this->userName = ($data['userName']) ? $data['userName'] : null;
$this->email = ($data['email']) ? $data['email'] : null;
$this->tel = ($data['tel']) ? $data['tel'] : null;
$this->gender = ($data['gender']) ? $data['gender'] : !null;
$this->password = ($data['password']) ? $data['password'] : null;
$this->confirmPassword = ($data['confirmPassword']) ? $data['confirmPassword'] : null;
}
}
<?php
namespace Registration\Model;
use Zend\Db\TableGateway\TableGatewayInterface;
class RegistrationTable
{
private $tableGateway;
public function __construct(TableGatewayInterface $tableGateway)
{
$this->tableGateway = $tableGateway;
}
public function saveRegistration(Registration $register)
{
$data = [
'fullName' => $register->fullName,
'userName' => $register->userName,
'email' => $register->email,
'tel' => $register->tel,
'gender' => $register->gender,
'password' => $register->password,
'confirmPassword' => $register->confirmPassword,
];
$this->tableGateway->insert($data);
$this->tableGateway->update($data);
}
}
使用电话号码作为主键根本不是一个好主意。如果要确保没有多个用户使用同一部手机,只需添加一个
unique
约束即可
顺便问一下,为什么要同时插入和更新行?或者,为什么总是插入一行,而这可能只是一个更新
我的建议是:不要用手机作为主键。向模型中添加id值
型号
名称空间注册\模型;
班级注册
{
公共$registrationId;
//…以及所有其他领域
公共函数exchangeArray(数组$data)
{
$this->registrationId=($data['registrationId'])?$data['registrationId']为空;
//以及所有其他领域
}
型号表
使用电话号码作为主键根本不是一个好主意。如果要确保没有多个用户使用同一部电话,只需向其添加一个唯一的
约束即可
顺便问一下,为什么要同时插入和更新行?或者,为什么总是插入一行,而这可能只是一个更新
我的建议是:不要用手机作为主键。给你的模型添加一个id值
型号
名称空间注册\模型;
班级注册
{
公共$registrationId;
//…以及所有其他领域
公共函数exchangeArray(数组$data)
{
$this->registrationId=($data['registrationId'])?$data['registrationId']为空;
//以及所有其他领域
}
型号表
感谢您的回复。我在phpmyadmin中对db上的“tel”添加了唯一约束,但没有起作用。或者,如果我要在zend中的代码模型中添加约束,我如何才能做到plzconcerningid,类型为INT和AUTO_INCREMENT?感谢您的回复。我在phpmyadmin中对db上的“tel”添加了唯一约束,但没有起作用。或者如果我要在zend中的代码模型中添加约束,我该如何做呢?plzconcerningid的类型是INT和AUTO_INCREMENT?