我正在尝试将数组($ sales)保存到数据库中

时间:2013-11-28 16:30:33

标签: cakephp

我正在尝试保存Array $ sales。

此处数据存入此数组:

sales
    0
        Product
            id146
            category
            descriptiondrei
            created2013-11-20 14:10:12
            modified2013-11-20 14:10:12
            shop_id27
            category_id1
            image(null)
        Shop
            id27
            user_id23
            titlebarby
            descriptionkleines
            created2013-10-29 12:51:32
            modified2013-10-29 12:51:32
        Category
            id1
            imageHaushalt

Controller中的方法如下所示:

public function save_notepad(){

    $sales = array();

    if($this -> Session -> valid() && $this -> Session -> check('notepad')) {
        $notepad = $this -> Session -> read('notepad');
        $this->loadModel('Product');
        $condition = array('Product.id' => $notepad);   
        $sales = $this -> Product-> find('all', array('conditions' => $condition)); 
        }   
    $this -> set('sales', $sales);

     if ($this->request->is('post')) {
        $this->Sale->create();
        if ($this->Sale->saveall($sales));
            $this->Session->setFlash(__('The sale has been saved'));
            return $this->redirect(array('action' => 'index'));
        }
    }

问题是,当我保存时,在数据库中它只保存id和创建的日期。

模型看起来像这样: class Sale扩展了AppModel {

public $belongsTo = array(
    'Product' => array(
        'className' => 'Product',
        'foreignKey' => 'product_id',
        'conditions' => array('Product.id' => 'Sale.product_id'),
        'fields' => '',
        'order' => ''
    ),
    'Shop' => array(
        'className' => 'Shop',
        'foreignKey' => 'shop_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
));

}

我还需要做什么来保存已经在Array $ sales中的“product_id”和“shop_id”?

感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

将数组序列化为xml或json,并将单个字符串blob另存为数据库。

答案 1 :(得分:0)

我找到了解决方案:

php echo $ this->表单>输入(' product-id',数组('类型' =>'文字',' ;价值' => $销售['产品'] [' ID']);

它需要'键入' bevor' value'。

相关问题