gpt4 book ai didi

mysql - cakephp 对同一个类有很多查询

转载 作者:太空宇宙 更新时间:2023-11-03 12:06:31 24 4
gpt4 key购买 nike

我正在使用这个绑定(bind)

$this->Company->bindModel( array(
'hasMany' => array(
'CompanyContactPerson'=>array('conditions' => array('Company.name Like'=>'%'.$a.'%'),),
),
'belongsTo' => array(
'Status',
'User'
)
));
$this->paginate = array(
'limit' =>20,
'conditions'=>$conditions,
'order' =>array('Company.id'=> 'desc') ,
);
$all_companies = $this->paginate('Company');

它给出这个查询作为结果。

SELECT `Company`.`id`, `Company`.`user_id`, `Company`.`name`, `Company`.`status_id`, `Company`.`email`, `Company`.`modified`, `Company`.`created`, `Status`.`id`, `Status`.`name`, `User`.`id`, `User`.`roll`, `User`.`username`, `User`.`email`, `User`.`password`, `User`.`first_name`, `User`.`last_name`, `User`.`gender`, `User`.`address`, `User`.`city`, `User`.`state`, `User`.`country`, `User`.`modified`, `User`.`created` 
FROM `companyinfo`.`companies` AS `Company`
LEFT JOIN `companyinfo`.`statuses` AS `Status` ON (`Company`.`status_id` = `Status`.`id`)
LEFT JOIN `companyinfo`.`users` AS `User` ON (`Company`.`user_id` = `User`.`id`)
WHERE 1 = 1 ORDER BY `Company`.`id` desc LIMIT 20

它对 CompanyContactPerson 模型进行单独查询。因此,我从 Company 表中得到了所有结果。我只需要公司表中满足 CompanyContactPerson 条件的那些行。我是如何做到这一点的

最佳答案

将其放入您的 Company 模型中,以便它可以建立关系。 :)

public $hasMany = array(
'CompanyContactPerson' => array(
'className' => 'CompanyContactPerson',
'foreignKey' => 'ForeignKeyinCompanyContactPerson',
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => '',
'dependent' => true
)
));

或将其放入您的 CompanyContactPerson 模型

public $belongsTo = array(
'Company' => array(
'className' => 'Company',
'foreignKey' => 'ForeignKeyofCompanyContactPersonTABLE',
'conditions' => '',
'fields' => '',
'order' => ''
)
);

关于mysql - cakephp 对同一个类有很多查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26271118/

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