namespace App\Controller\Api\V1;
use App\Controller\AppController;
-
+use Cake\ORM\TableRegistry;
/**
* Users Controller
*
public function view($id = null)
{
$user = $this->Users->get($id, [
- //'contain' => ['UserTypes', 'RelationshipsSoftwares', 'Relationships', 'Reviews']
- 'contain'=>['UserTypes','Reviews']
+ 'contain' => ['UserTypes','RelationshipsSoftwares', 'Relationships', 'Reviews']
+ // 'contain'=>['UserTypes','Reviews']
]);
- $this->set('user', $user);
- $this->set('_serialize', ['user']);
+
+
+
+ $creatorOfSoftwares = $this->getRelationsByUserId($id, 'CreatorOf');
+ $userSoftwares = $this->getRelationsByUserId($id, 'UserOf');
+ $backerSoftwares = $this->getRelationsByUserId($id, 'BackerOf');
+ $contributorSoftwares = $this->getRelationsByUserId($id, 'ContributorOf');
+ $providerForSoftwares = $this->getRelationsByUserId($id, 'ProviderFor');
+
+ $this->set(compact('user','creatorOfSoftwares','userSoftwares','backerSoftwares','contributorSoftwares','providerForSoftwares'));
+ $this->set('_serialize', ['user','creatorOfSoftwares','userSoftwares','backerSoftwares','contributorSoftwares','providerForSoftwares']);
}
+ /**
+ *
+ * @param Integer $id reprensent the user id
+ * @param String $typeOfRelation represent the name of the relation (filed => 'cd')
+ * @return type
+ */
+ private function getRelationsByUserId($id,$typeOfRelation){
+
+ $RelationshipsSoftwaresUsersTable = TableRegistry::get('relationships_softwares_users');
+
+ if (!empty($this->Users->Relationships->find('all')
+ ->select('id')
+ ->where(['relationships.cd = ' => $typeOfRelation])
+ ->toArray()) ){
+ $relationId = $this->Users->Relationships->find('all')
+ ->select('id')
+ ->where(['relationships.cd = ' => $typeOfRelation])
+ ->toArray()[0]->id;
+
+ // Get the all relationships between an user and softwares
+ $relations = $RelationshipsSoftwaresUsersTable->find('all')
+ ->contain(['Relationships'])->where(["relationships_softwares_users.user_id = " =>$id])
+ ->where(['relationships_softwares_users.relationship_id = '=> $relationId]);
+ }
+ return $relations ;
+ }
+
/**
* Add method
*
$this->hasMany('Reviews', [
'foreignKey' => 'user_id'
]);
-// $this->belongsToMany('RelationshipsSoftwares', [
-// 'foreignKey' => 'user_id',
-// 'targetForeignKey' => 'relationships_software_id',
-// 'joinTable' => 'relationships_softwares_users'
-// ]);
-// $this->belongsToMany('Relationships', [
-// 'foreignKey' => 'user_id',
-// 'targetForeignKey' => 'relationship_id',
-// 'joinTable' => 'relationships_users'
-// ]);
+ $this->belongsToMany('RelationshipsSoftwares', [
+ 'foreignKey' => 'user_id',
+ 'targetForeignKey' => 'relationship_id',
+ 'joinTable' => 'relationships_softwares_users'
+ ]);
+ $this->belongsToMany('Relationships', [
+ 'foreignKey' => 'user_id',
+ 'targetForeignKey' => 'recipient_id',
+ 'joinTable' => 'relationships_users'
+ ]);
}
/**