Question

Now I have no validation. I have Model/Tag and tag table.

Controller/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.*'),

Now cake run this sql

SELECT Link.* FROM db0tagplus.tag AS Tag WHERE Link.LFrom = 2152 LIMIT 20

Table relation is very complexed so I can not use assosiation.

how to generate this sql?

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

I guess, if I could unload model and table 'Tag' It will be OK. I cannot solve it with unbind.(bind is asssosiation matter).

App::import and app::use is loading model order, how to unload specified model?

Solved like this

$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'));
Was it helpful?

Solution

Using below code to make above query:

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

You have to use below code in order to remove existing binding

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

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top