Guest
CakePhp HABTM Saving -.-
Post
by Guest » 10 Feb 2025, 01:26
Ich googele hoch und niedrig darauf. Ich bin mir nicht sicher, wo ich falsch bin, weil ich keine Fehler bekomme. Filme - ID, Titel, Genre usw. < /p>
Genres - ID, Genre < /p>
Genres_Movies - Movie_ID , genre_id < /p>
Filmmodell: < /p>
Code: Select all
public $hasAndBelongsToMany = array(
'Genre' => array(
'className' => 'Genre',
'joinTable' => 'genres_movies',
'foreignKey' => 'movie_id',
'associationForeignKey' => 'genre_id'
)
);
< /code>
Ansicht: < /p>
echo $this->Form->create('Movie', array('controller' => 'movies', 'action' => 'add', 'type' => 'file'));
echo $this->Form->input('title');
echo $this->Form->input('description', array('type' => 'textarea');
echo $this->Form->input('imdb_url');
echo $this->Form->year('release_year', 1900, date('Y'));
echo $this->Form->input('length', array('type' => 'number'));
echo $this->Form->input('genre', array('type'=>'select', 'multiple'=>'checkbox', 'options'=> $genres));
echo $this->Form->input('image', array('type' => 'file'));
echo $this->Form->button('Submit');
< /code>
moviesController: < /p>
public function add()
{
/* for populating checkbox */
$this->set('genres', $this->Movie->Genre->find('list', array('fields' => array('Genre.id', 'Genre.genre'))));
if($this->request->is('post'))
{
$data_movie = array(
'title' => $this->request->data['Movie']['title'],
'description' => $this->request->data['Movie']['description'],
'imdb_url' => $this->request->data['Movie']['imdb_url'],
'release_year' => (int)$this->request->data['Movie']['release_year']['year'],
'length' => (int)$this->request->data['Movie']['length'],
'user_id' => (int)$this->Auth->user('id')
);
$data_genre = array('Genre' => array());
foreach($this->request->data['Movie']['genre'] as $genre)
array_push($data_genre['Genre'], (int)$genre);
$data = array('Movie' => $data_movie, 'Genre' => $data_genre);
$this->Movie->create();
if($this->Movie->save($data)
{
echo 'success';
}
}
}
< /code>
Warum habe ich mir die Mühe gemacht, Daten manuell für das Speichern im MovieController einrichten, das liegt daran Es sah ein bisschen anders aus. Also habe ich es versucht und das ist es, was ich jetzt habe (es sieht für mich gut aus)
http://s10.postimg.org/ikewszo95/untitled_1.jpg
Sie können das Formular hier sehen Nicht registrierte Benutzer) < /p>
hm Jede Hilfe würde grietisch geschätzt :) < /p>
Bearbeiten - Aktualisierter Controller < / p>
public function add()
{
$this->set('genres', $this->Movie->Genre->find('list', array('fields' => array('Genre.id', 'Genre.genre'))));
if($this->request->is('post'))
{
($this->Movie->saveAssociated($this->request->data))
{
$this->redirect('/');
$this->Session->setFlash('Success.');
}
}
}
1739147189
Guest
Ich googele hoch und niedrig darauf. Ich bin mir nicht sicher, wo ich falsch bin, weil ich keine Fehler bekomme. Filme - ID, Titel, Genre usw. < /p> Genres - ID, Genre < /p> Genres_Movies - Movie_ID , genre_id < /p> Filmmodell: < /p> [code]public $hasAndBelongsToMany = array( 'Genre' => array( 'className' => 'Genre', 'joinTable' => 'genres_movies', 'foreignKey' => 'movie_id', 'associationForeignKey' => 'genre_id' ) ); < /code> Ansicht: < /p> echo $this->Form->create('Movie', array('controller' => 'movies', 'action' => 'add', 'type' => 'file')); echo $this->Form->input('title'); echo $this->Form->input('description', array('type' => 'textarea'); echo $this->Form->input('imdb_url'); echo $this->Form->year('release_year', 1900, date('Y')); echo $this->Form->input('length', array('type' => 'number')); echo $this->Form->input('genre', array('type'=>'select', 'multiple'=>'checkbox', 'options'=> $genres)); echo $this->Form->input('image', array('type' => 'file')); echo $this->Form->button('Submit'); < /code> moviesController: < /p> public function add() { /* for populating checkbox */ $this->set('genres', $this->Movie->Genre->find('list', array('fields' => array('Genre.id', 'Genre.genre')))); if($this->request->is('post')) { $data_movie = array( 'title' => $this->request->data['Movie']['title'], 'description' => $this->request->data['Movie']['description'], 'imdb_url' => $this->request->data['Movie']['imdb_url'], 'release_year' => (int)$this->request->data['Movie']['release_year']['year'], 'length' => (int)$this->request->data['Movie']['length'], 'user_id' => (int)$this->Auth->user('id') ); $data_genre = array('Genre' => array()); foreach($this->request->data['Movie']['genre'] as $genre) array_push($data_genre['Genre'], (int)$genre); $data = array('Movie' => $data_movie, 'Genre' => $data_genre); $this->Movie->create(); if($this->Movie->save($data) { echo 'success'; } } } < /code> Warum habe ich mir die Mühe gemacht, Daten manuell für das Speichern im MovieController einrichten, das liegt daran Es sah ein bisschen anders aus. Also habe ich es versucht und das ist es, was ich jetzt habe (es sieht für mich gut aus) http://s10.postimg.org/ikewszo95/untitled_1.jpg Sie können das Formular hier sehen Nicht registrierte Benutzer) < /p> hm Jede Hilfe würde grietisch geschätzt :) < /p> Bearbeiten - Aktualisierter Controller < / p> public function add() { $this->set('genres', $this->Movie->Genre->find('list', array('fields' => array('Genre.id', 'Genre.genre')))); if($this->request->is('post')) { ($this->Movie->saveAssociated($this->request->data)) { $this->redirect('/'); $this->Session->setFlash('Success.'); } } } [/code]