如何获取关系表的所有关联信息?

发布于 2025-01-08 13:28:17 字数 2517 浏览 0 评论 0原文

我有一些相关的表,主要是模型:

<?php
App::uses('AppModel', 'Model');
class Information extends AppModel {
public $useTable = 'informations';
public $displayField = 'name';
public $validate = array(
    'name' => array(
        'notempty' => array(
            'rule' => array('notempty'),
            'message' => 'The field name can not be empty',
        ),
    ),
    'lastname' => array(
        'notempty' => array(
            'rule' => array('notempty'),
            'message' => 'The field lastname can not be empty',
        ),
    ),
    'email' => array(
        'email' => array(
            'rule' => array('email'),
            'message' => 'The email address is not correct',
        ),
    ),
);

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

public $hasMany = array(
        'Education' => array(
                'className' => 'Education',
                'foreignKey' => 'informations_id',
                'dependent' => true
        ),
        'Experience' => array(
                'className' => 'Experience',
                'foreignKey' => 'informations_id',
                'dependent' => true
        ),
        'Attachment' => array(
                'className' => 'Attachment',
                'foreignKey' => 'informations_id',
                'dependent' => true
        )
);

}

我需要获取与 ID 相关的所有数据,但是当我尝试使用此代码获取数据时:

  public function view($id = null) {
    $this->Information->id = $id;
    if (!$this->Information->exists()) {
        throw new NotFoundException(__('Invalid information'));
    }
    $this->set('information', $this->Information->read(null, $id));
}

未显示表中的信息 Education、Experience 和 Attachment仅获取此信息:

Array
(
   [id] => 9
   [name] => Reynier
   [lastname] => Perez Mira
   [email] => [email protected]
   [mobile_phone] => 04241805609
   [home_phone] => 02735555899
   [address] => asdas
   [picture] => skills.png
   [other_information] => asdasdasd
   [recruitment_status] => Call for Interview
   [countries_id] => 9
)

为什么?我错过了什么吗?

I have some related tables and for the main this is the Model:

<?php
App::uses('AppModel', 'Model');
class Information extends AppModel {
public $useTable = 'informations';
public $displayField = 'name';
public $validate = array(
    'name' => array(
        'notempty' => array(
            'rule' => array('notempty'),
            'message' => 'The field name can not be empty',
        ),
    ),
    'lastname' => array(
        'notempty' => array(
            'rule' => array('notempty'),
            'message' => 'The field lastname can not be empty',
        ),
    ),
    'email' => array(
        'email' => array(
            'rule' => array('email'),
            'message' => 'The email address is not correct',
        ),
    ),
);

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

public $hasMany = array(
        'Education' => array(
                'className' => 'Education',
                'foreignKey' => 'informations_id',
                'dependent' => true
        ),
        'Experience' => array(
                'className' => 'Experience',
                'foreignKey' => 'informations_id',
                'dependent' => true
        ),
        'Attachment' => array(
                'className' => 'Attachment',
                'foreignKey' => 'informations_id',
                'dependent' => true
        )
);

}

I need to get all the data related to a ID but when I try to get the data using this code:

  public function view($id = null) {
    $this->Information->id = $id;
    if (!$this->Information->exists()) {
        throw new NotFoundException(__('Invalid information'));
    }
    $this->set('information', $this->Information->read(null, $id));
}

The information from tables Education, Experience and Attachment is not showed only this info is get:

Array
(
   [id] => 9
   [name] => Reynier
   [lastname] => Perez Mira
   [email] => [email protected]
   [mobile_phone] => 04241805609
   [home_phone] => 02735555899
   [address] => asdas
   [picture] => skills.png
   [other_information] => asdasdasd
   [recruitment_status] => Call for Interview
   [countries_id] => 9
)

Why? I miss something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光磨忆 2025-01-15 13:28:17

更新您的函数以设置信息递归:

public function view($id = null) {
    $this->Information->id = $id;
    if (!$this->Information->exists()) {
        throw new NotFoundException(__('Invalid information'));
    }
    $this->Information->recursive = 1;
    $this->set('information', $this->Information->read(null, $id));
}

然后只需确保信息模型中的所有关系都设置为指向您想要包含的其他模型。

Update your function to set the recursive on Information:

public function view($id = null) {
    $this->Information->id = $id;
    if (!$this->Information->exists()) {
        throw new NotFoundException(__('Invalid information'));
    }
    $this->Information->recursive = 1;
    $this->set('information', $this->Information->read(null, $id));
}

Then just make sure all of your relationships in your Information model are setup to point to the other models you want to include.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文