先通过USER ID查找接收者ID,通过接收者ID定位数据
我正在使用 cakePHP 1.26。 我在本地主机数据库中得到了一个简单的表,其中包含一些数据:
user_id | message | receiver_id
-----------------------------
1 | helloworld | 12
2 | hi there | 12
假设系统现在从用户那里收到了 $userid。
用户只获得了用户ID。他想搜索特定接收者收到的所有消息
这是我针对上述情况的代码:
$receiverID=$this->user->read('receiver_id', $userid); // find the receiver ID
$data=$this->user->findByreceiver_id($receiverID); //then retrieve data by the receiver ID
debug($data);
代码不整洁,看起来很笨拙。
我不确定在这种情况下是否有最好的方法。
请指教。
I am using cakePHP 1.26.
I got a simple Table with some data in my localhost Database:
user_id | message | receiver_id
-----------------------------
1 | helloworld | 12
2 | hi there | 12
Let say the system now received the $userid from a user.
The user only got the user ID. He wants to search all the message received by a specific Receiver
Here is my code for the case mentioned above:
$receiverID=$this->user->read('receiver_id', $userid); // find the receiver ID
$data=$this->user->findByreceiver_id($receiverID); //then retrieve data by the receiver ID
debug($data);
The code isn't neat and looks clumsy.
I am not sure if there is any best way to do it in this case.
Please advise.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的意思是,你可以这样做:
这假设你上面的代码按预期工作。
或者您可以在模型中隐藏一些语法。编写一个 Model::find_by_receiver( $user_id ) ,它将包含实际的 find 调用,并返回它。不过,在包装纸上似乎有点矫枉过正。但它会让你的控制器显得更加整洁。
I mean, you could do:
This assumes that your code above works as expected.
Or you could hide some of that syntax in the model. Write a Model::find_by_receiver( $user_id ) and it would contain the actual find call, and return it. Seems a little overkill on the wrappers, though. But it will make your controller appear more neat.
食谱中的这个示例可能会有所帮助。
This example from the cookbook may help.