CakePHP:在hasMany关联中查找条件(CakePHP : find with conditions on hasMany association)
我试图在关联模型上使用条件过滤
find()
结果。我找到了CakePHP 2.x的解决方案,但我正在使用CakePHP 3.x.
我用这些模型:
- 联系人:
hasMany('Orders')
- 订单:
belongsTo('Contacts')
在我的ContactsController中,我想只显示具有一个或多个订单的联系人 。
public function customers() { $this->paginate = [ 'contain' => ['Orders'], 'having' => ['Orders'] ]; $contacts = $this->paginate($this->Contacts); $this->set('contacts', $contacts); }
当然,它不起作用,因为CakePHP不使用'hasMany'关联的连接,我得到这个错误:
错误:SQLSTATE [42S22]:找不到列:1054'having子句'中的未知列'Orders'
但是,我如何使用这些条件才能获得已经订购的联系人?
I am trying to filter
find()
results with conditions on an associated model.I found solutions available for CakePHP 2.x, but I am working with CakePHP 3.x.
I use these models :
- Contacts :
hasMany('Orders')
- Orders :
belongsTo('Contacts')
In my ContactsController, I want to display only contacts that have one or more orders.
public function customers() { $this->paginate = [ 'contain' => ['Orders'], 'having' => ['Orders'] ]; $contacts = $this->paginate($this->Contacts); $this->set('contacts', $contacts); }
Of course, it doesn't work, because CakePHP doesn't use joins with 'hasMany' associations, and I get this error :
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Orders' in 'having clause'
But how can I use the conditions to get only contacts that have already orders ?
原文:https://stackoverflow.com/questions/30510667
最满意答案
不滚动,只需设置内容偏移而不动画。 至少需要确定每个单元的高度,但不需要为每一行加载单元本身。
请注意,以这种方式显示的大多数应用程序将不会在当前历史记录中显示所有旧消息,它们只会有几页,如果向上滚动太远,则需要加载那些较旧的消息。 这种方法限制和加载问题。
Don't scroll, just set the content offset without animation. At minimum the height of each cell needs to be determined but the cell itself doesn't need to be loaded for every row.
Note that most apps which display in this way won't have all old messages in the immediate history, they'll only have a couple of pages, and if you scroll up too far they'll need to go and load those older messages. This approach bounds and loading issues.
相关问答
更多-
表中的NSFetchedResultsController和scrollToBottom(NSFetchedResultsController and scrollToBottom in Table)[2022-10-14]
不滚动,只需设置内容偏移而不动画。 至少需要确定每个单元的高度,但不需要为每一行加载单元本身。 请注意,以这种方式显示的大多数应用程序将不会在当前历史记录中显示所有旧消息,它们只会有几页,如果向上滚动太远,则需要加载那些较旧的消息。 这种方法限制和加载问题。 Don't scroll, just set the content offset without animation. At minimum the height of each cell needs to be determined but the ... -
NSFetchedResultsController在表更新时无法滚动(NSFetchedResultsController can't scroll while table updating)[2023-07-19]
我发现了问题。 这是因为我使用: self.tableView.rowHeight = UITableViewAutomaticDimension 感谢您的回答: UITableView在调用reloadRowsAtIndexPaths时弹回一个部分的顶部 I found problem. It is because i use: self.tableView.rowHeight = UITableViewAutomaticDimension Thanks for this answer: UITabl ... -
setFetchOffset仅适用于持久化实体。 我在遇到与NSFetchRequest setReturnsDistinctResults类似的挫折时发现了这一点,并发现Michael Waterfall对该主题的疑问: NSDictionaryResultType表达式未考虑新插入的对象 setFetchOffset only works on persisted entities. I figured this out while experiencing similar frustrations w ...
-
NSFetchedResultsController在一对多关系中(NSFetchedResultsController in one to many relationship)[2023-06-21]
如果您想跟踪这些项目(成员)中的更改,或者数据集非常大,则应使用FRC。 如果您的数据未从外部源更新,并且相对“小”,则可以将该关系用作数据源。 FRC用于跟踪更改并以有效的方式访问数据(根据需要进行故障和无故障对象,批量提取......)。 If you like to track changes in those items (Members), or the data set is very large, you should use a FRC. If your data is not update ... -
这个在你的函数中不再指向你的类,而是现在引用你的方法IonAccordion中的DOM对象。 改用这个技巧: import { Content } from 'ionic-angular'; @ViewChild(Content) content: Content; IonAccordion(){ const scope = this; this.accElement = document.getElementsByClassName('ion-accordion ...
-
使用NSFetchedResultsController从UITableView中删除(Delete from UITableView using an NSFetchedResultsController)[2022-11-29]
由于您使用的是NSFetchedResultsController您只需从上下文中删除对象,然后它就会为您更改并删除行。 因此,删除deleteRowsAtIndexPaths:行并调用它: [self.fetchedResultsController.managedObjectContext deleteObject:user]; Since you are using NSFetchedResultsController you only need to delete the object from ... -
请参阅sectionNameKeyPath文档:用于sectionNameKeyPath (在您的案例中为“name”)的密钥必须与第一个排序描述符(在您的案例中为“checked”)中使用的密钥相同。 它们可以不同,但是这两个键必须产生相同的相对顺序。 在你的情况下,我假设你想在“name”上添加一个额外的排序描述符并将其用作第一个排序描述符。 See the NSFetchedResultsController documentation: The key used for sectionNameKey ...
-
如果您的静态内容足够简单,您也可以将其全部插入标题(而不是第0部分)。 - (void)viewDidLoad { UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 400)]; // add labels, buttons, etc to header // ... self.tableView.tableHeaderView = header; } If your static co ...
-
对于任何有相同问题的人,这里有解决方案: 在你的: func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) { 确保更新案例是在插入案例之前......所 ...
-
延迟scrollToBottom()命令,以便ScrollPanel准备好接受输入。 Scheduler.get().scheduleDeferred(new Scheduler.ScheduledCommand() { @Override public void execute() { // your commands here scrollPanel.scrollToBottom(); } }); Dela ...