Php 雄辩的查找表和模型创建
因此,我已经在一个项目上工作了几周了,并且因为这个项目才刚刚接触到拉威尔(到目前为止,我绝对是一个粉丝!),不过今天我遇到了一个问题: 我有一个应用程序,需要根据用户提供的凭据向不同的API发出请求。所有这些任务都是为了提供相同的服务,但方式不同,这让我认为应该有一种很好的面向对象方法来实现这一点。我提出了一些使用战略模式和工厂模式的想法,但它们似乎比Eloquent的简单布局更不雅观 作为一个更好的例子,我的用户可以提供用户名、电子邮件或电话号码(取决于所选API的要求)。到目前为止,我已经制作了4张如下所示的桌子:Php 雄辩的查找表和模型创建,php,mysql,laravel,eloquent,Php,Mysql,Laravel,Eloquent,因此,我已经在一个项目上工作了几周了,并且因为这个项目才刚刚接触到拉威尔(到目前为止,我绝对是一个粉丝!),不过今天我遇到了一个问题: 我有一个应用程序,需要根据用户提供的凭据向不同的API发出请求。所有这些任务都是为了提供相同的服务,但方式不同,这让我认为应该有一种很好的面向对象方法来实现这一点。我提出了一些使用战略模式和工厂模式的想法,但它们似乎比Eloquent的简单布局更不雅观 作为一个更好的例子,我的用户可以提供用户名、电子邮件或电话号码(取决于所选API的要求)。到目前为止,我已经制
用户:id、用户名、passhash
api\u列表:id、api\u名称、api\u表名称、logo\u url
user\u api\u lookup:id、user\u id、api\u list\u id
api1\u表格:id、用户\u api\u查找\u id、电子邮件
这看起来像是做我正在尝试的事情的正确设置吗
更新:
我可能也应该提到我的应用程序是什么样子的
每个用户都有许多API集成,可以通过Eloquent的关系助手访问这些集成
class User
{
...
public function rewardMethods()
{
return $this->hasMany(APIIntegration::class);
}
...
}
希望这能澄清一点请先阅读此文。 本例假设以下语句:
- 一个API可以有多个凭据
- 一个用户可以有多个API
api
表将相当于您的api\u列表
,该列表将保存有关api的信息。在我的示例中(正如我所说,它非常简单),我只添加了一列名称
CREATE TABLE `apis` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
我为这个示例制作了一个简单的users
表:
CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`email` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
然后我创建了一个名为credentials
的新表,它将保存API可以拥有的所有凭据类型。例如:用户名,电子邮件,电话号码<代码>名称列将保存凭证的人性化名称
CREATE TABLE `credentials` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
我选择的方法将涵盖具有多个必需的凭证的api
。这就是为什么我需要一个介于api
和credentials
之间的透视表
CREATE TABLE `api_credential` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`api_id` int(11) NOT NULL,
`credential_id` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
我需要的下一个表是保存api
auser
所拥有的数据的表。这相当于您的user\u api\u lookup
表
CREATE TABLE `api_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`api_id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
最后一个表是保存实际凭证的内容api\u credential\u user
:
CREATE TABLE `api_credential_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`api_credential_id` int(11) NOT NULL,
`value` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
然后,我们需要生成(至少为此我需要一个用户artisan
)我们的模型,并设置它们之间的关系
php artisan make:model Api
php artisan make:model ApiCredential
php artisan make:model ApiCredentialUser
php artisan make:model ApiUser
php artisan make:model Credential
用户
型号随Laravel的安装而来
Api
模型如下所示:
(我添加了对每种关系的评论)
凭证
型号:
class Credential extends Model
{
protected $table = 'credentials';
/**
* Get all apis this credential belongs to.
*/
public function apis()
{
return $this->belongsToMany('App\Api');
}
public function apiCredential() {
return $this->hasOne('App\ApiCredential');
}
}
class ApiCredential extends Model
{
protected $table = 'api_credential';
/**
* Those are defined in the sake of consistency.
* Probably will not be used.
*/
public function api()
{
return $this->belongsTo('App\Api');
}
/**
* Those are defined in the sake of consistency.
* Probably will not be used.
*/
public function credential()
{
return $this->belongsTo('App\Credential');
}
public function user()
{
return $this->hasOne('App\ApiCredentialUser');
}
}
class User extends Model
{
protected $table = 'users';
public function apis()
{
return $this->belongsToMany('App\Api');
}
}
ApiCredential
型号:
class Credential extends Model
{
protected $table = 'credentials';
/**
* Get all apis this credential belongs to.
*/
public function apis()
{
return $this->belongsToMany('App\Api');
}
public function apiCredential() {
return $this->hasOne('App\ApiCredential');
}
}
class ApiCredential extends Model
{
protected $table = 'api_credential';
/**
* Those are defined in the sake of consistency.
* Probably will not be used.
*/
public function api()
{
return $this->belongsTo('App\Api');
}
/**
* Those are defined in the sake of consistency.
* Probably will not be used.
*/
public function credential()
{
return $this->belongsTo('App\Credential');
}
public function user()
{
return $this->hasOne('App\ApiCredentialUser');
}
}
class User extends Model
{
protected $table = 'users';
public function apis()
{
return $this->belongsToMany('App\Api');
}
}
apicredential用户
型号:
class ApiCredentialUser extends Model
{
protected $table = 'api_credential_user';
}
用户
型号:
class Credential extends Model
{
protected $table = 'credentials';
/**
* Get all apis this credential belongs to.
*/
public function apis()
{
return $this->belongsToMany('App\Api');
}
public function apiCredential() {
return $this->hasOne('App\ApiCredential');
}
}
class ApiCredential extends Model
{
protected $table = 'api_credential';
/**
* Those are defined in the sake of consistency.
* Probably will not be used.
*/
public function api()
{
return $this->belongsTo('App\Api');
}
/**
* Those are defined in the sake of consistency.
* Probably will not be used.
*/
public function credential()
{
return $this->belongsTo('App\Credential');
}
public function user()
{
return $this->hasOne('App\ApiCredentialUser');
}
}
class User extends Model
{
protected $table = 'users';
public function apis()
{
return $this->belongsToMany('App\Api');
}
}
用于演示目的的虚拟数据:
INSERT INTO `users` (`id`, `email`) VALUES (NULL, 'test@user.com'), (NULL, 'test2@user.com');
INSERT INTO `credentials` (`id`, `name`) VALUES (NULL, 'phone_number'), (NULL, 'username'), (NULL, 'email');
INSERT INTO `apis` (`id`, `name`) VALUES (NULL, 'Github API'), (NULL, 'StackOverflow API'), (NULL, 'Twitter API'), (NULL, 'Facebook API'), (NULL, 'LinkedIn API');
INSERT INTO `api_credential` (`id`, `api_id`, `credential_id`) VALUES (NULL, '1', '1'), (NULL, '2', '2'),(NULL, '3', '3'), (NULL, '4', '1'),(NULL, '5', '2');
INSERT INTO `api_user` (`id`, `api_id`, `user_id`) VALUES (NULL, '1', '1'), (NULL, '2', '2');
INSERT INTO `api_credential_user` (`id`, `api_credential_id`, `value`) VALUES (NULL, '1', '0700-1111-2222'), (NULL, '2', 'myUsername');
这些关系的示例用法:
Route::get('/test', function () {
//Fetching all users (dummy data)
$user_1 = App\User::find(1);
$user_2 = App\User::find(2);
//To list every user's apis
echo '<h3>User: ' . $user_1->email . '</h3>';
echo 'Apis attached: <br />';
echo '<ul>';
foreach ($user_1->apis as $api) {
echo '<li>' . $api->name . '</li>';
//You can get required credentials per api.
echo '<ul>';
foreach ($api->credentials as $credential) {
//And for every credential you can get user's value.
echo '<li><b>Name:</b> ' . $credential->name . ' - <b>User value</b>: ' . $credential->apiCredential->user->value . '</li>';
}
echo '</li></ul>';
}
echo '</ul>';
//To list every user's apis
echo '<h3>User: ' . $user_2->email . '</h3>';
echo 'Apis attached: <br />';
echo '<ul>';
foreach ($user_2->apis as $api) {
echo '<li>' . $api->name . '</li>';
//You can get required credentials per api.
echo '<ul>';
foreach ($api->credentials as $credential) {
//And for every credential you can get user's value.
echo '<li><b>Name:</b> ' . $credential->name . ' - <b>User value</b>: ' . $credential->apiCredential->user->value . '</li>';
}
echo '</li></ul>';
}
echo '</ul>';
//To list all apis
echo '<h3>All apis:</h3>';
$apis = \App\Api::all();
foreach ($apis as $api) {
echo $api->name . '<br />';
}
//To list all credentials:
echo '<h3>All credentials:</h3>';
$credentials = \App\Credential::all();
foreach ($credentials as $credential) {
echo $credential->name . '<br />';
}
});
路由::获取('/test',函数(){
//获取所有用户(虚拟数据)
$user_1=App\user::find(1);
$user_2=App\user::find(2);
//列出每个用户的API
回显“用户:”.$User_1->电子邮件。“;
echo“附加API:
”;
回声“”;
foreach($user_1->api作为$api){
回显“- ”.$api->name.“
”;
//您可以获得每个api所需的凭据。
回声“”;
foreach($api->凭证为$credential){
//对于每个凭证,您都可以获得用户的值。
回显'- 名称:'.$credential->Name.'-用户值:'.$credential->apiCredential->User->value'.
;
}
回音“
”;
}
回声“
”;
//列出每个用户的API
回显“用户:”.$User_2->电子邮件。“;
echo“附加API:
”;
回声“”;
foreach($user_2->api作为$api){
回显“- ”.$api->name.“
”;
//您可以获得每个api所需的凭据。
回声“”;
foreach($api->凭证为$credential){
//对于每个凭证,您都可以获得用户的值。
回显'- 名称:'.$credential->Name.'-用户值:'.$credential->apiCredential->User->value'.
;
}
回音“
”;
}
回声“
”;
//列出所有API
回显“所有API:”;
$Api=\App\Api::all();
foreach($api作为$api){
echo$api->name.“
”;
}
//要列出所有凭据,请执行以下操作:
回显“所有凭据:”;
$credentials=\App\Credential::all();
foreach($credentials作为$credential){
echo$credential->name.“
”;
}
});
上面提供的虚拟数据将给出以下结果:。因此API将需要其中之一:用户名、电子邮件或电话号码?或者它可能需要不止一个?这个
api1_表
是否用于一个api,而下一个api将有自己的数据库表?到目前为止,我们只讨论了需要其中一个api的api。是的,这就是我对apiN_表的想法。当使用像mysql这样的关系数据库时,这不是一个好方法。有没有更好的方法?我一直在看类似的laravel文档,但是还没有发现任何东西。在实现这个示例之后,我发现这一切有点让人困惑,但这并不是说这不是一个很好的方法。相反,我在eloquent中查阅了一些关于多态性的教程,发现了