0

I have three tables campaign_social_accounts, social_accounts, and social_networks

SocialNetworks contains are networks which user can connect to with columns as

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

SocialAccounts has the all accounts user is connected to with columns as

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

CampaignSocialAccounts has association of Campaigns and added social accounts to that campaign

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

In add() of CampaignSocialAccounts I want user to select from SocialAccounts for that, this is what I have done in controller

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

and add.ctp

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

Question

This shows id in the list as there is no other column in that field that can be set to displayField()

Also, I want to display list somewhat like

Facebook(112233445566)
Youtube(2233112233)

Where Facebook and Youtube are title from SocialNetworks table and (112233....) is user_key from SocialAccounts and the value of the option generated will be the id of the SocialAccounts

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

Is it possible, if yes, what is the best and simple approach to do this.

Edit 2: My Try

In controller action

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

SocialAccount.php entity

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

Still no effect

Anuj TBE
  • 9,198
  • 27
  • 136
  • 285

1 Answers1

1

in your SocialAccounts Entity you can define a virtual property

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

then tu can use your new virtual property in the find() call

in your controller

$socialAccounts = $this->CampaignSocialAccounts->SocialAccounts
    ->find('list', ['valueField' => 'full_name'])
    ->contain(['SocialNetworks'])
    ->where(['user_id' => $this->Auth->user('id')]);
arilia
  • 9,373
  • 2
  • 20
  • 44