cakephp保存不起作用

时间:2014-01-14 16:07:29

标签: cakephp cakephp-2.0 cakephp-2.4

<?php
App::uses('AppController', 'Controller');
App::uses('Link','Model');
/**
 * Articles Controller
 *
 * @property Article $Article
 * @property PaginatorComponent $Paginator
 */
class ArticlesController extends AppController {
    public $uses = array('Link','Article');
    public $paginate = array( 'limit' => 25);
     public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow('view');
    $this->Auth->authenticate = array(
        'Basic' => array('user' => 'admin'),
        //'Form' => array('user' => 'Member')
        );
    }
/**
 * Components
 *
 * @var array
 */
    public $components = array('Paginator');

/**
 * index method
 *
 * @return void
 */
    public function index() {
        $this->Article->recursive = 0;
        $this->set('articles', $this->Paginator->paginate());
    }

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
    public function view($id = null) {
        if ($this->request->is('post')) {
            $this->Article->create();
            $this->userID = $this->Auth->user('ID');
            debug($this->Auth->user());
            $this->request->data['article']['owner_id'] = $this->userID;
            debug($this->userID);
            if ($this->Article->save($this->request->data)) { //it works
                $this->last_id = $this->Article->getLastInsertID();
                debug($this->last_id);
                            $this->request->data = null;
                $this->request->data['Link'] = array(


/*   $this->request->data['link'] = array( */ I tried It too.
                    'owner_id' => 1,
                    'LFrom' => 2138,
                    'LTo' => $this->last_id,

'quant' => 1

                );
                $this->Link->create();

                debug($this->Link->save($this->request->data));
                if ($this->Link->save($this->request->data)) { // it does not works

                    $this->Session->setFlash(__('The article has been saved.'));
                    return $this->redirect(array('action' => 'view'));

                } else {
                    $this->Session->setFlash(__('The article could not be saved. Please, try again.'));
                }
            }
        }

debug($this->request->data);

性反应的

阵列(     'Link'=&gt;阵列(         'owner_id'=&gt; (int)1,         'LFrom'=&gt; (int)2138,         'LTo'=&gt; '100207',         'quant'=&gt; (int)1     )``

sql query 
1   INSERT INTO `db0tagplus`.`article` (`created`, `modified`, `name`) VALUES ('CURRENT_TIMESTAMP', 'CURRENT_TIMESTAMP', 'a縺ゅ≠')        1   1   6
2   SELECT COUNT(*) AS `count` FROM `db0tagplus`.`article` AS `Article` WHERE `Article`.`ID` = 100102

我想将insert sql运行到link表,但它不起作用。

如何保存?

最后我通过这些答案解决了这个问题。 https://gist.github.com/koyakei/8438908

1 个答案:

答案 0 :(得分:0)

就像下面的代码一样,它会显示与验证相关的错误 - “

///  debug($this->Link->save($this->request->data)); -- remove this line
if($this->Link->save($this->request->data)){
    // suceess case
 }else{
  pr($this->Link->invalidFields);
    /// failed case
  }