这是我在控制器中的代码:
$retrievedMessages = $messages->getUserContactMessages($userId, $offset, Messages::MESSAGE_PER_PAGE);
$messagesRefId = array_column($retrievedMessages, 'ref_id');
foreach ($messagesRefId as $refId) {
$messagesId = Messages::getTargetedContactMessageIds($refId);
foreach ($messagesId as $messageId) {
$messagesContactModel = new MessagesContact();
$messagesStatus = $messagesContactModel->getTargetedContactMessageStatus($messageId);
}
}
我打印了一些变量,这就是我得到的:
[17-Sep-2015 18:37:00 UTC] <pre>messagesRefIdArray
(
[0] => 6b7f5d06
[1] => 4fdd5045
[2] => 394824ab
[3] => dc56937a
[4] => 84048d4b
[5] => 3514d1e0
[6] => 7437f96c
[7] => fc7d9e2a
[8] => 180faa9b
[9] => b625a6cb
[10] => 98c07527
[11] => 1441b0b2
[12] => 835245c6
[13] => 89b7324f
[14] => e23be50b
[15] => 6f8096f2
[16] => d94de77f
[17] => 7f8660d8
[18] => 2df463cc
[19] => 6a98024f
)
</pre>
[17-Sep-2015 18:37:00 UTC] <pre>messagesIdArray
(
[0] => 760
[1] => 761
)
</pre>
[17-Sep-2015 18:37:00 UTC] <pre>messageStatusArray
(
[0] => 1
[1] => 0
)
</pre>
以下是模型Messages
中的函数:
public static function getTargetedContactMessageIds($refId)
{
$obj = new self;
$sql = 'SELECT id FROM messages WHERE ref_id = :ref_id AND status != :message_deleted AND type = :message_type';
$params = ['ref_id' => $refId, 'message_deleted' => self::STATUS_DELETED, 'message_type' => self::MESSAGE_TYPE_CONTACT];
$result = $obj->getResultSet($sql, $params)->toArray();
return array_column($result, 'id');
}
这是我在模型MessagesContact
中的函数:
public static function getTargetedContactMessageStatus($id)
{
$obj = new self;
$sql = 'SELECT status FROM messages_contact WHERE message_id = :id';
$params = ['id' => $id];
$result = $obj->getResultSet($sql, $params)->toArray();
return array_column($result, 'status');
}
正如您所看到的,数组中有许多元素,但它只通过第一个元素并停止。我错过了什么吗?
答案 0 :(得分:1)
在每次迭代中,你都要覆盖变量值,所以最后这些变量保留了最后一次迭代的值。
如果您的目标是让所有人$messagesStatus
执行以下操作:
$messagesStatus[] = $messagesContactModel->getTargetedContactMessageStatus($messageId);