如何返回两组数组数据以供 JQuery AJAX 访问?

发布于 2024-09-12 11:11:40 字数 336 浏览 9 评论 0原文

我正在学习 cakePHP 1.26。和 JQuery
在控制器中,我用这些代码行得到了两个操作:

function testing(){
$a = $this->User->findallByuser_id(1);
$b = $this->User->Post->findallByuser_id(1);

return a+b; // I was trying to return Array data $a and $b
}

如何返回两组数组数据($a 和 $b)以供 JQuery AJAX 访问?

如果可以的话请帮忙。

I am learning cakePHP 1.26. and JQuery
In a Controller, I got two an action with these lines of codes :

function testing(){
$a = $this->User->findallByuser_id(1);
$b = $this->User->Post->findallByuser_id(1);

return a+b; // I was trying to return Array data $a and $b
}

how would you return two groups of Array data ($a and $b) to be accessed by JQuery AJAX?

Please help if you could.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓝礼 2024-09-19 11:11:40

这本质上是尼克的回答,我完全同意他的观点,但我认为一个例子可能有用:

function testing(){
  $a = $this->User->findallByuser_id(1);
  $b = $this->User->Post->findallByuser_id(1);

  return json_encode( array( 'User' => $a, 'Posts' => $b ) )
}

这就是我所做的,我发现它非常有效。

This is essentially Nik's answer and I agree with him completely, but I think an example may be useful:

function testing(){
  $a = $this->User->findallByuser_id(1);
  $b = $this->User->Post->findallByuser_id(1);

  return json_encode( array( 'User' => $a, 'Posts' => $b ) )
}

This is what I do and I've found it to be very effective.

往事随风而去 2024-09-19 11:11:40

我会使用 json_encode/json_decode 当你访问它时,而在客户端,您可以使用 $.parseJSON(json_strong) 将其转换为对象。

I would use json_encode/json_decode and when you access it on you, while at the client side you can use $.parseJSON(json_strong) in order to convert it to object.

骄兵必败 2024-09-19 11:11:40

您需要在用户和他的帖子之间建立关系,也许是 HasMany 关系。设置完成后,您需要这样的查询:

$query['conditions'] = array('User.id' => $user_id);
$query['contain'] = array('Post');
$data = $this->User->find('all', $query);

请参阅 http://book.cakephp。 org/view/1323/Containable 了解有关使用 Containable 的信息。另请参阅 http://book.cakephp.org/view/1039/ Associations-Linking-Models-Together 了解有关通过关联链接模型的信息。

希望有帮助。

You need to have a relation between the user and his posts, maybe with a HasMany relation. When you have that set up, you need a query like this

$query['conditions'] = array('User.id' => $user_id);
$query['contain'] = array('Post');
$data = $this->User->find('all', $query);

See http://book.cakephp.org/view/1323/Containable for information on using Containable. Also see http://book.cakephp.org/view/1039/Associations-Linking-Models-Together for information on linking models via associations.

Hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文