我对sequelize陌生,我试图通过关联表进行请求
我有一个名为ArrayBuffer
Experience
一秒钟叫做module.exports = function (sequelize, DataTypes) {
const Experience = sequelize.define('experience', {
internalId: {
type: DataTypes.BIGINT,
unique: true,
allowNull: false,
},
label: {
type: DataTypes.STRING,
unique: false,
allowNull: false,
},
picture: {
type: DataTypes.TEXT,
unique: false,
allowNull: true,
},
type: {
type: DataTypes.STRING,
validate: {
isIn: {
args: [[
'generic',
'specific',
]],
msg: 'Must be a valid type',
},
},
unique: false,
allowNull: true,
},
author: {
type: DataTypes.STRING,
unique: false,
allowNull: true,
defaultValue: 'import',
},
isActive: {
type: DataTypes.BOOLEAN,
defaultValue: true,
},
});
Experience.associate = (models) => {
Experience.belongsToMany(models.Tag, {
as: 'Tags',
through: models.ExperienceTag,
});
};
return Experience;
};
Tags
关联表名称为module.exports = function (sequelize, DataTypes) {
const Tag = sequelize.define('tag', {
internalId: {
type: DataTypes.STRING,
unique: true,
allowNull: false,
},
name: {
type: DataTypes.STRING,
unique: false,
allowNull: false,
},
author: {
type: DataTypes.STRING,
unique: false,
allowNull: true,
defaultValue: 'import',
},
isActive: {
type: DataTypes.BOOLEAN,
defaultValue: true,
},
});
Tag.associate = (models) => {
Tag.belongsToMany(models.Experience, {
as: 'Experiences',
through: models.ExperienceTag,
});
};
return Tag;
};
我想让所有拥有ExperienceTags
的{{1}}
这是我的要求:
Experience
但是当我执行它时,会出现类似以下错误:
tagId = 44
答案 0 :(得分:0)
我认为您需要在包含中添加foreach($jsonObjs as $obj) {
echo '<tr>';
foreach($obj as $key => $value){
echo '<td>'.$value.'</td>';
}
echo '</tr>';
}
,因为您已经定义了与别名的关联
更改此
as: 'Experiences'
使用
Experience.findAll({
include: [{
model: ExperienceTag,
where: { tagId: 44 },
}],
})
答案 1 :(得分:0)
我认为您希望添加Tag
而不是ExperienceTag
,以下示例可能会为您提供帮助
Experience.findAll({
include: [{
model: Tag, //model name which you want to include
as: 'Tags', // you have to pass alias as you used while defining
where: { tagId: 44 },
}],
})