gpt4 book ai didi

php - 如何在 cakephp 2.6 中创建具有可容纳行为的虚拟字段

转载 作者:行者123 更新时间:2023-11-29 11:22:19 25 4
gpt4 key购买 nike

我在模型用户中有此代码:

 $hasOne = array(
'Member' => array(
'className' => 'Member',
'foreignKey' => 'user_id',
'dependent' => true
)
);


function rest_findUserById($id = NULL) {
$row = $this->find(
'first',
array(
'contain' => array(
'UserSession'=>array(
'fields'=>array('user_id', 'session_token')
) ,
'Member' => array(
'fields' => array("*")
)
),
'fields' => array('username', 'email'),
'conditions' => array('User.id' => $id)
));

if (!$row) {
return FALSE;
}
debug($row);exit;
}

在模型成员中:

$virtualFields = array(
'full_name' => 'CONCAT(Member.first_name, " ",Member.last_name)'
);

$belongsTo = array(
'Country' => array(
'className' => 'Country',
'foreignKey' => 'country_id',
'conditions' => '',
'fields' => '',
'order' => ''
),


'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
?>

当我调用函数 rest_findUserById() 时,我得到以下输出:

/app/Model/User.php (line 378)

array(
'User' => array(
'username' => 'testuser',
'email' => 'test@gmail.com',
'id' => '71'
),
'Member' => array(
'id' => '11',
'user_id' => '71',
'first_name' => 'Whjc',
'last_name' => 'test_user_lname',
'email' => '',
'phone' => '778899554455',
'image_dir' => '11',
'image' => '92e20fd0260dcc5ea289611221723b6a.jpg',
'address_line_1' => 'Shhd',
'address_line_2' => 'sdf',
'city' => 'Los Angeles Area',
'state' => 'delhi',
'country_id' => '0',
'zip_code' => '56456',
'is_address_different' => false,
'date_of_birth' => '0000-00-00',
'referred_by' => 'asdf',
'created' => '2016-07-27 13:52:30',
'created_by' => '3',
'modified' => '2016-08-02 12:25:22',
'modified_by' => '3',
'status' => false
),
'UserSession' => array(
(int) 0 => array(
'user_id' => '71',
'session_token' => 'a685b3db5ab87a928716c7d8b3272572'
)
)
)

但是当我调用这段代码时:

 $this->Member->recursive = -1;
debug($this->Member->find('first'));

我得到这个输出:

/app/Model/User.php(第 377 行)

array(
'Member' => array(
'id' => '4',
'user_id' => '64',
'first_name' => 'SDFS SDF',
'last_name' => 'test_user_lname',
'email' => '',
'phone' => '778899554455',
'image_dir' => '',
'image' => '',
'address_line_1' => 'sdf',
'address_line_2' => 'sdf',
'city' => 'Los Angeles Area',
'state' => 'delhi',
'country_id' => '0',
'zip_code' => '56456',
'is_address_different' => false,
'date_of_birth' => '1970-01-01',
'referred_by' => 'asdf',
'created' => '2016-07-22 15:25:30',
'created_by' => '0',
'modified' => '2016-07-22 15:25:30',
'modified_by' => '0',
'status' => false,
'full_name' => 'SDFS SDF test_user_lname'
)
)

正如您所看到的,虚拟字段(即“full_name”)出现在第二个输出中,但不在第一个输出中。我该如何解决这个问题?

最佳答案

来自manual

you cannot use virtualFields on associated models

建议的解决方案是将虚拟字段复制到您的模型中,这样

/app/Model/User.php

$this->virtualFields['member_full_name'] = $this->Member->virtualFields['full_name'];

当然,这不适用于 hasMany 或 HABTM 关系。另请注意,通过这种方式,您将在 User 数据数组中找到虚拟字段,而不是在 Member 数据数组中

关于php - 如何在 cakephp 2.6 中创建具有可容纳行为的虚拟字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38714648/

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