has and belongs to many - Saving hasOne with HABTM in CakePHP -
this problem occurred when added hasone relationship 1 of habtm models. categories_posts
not save in database. did before.
right now, form have this:
<?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 model looks this:
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' )); }
post model looks this:
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)' ); }
category model looks this:
class category extends appmodel { var $name = 'category'; var $hasandbelongstomany = array( 'post' => array( 'classname' => 'post' ) ); var $actsas = array('containable'); }
my tables looks this:
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
this method saving
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'); } } }
this sample array when submitting:
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" } }
why isn't categories_posts
saving?
it seems needed have in form:
echo $this->form->input('category.category', array('multiple' => 'checkbox'));
Comments
Post a Comment