如何以正确的方式更新cakephp 3中的joinData

时间:2016-01-27 10:23:04

标签: php cakephp-3.0

培训师属于许多客户 TrainersTable和CustomersTable都使用用户实体类。

我希望能够在一位培训师和客户之间更新_joinData

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 | something about customer |
|  1 |          1 |           3 |                          |
+----+------------+-------------+--------------------------+

下面的TrainersTable     

<?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 this 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     

<?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     

<?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,
   ];
}

现在是有趣的部分。 具有动作storeNote的UsersController通过ajax获取trainer_notecustomer_id。 replaceLinks是我到目前为止工作的唯一方式。 但是应该我是如何以更合理的方式做到这一点的。请给我一些提示。我想我在错误的道路上。     

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 a non standard version of the users model for trainers
        $this->Users = TableRegistry::get('Trainers');
   }

    /**
     * Lets trainers store a private note about each customer,
     * Only the trainer can view this note about the customer
     */
    public function storeNote($customerID)
    {
        if($this->request->is('ajax'))
        {
            $this->layout = 'ajax';
            $this->autoRender = false;
            $this->response->disableCache();
        }
        else
        {
            $this->log('Referer for request below: ' . $this->request->referer());
            throw new ForbiddenException();
        }

        if ($this->request->is('post')) {
            $trainerID = $this->Auth->user('id'); //the current user is a trainer

            $trainer = $this->Users->find()
                                   ->where(['id' => $trainerID])
                                   ->contain(['Customers'])
                                   ->first();
            $customers = $trainer->customers; //Pick the customers

            //Find the index of the customer with id $customerID
            $customerKey = 0;
            foreach($customers as $key => $singleCustomer)
            {
                if ($singleCustomer->id == $customerID) {
                    $customerKey = $key;
                }
            }

            //Update the trainer_note for customer with id $customerID
            $customers[$customerKey]->_joinData->trainer_note = $this->request->data['trainer_note'];

            //Replace the 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();
    }
}

0 个答案:

没有答案