php - How to update joinData in cakephp 3 the correct way -
trainers belongstomany customers
both trainerstable , customerstable use user entity class.
i want able update _joindata
between 1 trainer , customers.
users +----+-------+--------------------+ | id | level | email | +----+-------+--------------------+ | 1 | 30 | trainer@site.com | | 2 | 10 | customer@site.com | | 3 | 10 | customer2@site.com | +----+-------+--------------------+ trainers_users +----+------------+-------------+--------------------------+ | id | trainer_id | customer_id | trainer_note | +----+------------+-------------+--------------------------+ | 1 | 1 | 2 | customer | | 1 | 1 | 3 | | +----+------------+-------------+--------------------------+
trainerstable below
<?php namespace app\model\table; use app\model\entity\user; class trainerstable extends table { public function initialize(array $config) { $this->table('users'); $this->entityclass('app\model\entity\user'); //since table doesn't follow convention naming $this->displayfield('id'); $this->primarykey('id'); $this->belongstomany('customers',[ 'classname' => 'users', 'jointable' => 'trainers_users', 'foreignkey' => 'trainer_id', 'targetforeignkey' => 'user_id', ]); } }
customerstable below
<?php namespace app\model\table; use cake\orm\query; use cake\orm\ruleschecker; use cake\orm\table; use cake\validation\validator; class customerstable extends table { public function initialize(array $config) { $this->belongstomany('trainers',[ 'classname' => 'users', 'jointable' => 'trainers_users', 'foreignkey' => 'user_id', 'targetforeignkey' => 'trainer_id', ]); } }
userentity below
<?php namespace app\model\entity; use cake\orm\entity; /** * user entity. */ class user extends entity { protected $_accessible = [ 'email' => true, 'password' => true, 'trainers' => true, 'level' => true, 'customers' => true, ]; }
and interesting part. userscontroller action storenote gets trainer_note
, customer_id
via ajax. replacelinks way have got working far. how should have done in more sensible way. please give me hints. think i'm on wrong path here.
use app\controller\appcontroller; use cake\event\event; use cake\network\exception\notfoundexception; use cake\orm\tableregistry; class userscontroller extends appcontroller { public function initialize() { parent::initialize(); //load non standard version of users model trainers $this->users = tableregistry::get('trainers'); } /** * lets trainers store private note each customer, * trainer can view note customer */ public function storenote($customerid) { if($this->request->is('ajax')) { $this->layout = 'ajax'; $this->autorender = false; $this->response->disablecache(); } else { $this->log('referer request below: ' . $this->request->referer()); throw new forbiddenexception(); } if ($this->request->is('post')) { $trainerid = $this->auth->user('id'); //the current user trainer $trainer = $this->users->find() ->where(['id' => $trainerid]) ->contain(['customers']) ->first(); $customers = $trainer->customers; //pick customers //find index of customer id $customerid $customerkey = 0; foreach($customers $key => $singlecustomer) { if ($singlecustomer->id == $customerid) { $customerkey = $key; } } //update trainer_note customer id $customerid $customers[$customerkey]->_joindata->trainer_note = $this->request->data['trainer_note']; //replace links if ($this->users->customers->replacelinks($trainer,$customers)) { echo json_encode([ 'status' => 'success', 'redirect' => router::url('/trainer/users/viewcustomer/'.$customerid), ]); } else { $response = $this->render('/element/trainer/users/store_note'); $html = $response->body(); echo json_encode([ 'status' => 'fail', 'html' => $html, ]); } } exit(); } }
Comments
Post a Comment