php - 如何从一个或多个与模型类的表没有直接关系的表中检索数据

标签 php mysql cakephp

我被迫更改整个帖子,因为我相信我无法说清楚。另外,我得到了一个答案,但我相信这是由于我一开始对问题的解释不充分,所以这个答案对我的情况没有多大帮助。

请允许我更清楚地解释一下。下面是我的模型:

class HomeLoanDistributionsDetail extends AppModel {

    var $name = 'HomeLoanDistributionsDetail';
    var $actsAs = array('Logable' => array(
            'userModel' => 'User',
            'userKey' => 'user_id',
            'change' => 'list', // options are 'list' or 'full'
            'description_ids' => TRUE // options are TRUE or FALSE
    ));
    var $validate = array(
        'entry_date' => array(
            'rule' => 'date',
            'message' => 'Enter a valid date',
            'allowEmpty' => true
        ),
        'branch_id' => array('numeric'),
        'customer_id' => array('numeric'),
        'loan_amount' => array('numeric'),
        'service_charge' => array('numeric'),
        'security' => array('numeric'),
        'loan_taken_term' => array('numeric'),
        'purpose_id' => array('numeric'),
        'installment_amount' => array('numeric'),
        'installment_service_charge' => array('numeric'),
    );
    //The Associations below have been created with all possible keys, those that are not needed can be removed
    var $belongsTo = array(
        'Customer' => array(
            'className' => 'Customer',
            'foreignKey' => 'customer_id',
            'fields' => 'id,name,fathers_or_husbands_name,credit_card_no,membership_type'
        ),
        'Branch' => array(
            'className' => 'Branch',
            'foreignKey' => 'branch_id',
            'fields' => 'id,name',
        ),
        'HomeLoanCategory' => array(
            'className' => 'HomeLoanCategory',
            'foreignKey' => 'category_id',
            'fields' => 'id,name',
        )
    );

我还有另外两个模型类:

  1. LoanGroup 具有数据库表 loan_groups
  2. LoanGroupsCustomer 具有数据库表 loan_groups_customers

最重要的一点:

  • LoanGroupsCustomer 有一个名为 customer_id
  • 的字段
  • LoanGroupsCustomer 还有另一个名为 loan_group_id 的字段,它是 LoanGroup 的外键
  • LoanGroup 有一个名为 name 的字段。 我希望此字段包含在我的 HomeLoanDistributionsDetails 模型中。

基本上,可以得到正确结果集的相应 SQL 查询是这样的:

SELECT D.name AS loan_group, 
CONCAT(B.name,'/',B.fathers_or_husbands_name) AS customer_name_father_husband_Name ,
B.credit_card_no,B.membership_type AS Member_Class,
A.entry_date AS loan_date,
A.loan_amount, 
A.service_charge,
A.security,
A.loan_taken_term,
E.name,
A.installment_amount,
A.installment_service_charge

FROM home_loan_distributions_details A

LEFT JOIN customers B ON A.customer_id = B.id
LEFT JOIN loan_groups_customers C ON B.id = C.customer_id
LEFT JOIN loan_groups D ON C.loan_group_id = D.id
LEFT JOIN home_loan_categories E ON A.category_id = E.id

WHERE A.customer_id = 18

GROUP BY A.id

我完全不知道如何使用模型类中现有的 belongsTo 关联在模型类中执行以下操作:

LEFT JOIN loan_groups_customers C ON B.id = C.customer_id
LEFT JOIN loan_groups D ON C.loan_group_id = D.id

请注意,我的 home_loan_distributions_details 表没有任何可以直接映射到 loan_groups 的列,例如 loan_group_idloan_group 表作为外键。这是我的问题背后的主要原因。

我在 belongsTo 关联部分尝试了类似的操作,但收到了 SQL 错误(自然地,如预期):

'LoanGroup' => array(
            'className' => 'LoanGroup',
            'foreignKey' => 'customer_id',
            'fields' => 'id,name'
        ),

CakePHP 版本 - 1.2.5、PHP 版本 - 5.2.11、MySQL 版本 - 5.1.36。

最佳答案

您可以运行sub-query ,或者您可以使用 ClassRegistry::init('region'); 动态加载模型,并首先调用 find,传入外键。不过我推荐第一个,因为它会更有效。

关于php - 如何从一个或多个与模型类的表没有直接关系的表中检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27211927/

相关文章:

javascript - 如何将reCaptcha v2与JS和PHP表单集成?

internet-explorer - cakephp 在使用 Safari 和 Internet Explorer 时丢失 session

mySQL 数据类型用于存储带有缩写后缀的数字?

mysql - 将 GROUP BY 的多个子查询转换为 JOIN

cakephp - 在自定义组件中使用 session 组件

mysql - phpCake 可以调整为接受不符合约定的数据库名称对象名称吗

php - 我们可以从 php 文件传递​​一个包含 jquery $.get() 中 url 的变量吗

php - 疯狂的 xhr 错误

php - 在 CodeIgniter 中扩展库时遇到问题

Android MySQL 仅在 Android 4.0 中存在问题?