相关问题:多值字段是个好主意?
我知道多值字段类似于多对多关系.在MS Access应用程序中替换多值字段的最佳方法是什么?我有一个具有多值字段的应用程序.我不确定如何完全取消这些并以单值字段的形式实现完全相同的逻辑?
当我想将多值关系移动到单值关系时,表关系方面的实现是什么.
谢谢.
我有以下数据库方案:
table 'products'
id
category_id
Run Code Online (Sandbox Code Playgroud)
当然还有一个类别表,只有一个id.
数据看起来像这样:
Products
--------------------
| id | category_id |
--------------------
| 0 | 1 |
| 1 | 1 |
| 2 | 1 |
| 3 | 2 |
| 4 | 2 |
| 5 | 1 |
--------------------
Run Code Online (Sandbox Code Playgroud)
我想选择一个类别(例如类别1),因此我在product-repository类中选择该类别中的所有行:
return $this
->createQueryBuilder('u')
->andWhere('u.category = :category')
->setMaxResults(1)
->setParameter('category', $category->getId())
->getQuery()
->getSingleResult()
;
Run Code Online (Sandbox Code Playgroud)
我现在如何选择随机产品?另外:是否有可能通过关系来解决这个问题?
我在实体"类别"和"产品"之间有一个OneToMany关系,所以我也可以通过category-> getProducts()得到所有产品......
任何帮助都非常有用,谢谢
我的印象是,当我在模型中使用belongsTo关系时,它将返回对象,但我似乎只获得了id。应该发生这种情况吗?这样做有什么好处?
这是我的代码:
从我的照片模型
public function album()
{
return $this->belongsTo('Album', 'album');
}
Run Code Online (Sandbox Code Playgroud)
还有我的PhotosController
$photo = Photo::find($id);
$album = $photo->album;
return 'albums/' . $album->folder . '/thumbs/' . $photo->file;
Run Code Online (Sandbox Code Playgroud)
不要介意回报,它只是用于测试。我收到一个错误:
Trying to get property of non-object
Run Code Online (Sandbox Code Playgroud)
和var_dump()显示,所有我得到的是这张专辑的ID字符串
我在解决 Laravel 关系时遇到了一些麻烦。在我的应用程序中,用户和想法之间存在一对多的关系。(用户可能有多种想法。)我正在使用 Ardent。
这是我的用户模型:
使用 Illuminate\Auth\UserTrait;
使用 Illuminate\Auth\UserInterface;
使用 Illuminate\Auth\Reminders\RemindableTrait;
使用 Illuminate\Auth\Reminders\RemindableInterface;
使用 LaravelBook\Ardent\Ardent;
class User 扩展 Ardent 实现 UserInterface、RemindableInterface {
使用 UserTrait、RemindableTrait;
/**
* 模型使用的数据库表。
*
* @var 字符串
*/
受保护的 $table = '用户';
/**
* 从模型的 JSON 形式中排除的属性。
*
* @var 数组
*/
protected $hidden = array('password', 'remember_token');
protected $fillable = array('first_name', 'last_name', 'email', 'password');
公共 $validation_errors;
公共 $autoPurgeRedundantAttributes = true;
公共 $autoHashPasswordAttributes = true;
公共 $autoHydrateEntityFromInput = true;
public static $passwordAttributes = array('password');
公共静态 $rules = … 我正在尝试使用 EF Code First 来做到这一点:

有区域两个表:用户和区域。一个用户属于一个所需的区域,一个区域可以有零个或一个用户(作为管理员)。然后:
用户 *..1 区域和用户 0..1 区域
用户类:
public class User {
public int UserId { get; set; }
public string Name { get; set; }
[ForeignKey("Area")]
public int AreaId { get; set; }
[InverseProperty("Users")]
public virtual Area Area { get; set; }
public virtual Area AreaTitular { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
区域类:
public class Area {
public int AreaId { get; set; }
public string Name { get; set; }
public List<User> Users { get; …Run Code Online (Sandbox Code Playgroud) entity-framework one-to-one one-to-many code-first relationships
我正在尝试根据当前日期配置两个对象之间的关系。假设我有一个Person对象和一堆Event对象的关系。如果Event对象上有一个DateTime( start),我想与今天的所有事件建立关系。
到目前为止,我有:
class Person:
id = Column(Integer, primary_key=True)
todays_events = relationship('Event', primaryjoin='and_(Person.id == Event.person_id, cast(Event.start, Date) == "2016-04-23"')
Run Code Online (Sandbox Code Playgroud)
这有效,但我找不到我需要用“2016-04-23”替换日期字符串以获得等效的CURDATE().
有谁知道我在找什么?
谢谢。
我有两个具有多对多关系的主表和一个数据透视表。
模型“类型”
public function attributes()
{
return $this->belongsToMany('App\Attribute', 'attribute_type');
}
Run Code Online (Sandbox Code Playgroud)
模型“属性”
public function types()
{
return $this->belongsToMany('App\Type', 'attribute_type');
}
Run Code Online (Sandbox Code Playgroud)
数据透视表“attribute_type”
Schema::create('attribute_type', function (Blueprint $table) {
$table->increments('id');
$table->integer('type_id')->unsigned();
$table->foreign('type_id')->references('id')->on('types');
$table->integer('attribute_id')->unsigned();
$table->foreign('attribute_id')->references('id')->on('attributes');
});
Run Code Online (Sandbox Code Playgroud)
我想以随机顺序显示 5 个属于 id < 10 类型的属性。
$attributes = Attribute::whereHas('types', function ($query) {
$query->where('id', '<', '10');
})->orderByRaw("RAND()")->limit(5);
Run Code Online (Sandbox Code Playgroud)
例如
$attribute->id
Run Code Online (Sandbox Code Playgroud)
给我“未定义的属性:Illuminate\Database\Eloquent\Builder::$id”
我知道之前在 Stack Overflow 上已经问过这个问题,但答案并没有以我可以解释的方式为我做。我的一般方法受到本教程的启发。
我想要做的是创建一个非常简单的模型来为用户添加好友,通过一条记录在两端创建等效的好友关系。
在数据库级别,我只有一个“friendships”表,其中只有一个 user_id、一个friend_id 和一个 is_pending 布尔列。
在 user.rb 中,我将关系定义为:
has_many :friendships
has_many :friends, through: :friendships
Run Code Online (Sandbox Code Playgroud)
在friendship.rb 中,我将这种关系定义为:
belongs_to :user
belongs_to :friend, :class_name => 'User'
Run Code Online (Sandbox Code Playgroud)
如果我添加好友,我可以访问如下:
> a = User.first
> b = User.last
> Friendship.new(a.id, b.id)
> a.friends
=> #<User b>
Run Code Online (Sandbox Code Playgroud)
这是完美的,但我想要的是也能够像这样朝另一个方向前进:
> b.friends
Run Code Online (Sandbox Code Playgroud)
不幸的是,按照原样定义的关系,我得到了一个空集合。运行的 SQL 显示它正在搜索 user_id = b.id。我如何指定它还应该搜索friend_id = b.id?
我的查询是这样的:
<?php
public function getListReviews()
{
$reviews = Review::where('user_id', auth()->user()->id)
->get();
return $reviews;
}
Run Code Online (Sandbox Code Playgroud)
从查询中,它可以按ID获取所有评论数据
我想要获取用户照片,商店照片和产品照片
我想用口才:关系
我如何通过口才:关系?
我的审查模式是这样的:
<?php
namespace App;
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
use Jenssegers\Mongodb\Eloquent\HybridRelations;
class Review extends Eloquent
{
use HybridRelations;
protected $connection = 'mongodb';
protected $fillable = ['user_id', 'user_name', 'product_id', 'product_name', 'store_id', 'store_name', 'invoice_number', 'rating', 'comments', 'created_at', 'updated_at'];
public function user()
{
return $this->belongsTo(User::class);
}
}
Run Code Online (Sandbox Code Playgroud)
我的用户模型是这样的:
<?php
namespace App;
...
class User extends Authenticatable
{
...
protected $fillable = ['name', 'email', 'password', 'birth_date', 'mobile_number', …Run Code Online (Sandbox Code Playgroud) 在我的hyperledger作曲家应用程序中,我有客户和顾问.客户对已添加到其"readAccessList"中的顾问具有读取权限.
以下是这两种参与者类型的模型:
participant Client identified by id {
o String id
o String name
--> Consultant[] readAccessList optional
}
participant Consultant identified by id {
o String id
o String name
o String text
}
Run Code Online (Sandbox Code Playgroud)
将顾问添加到客户端的readAccessList的事务定义如下:
transaction AddToReadableConsultantsOfClient {
--> Client client
--> Consultant[] newReadableConsultants
}
Run Code Online (Sandbox Code Playgroud)
此事务的事务处理器功能如下:
/**
* transaction AddToReadableConsultantsOfClient
* @param {org.comp.myapp.AddToReadableConsultantsOfClient} transaction
* @transaction
*/
async function addToReadableConsultantsOfClient(transaction) {
// Save the old version of the client:
const clientOld = transaction.client;
// Update the client with the …Run Code Online (Sandbox Code Playgroud) relationships ×10
php ×5
laravel ×4
eloquent ×3
activerecord ×1
code-first ×1
database ×1
doctrine ×1
entity ×1
laravel-5.3 ×1
many-to-many ×1
ms-access ×1
multivalue ×1
mysql ×1
one-to-many ×1
one-to-one ×1
oop ×1
python ×1
ruby ×1
sqlalchemy ×1
symfony ×1