【问题标题】:Cakephp - find threadCakephp - 查找线程
【发布时间】:2011-04-26 03:27:37
【问题描述】:

我有一个 tweet 表,用于存储类似这样的 tweet 及其回复

tweet(id, parent_id, tweet_message, time)

如果它是一条推文,则 parent_id 是一个自我 ID,如果它是一个回复,它是父 ID。如何查找以提取 id = parent_id 的推文

示例:

tweet(1, 1, 'My name is Harsha', 'time') parent_id = id 因为它是一条推文而不是回复 tweet(2, 1, 'Hello Harsha', 'time') parent_id = 1 告诉它对 id = 1 的推文的回复

【问题讨论】:

    标签: cakephp model find cakephp-1.3


    【解决方案1】:

    你为什么不使用Tree behaviour

    Yarek T 的建议可能会起作用,但仅适用于一个级别。

    【讨论】:

      【解决方案2】:

      理论上这应该可行。查看 SQL 调试日志以了解如果有任何错误它会做什么

      $conditions = array(
          'Tweet.id = Tweet.parent_id'
          );
      $tweets = $Tweet->find('all', $conditions);
      

      或者,如果您正在寻找一个特定的 ID,您可以这样做

      $conditions = array(
          'Tweet.id' => 1,
          'Tweet.parent_id' => 1
          );
      $tweets = $Tweet->find('all', $conditions);
      

      您可以使用 MySQL <> not 运算符来搜索所有非 root 推文

      $conditions = array(
          'Tweet.id' => 1,
          'Tweet.parent_id <>' => 1
          );
      $tweets = $Tweet->find('all', $conditions);
      

      'Tweet.id <> = Tweet.parent_id'
      

      【讨论】:

      • 谢谢。第一个是我正在尝试的。我是这样做的 $conditions = array( 'Tweet.id' = 'Tweet.parent_id' );
      • 现在要回复,我该如何反其道而行之,“Tweet.id 不等于 Tweet.parent_id”
      • 您应该考虑使用 Nik 建议的树行为。如果您的模型变得复杂,Cake 的模型行为非常有用。
      猜你喜欢
      • 2013-08-09
      • 2012-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-19
      • 2012-04-12
      • 1970-01-01
      相关资源
      最近更新 更多