简体   繁体   English

如何在cakephp中找到关联表的最后一条记录?

[英]How to find the last record of an associated table in cakephp?

I am using cakephp 2.4.5. 我正在使用cakephp 2.4.5。 I want to find the last record of an associated table. 我想找到关联表的最后一条记录。 Below is my code 以下是我的代码

    $Latest = $this->ParentModel->find('first',array(
                           'conditions' => array('ParentModel.id'=>$id),
                           'order' => array('ParentModel.AssociatedModel.id' => 'DESC')
                                                          ));    

This code has missing column error with ParentModel.AssociatedModel.id but the id column is definitely there. 此代码与ParentModel.AssociatedModel.id缺少列错误,但id列肯定存在。 Does it look right in the first place? 它首先看起来是否合适? If not, how can I fix this part or is there a better implementation? 如果没有,我该如何修复这个部分或者是否有更好的实现? Thank you 谢谢

You can't do "cross conditions" though associated models, but you can use Containable behavior to filter associated models. 您不能通过关联模型执行“交叉条件”,但您可以使用可Containable行为来过滤关联模型。

For this, you have to bind the behavior to your model through: 为此,您必须通过以下方式将行为绑定到模型:

public $uses = array('Containable');

And then do your query: 然后进行查询:

$this->ParentModel->find('first', array(
    'conditions' => array('ParentModel.id' => $id),
    'contain' => array(
        'AssociatedModel' => array(
            'limit' => 1,
            'order' => array('id DESC')
        )
    )
));

This will give you the parent model, and the last associated model. 这将为您提供父模型和最后一个关联模型。

Have you tried to read the manual about retrieving data ? 您是否尝试阅读有关检索数据的手册 It is clearly explained there. 这里有清楚的解释。

If AssociatedModel is somehow associated with one of the four association types with ParentModel, you'll have to use this syntax: 如果 AssociatedModel以某种方式与ParentModel 的四种关联类型之一相关联 ,则必须使用以下语法:

$Latest = $this->ParentModel->find('first',array(
   'contain' => array('AssociatedModel'),
   'conditions' => array('ParentModel.id' => $id),
   'order' => array('AssociatedModel.id' => 'DESC')
)); 

Make sure the other model is included by using recursive or contain(). 使用递归或contains()确保包含其他模型。 See this page about Containable . 请参阅此页关于可包含的内容

If your field name in the DB is really containing a dot CakePHP2 will have a problem with that because it uses the dot notation as separator. 如果数据库中的字段名称确实包含一个点,那么CakePHP2就会出现问题,因为它使用点表示法作为分隔符。 At least I've never used a dot in a database field, it's doable but doesn't make much sense and obviously breaks Cake, no idea if you can work around that. 至少我从来没有在数据库领域使用过一个点,它是可行的,但没有多大意义,显然打破了Cake,不知道你是否可以解决这个问题。

I guess it should be like this 我猜它应该是这样的

    $Latest = $this->ParentModel->AssociatedModel->find('first',array(
                       'conditions' => array('ParentModel.id'=>$id),
                       'order' => array('AssociatedModel.id DESC')
                                                      ));    

Check if it is correct. 检查它是否正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM