cakephp只保存并插入另一个表,没有来自不同模型的关联

时间:2014-01-16 02:46:34

标签: cakephp cakephp-2.1

现在我没有验证。我有模型/标签和标签表。

  

控制器/ TagController

<?php
App::uses('AppController', 'Controller');
/*App::uses('Article', 'Model');
App::uses('Link', 'Model');
App::uses('User', 'Model');*/
// with some model , sql is extending so I fell it commented out  is better.
/**
 * Tags Controller
 *
 * @property Tag $Tag
 * @property PaginatorComponent $Paginator
 */
class TagsController extends AppController {
    public $uses = array(//'Tag','Article','Link','User'
            );

     $this->Paginator->settings = array(
    'conditions'=> array(
                "Link.LFrom = $id"
             ),
    'fields' => array( 'Link.*'),

现在蛋糕运行这个sql

SELECT Link。* FROM db0tagplustag AS Tag WHERE LinkLFrom = 2152 LIMIT 20

表关系非常复杂,所以我不能使用assosiation。

如何生成这个sql?

SELECT  `Link`.* FROM `Link` WHERE `Link`.`LFrom` = 2152 LIMIT 20

我想,如果我可以卸载模型和表'Tag'就可以了。 我无法用unbind解决它。(bind是asssosiation matter)。

App::importapp::use正在加载模型订单,如何卸载指定的模型?

像这样解决

$this->loadModel('Article');

    $this->Paginator->settings = array(
        'conditions'=> array(
                    "Link.LTo = Article.ID"
                 ),
        'fields' => array('Link.*','taglink.*','Article.*'
            ),
        'joins'
         => array(
        array(
                    'table' => 'Link',
                    'type' => 'INNER',
                    'conditions' => array(
            array("$id = Link.LFrom")
            )
                ),
        array(
                    'table' => 'Link',
                    'alias' => 'taglink',
                    'type' => 'INNER',
                    'conditions' => array(
            array("Link.ID = taglink.LTo"),
            array("$trikeyID = taglink.LFrom")
            )
                ),
        )
    );
    debug($this->Paginator->paginate('Article'));
    $this->set('results',$this->Paginator->paginate('Link'));

1 个答案:

答案 0 :(得分:0)

使用以下代码进行上述查询:

  App::import('model','Link');
  $Link = new Link();
  $Link->find('all',array('conditions'=>array('LFrom'=>2152),
                          'limit'=>20,
                          'recursive'=>-1
             ));

您必须使用以下代码才能删除现有绑定

  // Let's remove the hasMany...
    $this->Leader->unbindModel(
        array('hasMany' => array('ModelName'))
    );

供参考:http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#creating-and-destroying-associations-on-the-fly