gpt4 book ai didi

php - 在 cake php 中从 db 获取最新保存的文件版本

转载 作者:可可西里 更新时间:2023-11-01 09:06:00 25 4
gpt4 key购买 nike

我是 cake php 的新手。我在 db 上保存了笔记。每次用户编辑此注释时,它都会在数据库中保存为新版本,如 1.0、1.1、1.2……现在的问题是我想选择注释但具有最高版本。我有很多关系。这些笔记与项目有关。但我试过在我的关系中使用 finderQuery

我的项目模型关系是

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

我也试过用 limit as 1 'limit' => '1', 但是.n 它只显示 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

最佳答案

我已经用我的自定义查询做到了这一点。如果有更好的方法,任何人都可以回答。我的 Controller 功能是

 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));
}

模型关系船是

public $hasMany = array(
'Notes' => array(
'className' => 'Notes',
'foreignKey' => 'project_id',
'dependent' => true,
'conditions' => '',
'group' => 'Notes.version',
'order' => 'Notes.version desc',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
),
'Notescomment' => array(
'className' => 'Notescomment',
'foreignKey' => 'note_id',
'dependent' => true,
'conditions' => '',
'fields' => '',
'order' => 'Notescomment.created desc',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
),
'Phase' => array(
'className' => 'Phase',
'foreignKey' => 'project_id',
'dependent' => true,
'conditions' => '',
'fields' => '',
'order' => 'Phase.position asc',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
),
'Notification' => array(
'className' => 'Notification',
'foreignKey' => 'project_id',
'dependent' => true,
'conditions' => '',
'fields' => '',
'order' => 'Notification.created desc',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)

);

/**
* Contains this model's hasAndBelongsTo entity associations
*
* @var array
*/
public $hasAndBelongsToMany = array(
'Client' => array(
'className' => 'Client',
'joinTable' => 'projects_clients',
'foreignKey' => 'project_id',
'associationForeignKey' => 'client_id',
'unique' => 'keepExisting',
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
),
'User' => array(
'className' => 'User',
'joinTable' => 'projects_users',
'foreignKey' => 'project_id',
'associationForeignKey' => 'user_id',
'unique' => 'keepExisting',
'with' => 'ProjectsUser',
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
),
);

关于php - 在 cake php 中从 db 获取最新保存的文件版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31153538/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com