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.
/**
* 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.'));
}
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'));