【问题标题】:Cakephp Find behavior Containable not workingCakephp Find 行为 Containable 不起作用
【发布时间】:2012-11-05 22:50:51
【问题描述】:

我有三个模型

  1. 用户
  2. 备注(与帖子相同)
  3. 友谊

我正在尝试检索由我订阅的用户共享的帖子。

我在 UsersController 中所做的是首先检索了我所有朋友的列表

 $lof=$this->User->Friendship->find('all', array('conditions'=>array('Friendship.user_from'=>$this->Auth->user('id')), 'contain'=>array('Friendship.user_to')));

现在我正在尝试显示我朋友分享的所有帖子

$allnotes= $this->User->Note->find('all', array('conditions'=>array('Note.user_id'=>array($lof)), 'order' => array('Note.created DESC')));

这给了我一个错误

Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Array' in 'where clause'

SQL Query: SELECT `Note`.`id`, `Note`.`user_id`, `Note`.`notes`, `Note`.`created`,     `User`.`id`, `User`.`name`, `User`.`email`, `User`.`username`, `User`.`password`, `User`.`gender`, `User`.`dob`, `User`.`image`, `User`.`tmp_name`, `User`.`search` FROM `fyp`.`notes` AS `Note` LEFT JOIN `fyp`.`users` AS `User` ON (`Note`.`user_id` = `User`.`id`) WHERE `Note`.`user_id` = (Array) ORDER BY `Note`.`created` DESC

我知道我正在尝试添加一个数组,但是当我尝试类似这样的相同查找查询时,它起作用了。

$allnotes= $this->User->Note->find('all', array('conditions'=>array('Note.user_id'=>array(114,115)), 'order' => array('Note.created DESC')));

从 Friendship 表中仅获取 user_to 的值并将其分配给我的查询中的 Note.user_id 的解决方案到底是什么。

【问题讨论】:

  • 在你之前的问题中,答案建议给find('list')而不是find('all')。试试看。 find('all') 返回一个多维数组,这可能会导致您的问题。

标签: php cakephp


【解决方案1】:

问题是您的 $lof 变量不包含 id 数组。它将包含来自友谊模型的完整数据数组。如果您查看$lof 变量的值,您应该明白我的意思。您可以使用debug() 函数来执行此操作。

要执行您想做的事情,您必须使用“列表”查找方法并使用Friendship.user_to 字段。像这样:

$lof = $this->User->Friendship->find('list', array(
    'fields' => array('Friendship.id', 'Friendship.user_to'),
    'conditions' => array(
        'Friendship.user_from' => $this->Auth->user('id')
));

然后找到你现在可以使用的所有笔记

$allnotes = $this->User->Note->find('all', array(
    'conditions' => array(
         'Note.user_id' => $lof
     ),
     'order' => array('Note.created DESC')
));

您可以在CakePHP's cookbook 中阅读有关不同查找方法的更多信息(我已链接到列表一)

【讨论】:

  • 谢谢乔希 - 我的错误。真的很困惑。干杯
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多