我有一个CakePHP锦标赛网站,我需要管理以下条件:
我需要管理比赛分区和比赛时间表的两个竞争者之间会有一场激烈的竞争,而竞争对手的表中的竞争对手和对手将是相同的,意味着相同的用户将相互对立,而不是这个如果我将两个相同的字段(competitor_id)保存到匹配计划表中,管理员也可以管理竞争对手的订单,如果某些竞争对手不可用等等.
我想在另一个表示例中删除所有没有现有外键的行:
table1
+----+-------+
|id | data |
+----+-------+
| 1 | hi |
+----+-------+
| 2 | hi |
+----+-------+
| 3 | hi |
+----+-------+
| 4 | hi |
+----+-------+
| 5 | hi |
+----+-------+
table2
+----+-------+
|a_id| data |
+----+-------+
| 1 | hi |
+----+-------+
| 20 | hi |
+----+-------+
| 3 | hi |
+----+-------+
| 40 | hi |
+----+-------+
| 5 | hi |
+----+-------+
Run Code Online (Sandbox Code Playgroud)
该查询将删除table2上ID为#20和40的行.
我需要这样做,以便我可以建立与table1和table2的关系.
我有三个数据库表:
+------+-----------+---------------------+
| user | user_type | user_type_relations |
+------+-----------+---------------------+
Run Code Online (Sandbox Code Playgroud)
每个用户可以有多种类型,但一种用户类型只能有一个用户。为了存储这种关系,我使用了第三个关系表,其结构如下:
+---------------------+
| user_type_relations |
+---------------------+
| id |
| user_id |
| user_type_id |
+---------------------+
Run Code Online (Sandbox Code Playgroud)
我已经定义了我的模型中的关系,如下所示:
User 模型:
public function userTypeRelations()
{
return $this->hasMany('UserTypeRelations', 'user_id', 'id');
}
Run Code Online (Sandbox Code Playgroud)
UserType 模型:
public function userTypeRelation()
{
return $this->hasMany('UserTypeRelations', 'user_type_id', 'id');
}
Run Code Online (Sandbox Code Playgroud)
UserTypeRelations 模型:
public function user()
{
return $this->hasMany('User', 'id', 'user_id');
}
public function userType()
{
return $this->hasMany('UserType', 'id', 'user_type_id');
}
Run Code Online (Sandbox Code Playgroud)
这就是我在将其传递给视图之前尝试访问控制器中特定用户的用户类型的方式:
$users = User::with('userTypeRelations')->with('userType')->orderBy($order)->where('status', 'active')->paginate(10);
Run Code Online (Sandbox Code Playgroud)
我以为首先我会得到关系表的值,然后我会很容易地得到每个用户的用户类型,但我收到以下错误:
BadMethodCallException
Call to undefined method …Run Code Online (Sandbox Code Playgroud) 我使用 Laravel 5.4 开发了一个电子商务(具有多个产品属性功能)网站。一切正常。但是当我尝试在数据透视表中同步同一属性的多个值时。Laravel 忽略重复的 pares。例如,我有一个名为“网络”的属性,它有 3 个值:2G、3G、4G。手机支持3G和4G网络。我想同步数据库中的 3G 和 4G 值。Laravel 忽略其中之一。
Products Table:
ID - Product Name
1 - Test Mobile
Attributes Table
ID - AttributeName
1 - Network
AttributeValues Table
ID - AttributeID - AttributeValue
1 - 1 - 2G
2 - 1 - 3G
3 - 1 - 4G
ProductAttributes Table
ID - AttributeID - ProductID - AttributeValue
1 - 1 - 1 - 3G
1 - 1 - 1 - 4G
Run Code Online (Sandbox Code Playgroud)
我想将产品属性存储在“ProductAttributes”表中。但是 Laravel 忽略其中之一。
我正在保存这样的数据: …
所以,我有一个 Laravel 网络并有一些表,其中两个是users和user_data。
user_data有一个user_id引用idon的外键users。
users具有用户名、电子邮件、密码等一般属性,并且user_data与usersgame_money、game_job、game_tier等具有 1 对 1 的关系。如果我将这 2 个组合到 1 个表中,列太长,所以我很难规范化它。
用户注册工作顺利。但是我不知道如何user_data在新用户注册时添加新条目并在users. 中的属性列user_data(如 game_money 等)是由 laravel 之外的外部应用程序填充的,所以我只需要user_data.user_id在外键中添加一个条目,并让 user_data 中的其他属性在被更新之前使用默认值或 null外部应用程序。
这是我的user_data迁移:
Schema::create('user_data', function (Blueprint $table) {
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users');
$table->string('account_tier')->default("Free");
$table->float('economy')->default(0);
$table->string('job')->nullable();
$table->timestamps();
});
Run Code Online (Sandbox Code Playgroud)
我应该将插入查询放在 Laravel 中的什么位置?或者我应该处理user_data使用外部应用程序?
非常感谢。
我试图围绕xml架构,我想弄清楚的一件事是如何做关系类型架构,其中元素引用另一个,可能在另一个架构中.我看过xsd:key和xsd:keyref,它看起来像我感兴趣的东西,但我不确定.最初我只是设置类型为xs:ID abd xs:IDREF的属性,就我所知,这显然不一定是指特定元素.
基本上,我有几个不同的xml文件,其中元素引用同一文件或其他文件中的其他元素.它看起来很像一个关系数据库,我很乐意使用它,但要求只使用XML文件,所以我至少试图建立一些理智而不仅仅是看似随机的字符串依赖于xml注释来定义关系.它适用于较小的项目,但它肯定不具备可扩展性.
有什么想法吗?
我有两个名为Position和Event的模型,它们彼此之间具有多对多关系。
<?php
class Event extends Model
{
protected $fillable = [
'name', 'opta_id'
];
public function positions() {
return $this->belongsToMany(Position::class);
}
}
Run Code Online (Sandbox Code Playgroud)
class Position extends Model
{
protected $fillable = [
'name', 'short_name'
];
public function events() {
return $this->belongsToMany(Event::class);
}
}
Run Code Online (Sandbox Code Playgroud)
每个数据库中的event每个position数据库都应有一个透视条目,没有例外。因此,每当用户创建一个新event条目时,我都希望为每个现有条目创建一个透视条目position。
我正在努力使用文档和SO来解决这个问题。我可以通过显式命名db中所有位置的ID来使用sync()或attach()进行连接。在EventController的store方法中:
$data = $request->all();
$event = Event::create($data);
$event->positions()->sync([1, 22, 34, 167]);
Run Code Online (Sandbox Code Playgroud)
但是,要使此方法起作用,我首先必须从positions表中获取所有条目,将它们格式化为ID数组,然后将它们传递给此方法。有任何内置或规范的方法可以做到这一点吗?
我有2个模型:User并且PrivateMessage必须关联(因为用户有许多私人消息作为接收者和发件人,私人消息属于用户)
这是我的private_messages表格结构:
private_messages:
sender_id:integer
reciever_id:integer
title:string
message:text
Run Code Online (Sandbox Code Playgroud)
我很难理解如何为发件人用户和收件人用户连接相同的邮件,现在我的模型代码如下所示:
class User < ActiveRecord:Base
has_many :private_messages
end
Run Code Online (Sandbox Code Playgroud)
和
class PrivateMessage < ActiveRecord::Base
belongs_to :user, :through => :sender_id
belongs_to :user, :through => :reciever_id
end
Run Code Online (Sandbox Code Playgroud)
那是对的吗?
谢谢大家的高级帮助.
我是rails的绝对初学者,我正在努力完成一个教程.
任务如下.我有一个从脚手架构建的帖子模型,只有内容:字符串字段.
然后是一个类别模型,而不是一个脚手架等.这个想法是一个类别has_many:posts和post belongs_to:category.类别具有字段名称和描述.这很好,我理解这一点,我已经将这些添加到模型中.
我也进行了迁移
rails generate migration AddCategoryToPost category:references
Run Code Online (Sandbox Code Playgroud)
我现在如何让用户在发布帖子时添加类别.
因此,事件的顺序是用户创建帖子,他们可以在创建帖子时添加类别.该类别具有用户需要定义的名称和描述.
def new
@post = Post.new
end
def create
@category = Category.new(caregory_params)
@post = Post.new(post_params)
respond_to do |format|
if @post.save
format.html { redirect_to @post, notice: 'Post was successfully created.' }
format.json { render :show, status: :created, location: @post }
else
format.html { render :new }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
Run Code Online (Sandbox Code Playgroud)
如何更改post控制器的新方法,创建方法和更新方法以实现此目的,从而表单应包含什么来创建新帖子(和类别).
非常感谢你在高级方面的帮助,我只是不明白你将如何去做,因为类别需要是一个'对象',需要添加到post对象(需要添加到数据库) .
是否有可能/权利在表之间建立只有一种方式的关系?我有一个invoices表,我需要在其他表中引用喜欢commission_payments,membership_payments但invoices表不需要a commission_payment_id或a membership_payment_id.换句话说,可能发生不同类型的交易,并且它们都可能附有发票,但发票不需要引用这些交易表.
invoices commission_payments membership_payments
--------------- --------------------- ---------------------
-id -id -id
... -invoice_id -invoice_id
... ...
Run Code Online (Sandbox Code Playgroud)
我为每个表创建了Eloquent模型.我在其他两个模型上添加了一个hasOne关系invoices.
class CommissionPayment extends Model{
public function invoice(){
return $this->hasOne('App\Models\Invoice');
}
}
Run Code Online (Sandbox Code Playgroud)
然后我尝试访问Comission Payment的附加发票,如下所示:
$com = CommissionPayment::first();
$com->invoice->id;
Run Code Online (Sandbox Code Playgroud)
然后我得到这个错误:
SQLSTATE[42S22]: Column not found: 1054 Unknown column
'invoices.commission_payment_id' in 'where clause' (SQL: select * from `invoices`
where `invoices`.`commission_payment_id` = 15 and `invoices`.`commission_payment_id` is not
null limit 1)
Run Code Online (Sandbox Code Playgroud)
为什么要 …
entity-relationship database-relations one-to-one has-one laravel
laravel ×5
php ×3
database ×2
mysql ×2
ruby ×2
associations ×1
cakephp ×1
eloquent ×1
events ×1
foreign-keys ×1
has-one ×1
laravel-5.4 ×1
models ×1
one-to-one ×1
orm ×1
schema ×1
sql ×1
xsd ×1