使用sequelize,我们无法执行包含来自其他模型(Player)的字段的查询,而不包括模型的名称作为属性。
我们想获得这个结果(查看每个对象的最后一个属性:number):
async function getPlayers() {
const playerRole = await sequelize.models.Role.getPlayerRole();
return sequelize.models.User.findAll({
include: [{
association: sequelize.models.User.hasOne(sequelize.models.UserRole),
model: sequelize.models.UserRole,
where: {
club_id: this.clubId,
team_id: this.id,
role_id: playerRole.id
}
}, {
association: sequelize.models.User.hasOne(sequelize.models.Player),
model: sequelize.models.Player,
where: {
team_id: this.id,
},
attributes: ['number']
}]
});
}
相反,我们得到了这个:
for ($i = 1; $i <= $message_count; ++$i)
{
$header = imap_header($imap, $i);
$body = imap_fetchbody($imap, $i,2);
$prettydate = date("jS F Y", $header->udate);
$nom = imap_msgno($imap, $i);
if (isset($header->from[0]->personal)) {
$personal = $header->from[0]->personal;
} else {
$personal = $header->from[0]->mailbox;
}
$subject=$header->Subject;
$email = "$personal <{$header->from[0]->mailbox}@{$header->from[0]->host}>";
echo $nom;
echo '<br><br>';
echo "On $prettydate, $email said \"$body\".\n";
echo '<br><br>';
}
我们涉及4个模型:用户(几乎所有属性),播放器(有号码),角色(包含角色)和UserRole(与用户和角色相关)。
if( !empty($_POST) ){
//--YOUR OTHER CODING--
//...........
$domain_url = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]";
$redirect_url = 'https://example.com/forms/thank-you/'; //-->MUST BE 'https://';
header("Content-type: application/json");
header("Access-Control-Allow-Credentials: true");
header("Access-Control-Allow-Origin: *.ampproject.org");
header("AMP-Access-Control-Allow-Source-Origin: ".$domain_url);
if( isEmailAlreadyExist($_POST['email']) ){ //-->SAMPLE Valiation!
//-->Any 40X status code!
//Reference-1: https://github.com/ampproject/amphtml/issues/6058
//Reference-2: http://php.net/manual/pt_BR/function.http-response-code.php
header("HTTP/1.0 412 Precondition Failed", true, 412);
echo json_encode(array('errmsg'=>'This email already exist!'));
die();
}else{
//--Assuming all validations are good here--
if( empty($redirect_url) ){
header("Access-Control-Expose-Headers: AMP-Access-Control-Allow-Source-Origin");
}else{
header("AMP-Redirect-To: ".$redirect_url);
header("Access-Control-Expose-Headers: AMP-Redirect-To, AMP-Access-Control-Allow-Source-Origin");
}
echo json_encode(array('successmsg'=>'My success message. [It will be displayed shortly(!) if with redirect]'));
die();
}
}
答案 0 :(得分:1)
您可以使用sequelize.col()
,但您应该将raw: true
添加到findAll
调用,否则结果将转换为User
模型实例,因此{无论如何,{1}}属性都会被忽略。
number
我删除了return sequelize.models.User.findAll({
attributes: { include: sequelize.col('Player.number') },
raw: true, // added to prevent returning User instances from the query, only simple JSON data
include: [
{
model: sequelize.models.UserRole,
where: {
club_id: this.clubId,
team_id: this.id,
role_id: playerRole.id
}
},
{
model: sequelize.models.Player,
where: {
team_id: this.id,
},
attributes: []
}
]
});
属性 - 我假设您已经在模型定义中声明了它们,因此在进行查询时不需要每次都使用它们。
必须在Sequelize实例上调用association
方法。