0

我有三张桌子campaign_social_accountssocial_accountssocial_networks

SocialNetworkscontains 是用户可以通过列连接到的网络

+-----+--------+
| id  | title  |
+-----+--------+

SocialAccounts让所有帐户用户与列连接为

+----+---------+-------------------+--------------+----------+
| id | user_id | social_network_id | access_token | user_key |
+----+---------+-------------------+--------------+----------+

CampaignSocialAccounts与该活动关联Campaigns并添加了社交帐户

+-----+-------------+-------------------+
| id  | campaign_id | social_account_id |
+-----+-------------+-------------------+

我希望用户从中进行选择add(),这就是我在控制器中所做的CampaignSocialAccountsSocialAccounts

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

add.ctp

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

问题

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

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

Facebook(112233445566)
Youtube(2233112233)

Where Facebookand Youtubeare title from SocialNetworkstable and (112233....)is user_keyfromSocialAccounts并且生成的选项的值将idSocialAccounts

<option value="1<id from social_accounts>">Facebook(112233445566)</option>
<option value="2<id from social_accounts>">Youtube(2233112233)</option>

是否有可能,如果是,那么最好和最简单的方法是什么。

编辑 2:我的尝试

在控制器动作中

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

SocialAccount.php 实体

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

还是没有效果

4

1 回答 1

1

在您的SocialAccounts实体中,您可以定义一个虚拟属性

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

然后你可以在 find() 调用中使用你的新虚拟属性

在你的控制器中

$socialAccounts = $this->CampaignSocialAccounts->SocialAccounts
    ->find('list', ['valueField' => 'full_name'])
    ->contain(['SocialNetworks'])
    ->where(['user_id' => $this->Auth->user('id')]);
于 2017-06-29T06:17:05.787 回答