Hey alle zusammen, vielleicht kann mir jemand bei meinen Problem helfen. Ich habe vor 2 Wochen mich ein wenig in cakephp eingearbeitet. Nun möchte ich mit hilfe 2 verschiedenen ids die in einer Tabelle stehen aus einer Tabelle eine Nachricht anzeigen lassen. Die eine ID ist halt die notice_id und die andere user_id damit man weiß wem welche Notice gehört.
NoticesUserController.php
Entity/NoticesUser:
Table/NoticesUsersTable
NoticesUserController.php
PHP Code:
<?php
namespace App\Controller;
use App\Controller\AppController;
/**
* NoticesUsers Controller
*
* @property \App\Model\Table\NoticesUsersTable $NoticesUsers
*
* @method \App\Model\Entity\NoticesUser[] paginate($object = null, array $settings = [])
*/
class NoticesUsersController extends AppController
{
/**
* Index method
*
* @return \Cake\Http\Response|void
*/
public function index()
{
$this->paginate = [
'contain' => ['Notices', 'Users']
];
$noticesUsers = $this->paginate($this->NoticesUser);
$this->set(compact('noticesUsers'));
$this->set('_serialize', ['noticesUsers']);
}
public $paginate = [
'limit' => 25,
'Notices' => [
'Notices.id' => 'asc'
]
];
public function initialize()
{
parent::initialize();
$this->loadComponent('Paginator');
}
/**
* View method
*
* @param string|null $id Notices User id.
* @return \Cake\Http\Response|void
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
*/
public function view($id = null)
{
$noticesUser = $this->NoticesUsers->get($id, [
'contain' => ['Notices', 'Users']
]);
$this->set('noticesUser', $noticesUser);
$this->set('_serialize', ['noticesUser']);
}
/**
* Add method
*
* @return \Cake\Http\Response|null Redirects on successful add, renders view otherwise.
*/
public function add($notice_id = null, $user_id = null)
{
$this->loadModel('Users');
$this->loadModel('NoticesUsers');
$users = $this->Users->getAllUsers();
if($user_id) {
//Es wird überprüft ob evtl. schon die Notiz geteilt wurde
if(!$this->NoticesUsers->find()->where(['notice_id' => $notice_id, 'user_id' => $user_id])->first()) {
$new = $this->NoticesUsers->newEntity();
$new->notice_id = $notice_id;
$new->user_id = $user_id;
if($this->NoticesUsers->save($new)) {
$this->Flash->success(__('Die Notiz wurde erfolgreich geteilt.'));
} else {
$this->Flash->error(__('Die Notiz konnte nicht geteilt werden. Bitte versuchen Sie es erneut.'));
}
} else {
$this->Flash->error(__('Die Notiz wurde bereits mit dem Benutzer geteilt.'));
}
}
$this->set(compact('users', 'notice_id'));
$this->set('_serialize', ['users', 'notice_id']);
/*
$noticesUser = $this->NoticesUsers->newEntity();
if ($this->request->is('post')) {
$noticesUser = $this->NoticesUsers->patchEntity($noticesUser, $this->request->getData());
if ($this->NoticesUsers->save($noticesUser)) {
$this->Flash->success(__('The notices user has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The notices user could not be saved. Please, try again.'));
}
$notices = $this->NoticesUsers->Notices->find('list', ['limit' => 200]);
$users = $this->NoticesUsers->Users->find('list', ['limit' => 200]);
$this->set(compact('noticesUser', 'notices', 'users'));
$this->set('_serialize', ['noticesUser']);*/
}
/**
* Edit method
*
* @param string|null $id Notices User id.
* @return \Cake\Http\Response|null Redirects on successful edit, renders view otherwise.
* @throws \Cake\Network\Exception\NotFoundException When record not found.
*/
public function edit($id = null)
{
$noticesUser = $this->NoticesUsers->get($id, [
'contain' => []
]);
if ($this->request->is(['patch', 'post', 'put'])) {
$noticesUser = $this->NoticesUsers->patchEntity($noticesUser, $this->request->getData());
if ($this->NoticesUsers->save($noticesUser)) {
$this->Flash->success(__('The notices user has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The notices user could not be saved. Please, try again.'));
}
$notices = $this->NoticesUsers->Notices->find('list', ['limit' => 200]);
$users = $this->NoticesUsers->Users->find('list', ['limit' => 200]);
$this->set(compact('noticesUser', 'notices', 'users'));
$this->set('_serialize', ['noticesUser']);
}
/**
* Delete method
*
* @param string|null $id Notices User id.
* @return \Cake\Http\Response|null Redirects to index.
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
*/
public function delete($id = null)
{
$this->request->allowMethod(['post', 'delete']);
$noticesUser = $this->NoticesUsers->get($id);
if ($this->NoticesUsers->delete($noticesUser)) {
$this->Flash->success(__('The notices user has been deleted.'));
} else {
$this->Flash->error(__('The notices user could not be deleted. Please, try again.'));
}
return $this->redirect(['action' => 'index']);
}
}
Entity/NoticesUser:
PHP Code:
<?php
namespace App\Model\Entity;
use Cake\ORM\Entity;
class NoticesUser extends Entity
{
protected $_accessible = [
'notice_id' => true,
'user_id' => true,
'created' => true,
'modified' => true,
'notice' => true,
'user' => true
];
}
Table/NoticesUsersTable
PHP Code:
<?php
namespace App\Model\Table;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;
class NoticesUsersTable extends Table
{
public function initialize(array $config)
{
parent::initialize($config);
$this->setTable('notices_users');
$this->setDisplayField('id');
$this->setPrimaryKey('id');
$this->addBehavior('Timestamp');
$this->belongsTo('Notices', [
'foreignKey' => 'notice_id',
'joinType' => 'INNER'
]);
$this->belongsTo('Users', [
'foreignKey' => 'user_id',
'joinType' => 'INNER'
]);
}
public function validationDefault(Validator $validator)
{
$validator
->integer('id')
->allowEmpty('id', 'create');
return $validator;
}
public function buildRules(RulesChecker $rules)
{
$rules->add($rules->existsIn(['notice_id'], 'Notices'));
$rules->add($rules->existsIn(['user_id'], 'Users'));
return $rules;
}
}