我正在尝试更新具有两个主键的Model.
模型
namespace App;
use Illuminate\Database\Eloquent\Model;
class Inventory extends Model
{
/**
* The table associated with the model.
*/
protected $table = 'inventories';
/**
* Indicates model primary keys.
*/
protected $primaryKey = ['user_id', 'stock_id'];
...
Run Code Online (Sandbox Code Playgroud)
移民
Schema::create('inventories', function (Blueprint $table) {
$table->integer('user_id')->unsigned();
$table->integer('stock_id')->unsigned();
$table->bigInteger('quantity');
$table->primary(['user_id', 'stock_id']);
$table->foreign('user_id')->references('id')->on('users')
->onUpdate('restrict')
->onDelete('cascade');
$table->foreign('stock_id')->references('id')->on('stocks')
->onUpdate('restrict')
->onDelete('cascade');
});
Run Code Online (Sandbox Code Playgroud)
这是应该更新Inventory模型的代码,但它没有.
$inventory = Inventory::where('user_id', $user->id)->where('stock_id', $order->stock->id)->first();
$inventory->quantity += $order->quantity;
$inventory->save();
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
Illegal offset type
Run Code Online (Sandbox Code Playgroud)
我也尝试使用updateOrCreate()方法.它不起作用(我得到同样的错误).
谁能告诉我们应该如何更新带有两个主键的Model?
我试图通过获取dict
元素新对象来设置每个字典元素的类定义.
例:
types = {}
types[first_type] = FirstType()
types[second_type] = SecondType()
Run Code Online (Sandbox Code Playgroud)
通过设置types[first_type]
我必须得到new FirstType()
:
some_var = new types[first_type] # this is illegal statement.
Run Code Online (Sandbox Code Playgroud)
我怎样才能在Python中实现这一目标?
出于好奇:
如果使用了auto
类型说明符,编译时间会有很大的不同吗?
我尚未找到任何有关此的信息。
我刚刚开始使用 Symfony4 / Doctrine2。因此,我需要实现 Doctrine2 自定义NamingStrategy
,但除此之外找不到任何文档/样本: https: //www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/namingstrategy.html以及这个答案如何在 symfony2 中设置表前缀。他们没有解释如何在与 Symfony4 相关的方面执行此操作。我知道我需要创建一个实现NamingStrategy
接口的类,所以我应该将该类放在 Symfony4 文件夹结构中的什么位置src/Entity
?
然后我需要将类注册为服务,对于经验丰富的 Symfony 开发人员来说一定很容易知道,但由于我刚刚开始,对此进行一些详细说明会很高兴。谢谢,任何意见都会受到赞赏。
我有一个服务:
export default class WorldService {
constructor(worldModel) {
this.worldModel = worldModel;
}
async getWorlds() {
let resData = {};
await this.worldModel.find({}, (err, worlds) => {
resData = worlds;
});
return resData;
}
}
Run Code Online (Sandbox Code Playgroud)
我有一个猫鼬模型:
import mongoose from 'mongoose';
const worldSchema = mongoose.Schema({
name: {
type: String,
required: true,
},
});
export default mongoose.model('World', worldSchema);
Run Code Online (Sandbox Code Playgroud)
我想模拟 Mongoose 模型并测试名为getWorlds()
. 我在我的项目中使用Jest作为测试框架。
我尝试在 Jest 中编写单元测试:
import WorldModel from '../../../src/models/world';
import WorldService from '../../../src/services/world';
describe('When …
Run Code Online (Sandbox Code Playgroud) auto ×1
c++ ×1
c++11 ×1
dictionary ×1
doctrine-orm ×1
eloquent ×1
java ×1
javascript ×1
jestjs ×1
laravel ×1
laravel-5 ×1
main-method ×1
mongodb ×1
mongoose ×1
node.js ×1
php ×1
python ×1
python-2.7 ×1
python-3.x ×1
symfony4 ×1