Haben die Tabelle word(word_id) und relation(relation_id, word1_id,word2_id)
Also eine doppelte HABTM beziehung - glaub ich halt^^
Der Code in Model bzw Controller ist folgender:
Relation Controller
- Code: Alles auswählen
- <?php
App::uses('AppController', 'Controller');
/**
* Periods Controller
*
* @property Period $Period
*/
class RelationsController extends AppController {
public function index() {
$this->Relation->recursive = 0;
$this->set('relations', $this->paginate());
}
public function view($id = null) {
$this->Relation->id = $id;
if (!$this->Relation->exists()) {
throw new NotFoundException(__('Invalid relation'));
}
$this->set('relation', $this->Relation->read(null, $id));
}
public function add() {
if ($this->request->is('post')) {
$this->Relation->create();
if ($this->Relation->save($this->request->data)) {
$this->Session->setFlash(__('The Relation has been saved'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The Relation could not be saved. Please, try again.'));
}
}
$words = $this->Relation->Word->word1_id->find('list');
$words2 = $this->Relation->Word->word2_id->find('list');
$this->set(compact('words','words2'));
}
public function edit($id = null) {
$this->Relation->id = $id;
if (!$this->Relation->exists()) {
throw new NotFoundException(__('Invalid Relation'));
}
if ($this->request->is('post') || $this->request->is('put')) {
if ($this->Relation->save($this->request->data)) {
$this->Session->setFlash(__('The Relation has been saved'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The Relation could not be saved. Please, try again.'));
}
} else {
$this->request->data = $this->Relation->read(null, $id);
}
$word1s = $this->Relation->word1_id->find('list');
$word2s = $this->Relation->word2_id->find('list');
$this->set(compact('word1_ids', 'word2_ids'));
}
public function delete($id = null) {
if (!$this->request->is('post')) {
throw new MethodNotAllowedException();
}
$this->Relation->id = $id;
if (!$this->Relation->exists()) {
throw new NotFoundException(__('Invalid Relation'));
}
if ($this->Relation->delete()) {
$this->Session->setFlash(__('Relation deleted'));
$this->redirect(array('action' => 'index'));
}
$this->Session->setFlash(__('Relation was not deleted'));
$this->redirect(array('action' => 'index'));
}
}
Relation Model
- Code: Alles auswählen
- <?php
App::uses('AppModel', 'Model');
/**
* Period Model
*
* @property User $User
*/
class Relation extends AppModel {
/**
* Use database config
*
* @var string
*/
public $useDbConfig = '**********';
/**
* Use table
*
* @var mixed False or table name
*/
public $useTable = 'relation';
/**
* Primary key field
*
* @var string
*/
public $primaryKey = 'relation_id';
/**
* Display field
*
* @var string
*/
public $displayField = 'relation_id';
//The Associations below have been created with all possible keys, those that are not needed can be removed
/**
* belongsTo associations
*
* @var array
*/
public $belongsTo =
array(
'Word1' => array(
'className' => 'Word',
'foreignKey' => 'word1_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Word2' => array(
'className' => 'Word',
'foreignKey' => 'word2_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
}
WordController
- Code: Alles auswählen
- <?php
//Configure::write('Routing.prefixes', array('admin'));
App::uses('AppController', 'Controller');
/**
* Words Controller
*
* @property Word $Word
*/
class WordsController extends AppController {
/**
* index method
*
* @return void
*/
/* public function index() {
$this->Word->recursive = 0;
$this->set('words', $this->paginate());
}
/**
* view method
*
* @param string $id
* @return void
*/
/* public function view($id = null) {
$this->Word->id = $id;
if (!$this->Word->exists()) {
throw new NotFoundException(__('Invalid word'));
}
$this->set('word', $this->Word->read(null, $id));
}
public function add() {
$languages = $this->Word->Language->find('list');
$users = $this->Word->User->find('list');
$this->set(compact('languages'));
$this->set(compact('users'));
if (!empty($this->data)) {
if ($this->Word->save($this->data)) {
$this->Session->setFlash('Der Beitrag wurde erfolgreich gespeichert');
$this->redirect('/words');
} else
$this->Session->setFlash('Fehler');
}
}
public function edit($id = NULL) {
$this->Word->id = $id;
if ($this->request->is('get')) {
$this->request->data = $this->Word->read();
} else {
if ($this->Word->save($this->request->data)) {
$this->Session->setFlash('Your post has been updated.');
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash('Unable to update your post.');
}
}
}
/**
* admin_delete method
*
* @param string $id
* @return void
*/
public function delete($id = null) {
if (!$this->request->is('post')) {
throw new MethodNotAllowedException();
}
$this->Word->id = $id;
if (!$this->Word->exists()) {
throw new NotFoundException(__('Invalid word'));
}
if ($this->Word->delete()) {
$this->Session->setFlash(__('Word deleted'));
$this->redirect(array('action' => 'index'));
}
$this->Session->setFlash(__('Word was not deleted'));
$this->redirect(array('action' => 'index'));
}
WordModel
- Code: Alles auswählen
- <?php
App::uses('AppModel', 'Model');
/**
* Word Model
*
* @property Language $Language
* @property User $User
* @property Media $Media
* @property Word $Translations
*/
class Word extends AppModel {
/**
* Use database config
*
* @var string
*/
public $useDbConfig = '**************';
/**
* Use table
*
* @var mixed False or table name
*/
public $useTable = 'word';
/**
* Primary key field
*
* @var string
*/
public $primaryKey = 'word_id';
/**
* Display field
*
* @var string
*/
//public $displayField = 'word_id';
//The Associations below have been created with all possible keys, those that are not needed can be removed
/* var $hasAndBelongsToMany = array(
'Relation' =>
array(
'className' => 'Relation' ,
'joinTable' => 'relation',
'foreignKey' => 'word1_id',
'associationForeignKey' => 'word2_id'
)
);
var $displayField = 'language_word';
//var $validate = array(
// 'language_id' => array(
// 'numeric' => array(
//'rule' => array('numeric'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
// ),
//),
//);
/**
* belongsTo associations
*
* @var array
*/
public $belongsTo = array(
'Language' => array(
'className' => 'Language',
'foreignKey' => 'language_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
public $hasAndBelongsToMany = array(
'Relation1' =>
array(
'className' => 'Relation',
'joinTable' => 'relation',
'foreignKey' => 'word_id',
'associationForeignKey' => 'word1_id',
), 'Relation2' =>
array(
'className' => 'Relation',
'joinTable' => 'relation',
'foreignKey' => 'word_id',
'associationForeignKey' => 'word2_id',
)
);?>
Gehe ich nun auf delete eines Words kommt die Fehlermeldung
Database Error
Error: SQLSTATE[42703]: Undefined column: 7 ERROR: column Relation.id does not exist LINE 1: SELECT "Relation"."id" AS "Relation__id" FROM "relation" AS ... ^
SQL Query: SELECT "Relation"."id" AS "Relation__id" FROM "relation" AS "Relation" WHERE "Relation"."word1_id" = 2