使用cakephp 3中关联模型的显示字段构建自定义输入字段列表

使用cakephp 3中关联模型的显示字段构建自定义输入字段列表,cakephp,cakephp-3.4,Cakephp,Cakephp 3.4,我有三个表,分别是campaign\u social\u accounts,social\u accounts,和social\u networks SocialNetworks包含用户可以通过列连接到的网络 +-----+--------+ | id | title | +-----+--------+ +----+---------+-------------------+--------------+----------+ | id | user_id | social_networ

我有三个表,分别是
campaign\u social\u accounts
social\u accounts
,和
social\u networks

SocialNetworks
包含用户可以通过列连接到的网络

+-----+--------+
| id  | title  |
+-----+--------+
+----+---------+-------------------+--------------+----------+
| id | user_id | social_network_id | access_token | user_key |
+----+---------+-------------------+--------------+----------+
SocialAccounts
具有用户连接到的所有帐户,列为

+-----+--------+
| id  | title  |
+-----+--------+
+----+---------+-------------------+--------------+----------+
| id | user_id | social_network_id | access_token | user_key |
+----+---------+-------------------+--------------+----------+
活动社会帐户
活动
关联,并向该活动添加了社会帐户

+-----+-------------+-------------------+
| id  | campaign_id | social_account_id |
+-----+-------------+-------------------+
activitysocialaccounts
add()
中,我希望用户从
SocialAccounts
中进行选择,这就是我在controller中所做的

$socialAccounts = $this->CampaignSocialAccounts->SocialAccounts->find('list', [
   'conditions' => [
       'user_id' => $this->Auth->user('id')
   ]
]);
$socialAccounts = $this->CampaignSocialAccounts->SocialAccounts
    ->find('list', ['valueField' => 'full_name'])
    ->contain(['SocialNetworks'])
    ->where(['user_id' => $this->Auth->user('id')]);
add.ctp

echo $this->Form->control('social_account_id', ['options' => $socialAccounts]);
问题

这将在列表中显示
id
,因为该字段中没有其他列可以设置为
displayField()

另外,我想显示列表,有点像

Facebook(112233445566)
Youtube(2233112233)
其中
Facebook
Youtube
是来自
SocialNetworks
表的标题,
(112233…)
是来自
SocialAccounts
user\u key
,生成的选项值将是
SocialAccounts
id

<option value="1<id from social_accounts>">Facebook(112233445566)</option>
<option value="2<id from social_accounts>">Youtube(2233112233)</option>
SocialAccount.php实体

public function _getSocialAccountTitle()
{
    if (isset($this->social_network)) {
        return $this->social_network->title.' ('.$this->user_key.')';
    }
    return $this->id;
}

仍然无效

在您的
社会账户
实体中,您可以定义虚拟财产

public function _getFullName()
{
    if(isset($this->social_network))
        return $this->social_network->name.' ('.$this->user_key.')';
    return $this->id;
}
然后tu可以在find()调用中使用新的虚拟属性

在控制器中

$socialAccounts = $this->CampaignSocialAccounts->SocialAccounts->find('list', [
   'conditions' => [
       'user_id' => $this->Auth->user('id')
   ]
]);
$socialAccounts = $this->CampaignSocialAccounts->SocialAccounts
    ->find('list', ['valueField' => 'full_name'])
    ->contain(['SocialNetworks'])
    ->where(['user_id' => $this->Auth->user('id')]);

见编辑2,我试过你说的。仍仅显示
id