2010-12-01 6 views
0

Ce problème s'est produit lorsque j'ai ajouté une relation hasOne à l'un des modèles habtm. categories_posts maintenant ne rien enregistrer dans la base de données. Je l'ai fait avant.Enregistrement de hasOne avec HABTM dans CakePHP

En ce moment, la forme que j'ai est ceci:

<?php 
echo $this->Form->create('Post'); 
echo $this->Form->input('Post.title'); 
echo $this->Form->input('Post.Category', array('multiple' => 'checkbox')); 
echo $this->Form->input('Post.body', array('rows' => '3')); 

echo $this->Form->input('Page.title'); 
echo $this->Form->input('Page.uri'); 
echo $this->Form->input('Page.meta_keywords'); 
echo $this->Form->input('Page.meta_description'); 
echo $this->Form->input('Page.layout'); 

echo $this->Form->end('Save Post'); 
?> 

page modèle ressemble à ceci:

class Page extends AppModel { 
var $name = 'Page'; 
var $order = array('Page.modified' => 'desc'); 

var $hasOne = array(
    'Post' => array(
    'className' => 'Post' 
)); 

var $hasMany = array(
    'Snippet' => array(
    'className' => 'Snippet' 
)); 
} 

modèle Post ressemble à ceci:

class Post extends AppModel { 
var $name = 'Post'; 
var $hasAndBelongsToMany = array(
    'Category' => array(
    'className' => 'Category' 
) 
); 
var $belongsTo = array(
    'Page' => array(
    'className' => 'Page' 
) 
); 
var $actsAs = array('Containable'); 
var $virtualFields = array(
    'date_posted' => 'DATE_SUB(Post.created, INTERVAL 7 DAY)' 
); 

} 

ressemble Model Catégorie comme ceci:

class Category extends AppModel { 
    var $name = 'Category'; 
    var $hasAndBelongsToMany = array(
     'Post' => array(
      'className' => 'Post' 
     ) 
    ); 

    var $actsAs = array('Containable'); 
} 

mes tables ressemble un peu à ceci:

categories 
id name 

categories_posts 
category_id post_id 

pages 
id title uri meta_keywords meta_description layout created modified 

posts 
id page_id title uri body created modified 

Ceci est la méthode qui fait l'épargne

function admin_add() { 
    $this->set('categories', $this->Post->Category->find('list')); 

    if (! empty($this->data)) { 
     if ($this->Post->saveAll($this->data)) { 
      $this->Session->setFlash('Your post has been saved', 'flash_good'); 
      $this->redirect($this->here); 
     } else { 
      $this->Session->setFlash('Your post has not been saved','flash_bad'); 
     } 
    } 
} 

C'est un tableau d'échantillons lors de la présentation:

array(2) { 
    ["Post"]=> 
    array(3) { 
    ["title"]=> 
    string(10) "Post Title" 
    ["Category"]=> 
    array(4) { 
     [0]=> 
     string(1) "1" 
     [1]=> 
     string(1) "2" 
     [2]=> 
     string(1) "3" 
     [3]=> 
     string(1) "4" 
    } 
    ["body"]=> 
    string(16) "<p>Post Body</p>" 
    } 
    ["Page"]=> 
    array(5) { 
    ["title"]=> 
    string(10) "Page Title" 
    ["uri"]=> 
    string(0) "" 
    ["meta_keywords"]=> 
    string(11) "Page Meta K" 
    ["meta_description"]=> 
    string(11) "Page Meta D" 
    ["layout"]=> 
    string(11) "Page Layout" 
    } 
} 

Pourquoi ISN 't categories_posts économiser?

Répondre

1

Il semble que tout ce que je devais faire était d'avoir cette sous la forme:

echo $this->Form->input('Category.Category', array('multiple' => 'checkbox')); 
Questions connexes