从db in cake php获取最新保存的文件版本

时间:2015-07-01 05:57:35

标签: php mysql cakephp mysqli

我是新蛋糕php。我在db上保存了笔记。每次用户编辑此笔记时,它都会像db.1.1,1,1.1中那样保存为新版本......现在问题是我想选择笔记但是版本最高。我有很多关系。这些笔记与项目有关。但我试过在我的关系中使用finderQuery,如

我的项目模型关系是

public $hasMany = array(
        'Notes' => array(
            'className' => 'Notes',
            'foreignKey' => 'project_id',
            'dependent' => true,
            'conditions' => '',
            'fields' => '',
            'order' => 'Notes.version desc',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        )
);

我也尝试过限制为1 'limit' => '1',,但是它只显示了1条记录。 我发现它可以用finderQuery来完成。所以我使用它像

'Notes' => array(
            'className' => 'Notes',
            'foreignKey' => 'project_id',
            'dependent' => true,
            'conditions' => '',
            'fields' => '',
            'order' => 'Notes.version desc',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => 'SELECT Notes.* FROM ce_notes AS Notes WHERE Notes.version="select max(Notes.version) from ce_notes"',
            'counterQuery' => ''
        )

它显示我的结果为空。

更新

如果我在

之类的条件下使用静态版本
public $hasMany = array(
        'Notes' => array(
            'className' => 'Notes',
            'foreignKey' => 'project_id',
            'dependent' => true,
            'conditions' => 'Notes.version=1.1', // here if Notes.version=max(Notes.version) can work 
            'fields' => '',
            'order' => 'Notes.version desc',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        ),

我找不到这样做的方法。虽然我可以使用核心mysqli查询执行此操作,但我想使用cakephp的标准。请帮我 enter image description here

1 个答案:

答案 0 :(得分:0)

我使用自定义查询完成了此操作。任何人都可以回答,如果有更好的方法来做到这一点。我的控制器功能是

<html>

<style>
    * {
        -webkit-box-sizing: border-box;
           -moz-box-sizing: border-box;
            -ms-box-sizing: border-box;
                box-sizing: border-box;
    }

    .pic {
        border: 5px solid #FF0000;  
        float: left;
        width: 475px;
        height: 375px;
        margin: 20px;
        overflow: hidden;
        position: relative; 

        -webkit-box-shadow: 5px 5px 5px #C0C0C0;
                box-shadow: 5px 5px 5px #C0C0C0;  
    }

    .grow img {
        width: 475px;
        height: 375px;

        -webkit-transition: all 1s ease;
           -moz-transition: all 1s ease;
             -o-transition: all 1s ease;
            -ms-transition: all 1s ease;
                transition: all 1s ease;
    }

    .grow img:hover {
        width: 570px;
        height: 450px;
    }

    .label {
        top: 150px;
            position:absolute;
            text-align:center;
            font-size:500%; 
            color: FF0000;
            width: 100%;
    }
</style>

<a href="MyLink.html">
    <div class="grow pic">
        <img src="MyImage.jpg" alt="TITLE">
        <div class="label">TITLE</div>
    </div>
</a>

模型关系是

 public function admin_view($id = null)
    {
        $this->Project->id = $id;
        if (!$this->Project->exists())
        {
            throw new NotFoundException(__('Invalid project'));
        }
        $project = $this->Project->find('first', array(
            'conditions' => array('Project.id' => $id),
            'contain' => array('Client', 'Phase','Notes')));
        $query="SELECT  `Notes`.`id` ,  `Notes`.`project_id` ,  `Notes`.`version` ,  `Notes`.`title` ,  `Notes`.`slug` ,  `Notes`.`comment_count` ,  `Notes`.`description` ,  `Notes`.`created` , `Notes`.`modified` ,  `Notes`.`project_id` 
            FROM  `EB`.`ce_notes` AS  `Notes` 
            WHERE  `Notes`.`project_id` = ('".$id."') 
            AND  `Notes`.`version` = ( 
            SELECT MAX(  `Notes`.`version` ) 
            FROM  `EB`.`ce_notes` AS  `Notes` 
            WHERE  `Notes`.`project_id` = ('".$id."') ) 
            ORDER BY  `Notes`.`version`";
       $notes= $this->Project->query($query);       
        $this->set(array('project'=>$project,'notes'=>$notes));
    }