我有一个小问题,我要花几天的时间。
我需要在CakePHP的两个不同模型中进行搜索,我使用的是CakeDC搜索插件。模型为'Desordens'和'Sinonimos',关联为hasMany。一个“ Desorden”有许多“ Sinonymous”:

public $hasMany = array(
    'Sinonimo' => array(
        'className' => 'Sinonimo',
        'foreignKey' => 'desorden_id',
        'dependent' => false,
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'exclusive' => '',
        'finderQuery' => '',
        'counterQuery' => ''
    ),


因此,我按照CakeDC Docs中的示例进行了此操作:

public $filterArgs = array(
    'busca' => array(
        'type' => 'like',
        'encode' => true,
        'before' => false,
        'after' => false,
        'field' => array(
            'Desorden.name',
            'Desorden.orphanumber',
            'Sinonimo.sinonimo')
    )
);


我想做的是一个搜索,用户可以在其中插入名称,字母数字或同义词。在SQL中将是:

SELECT `Desorden`.`id`, `Desorden`.`name`, `Desorden`.`orphanumber`, `Desorden`.`expertlink`, `Desorden`.`tipodesordem`, `Desorden`.`descricao`, `Sinonimo`.`id`, `Sinonimo`.`sinonimo`, `Sinonimo`.`desorden_id` FROM `rederaras`.`desordens` AS `Desorden` LEFT JOIN `rederaras`.`sinonimos` AS `Sinonimo` ON (`Sinonimo`.`desorden_id` = `Desorden`.`id`) WHERE ((`Desorden`.`name` LIKE 'mult') OR (`Desorden`.`orphanumber` LIKE 'mult') OR (`Sinonimo`.`sinonimo` LIKE 'mult'))


但是表'Sinonimo'没有被加入,我遇到了1054错误:


  错误:SQLSTATE [42S22]:找不到列:1054“ where子句”中的未知列“ Sinonimo.sinonimo”


我不知道我错过了什么,我已经配置了这样的控制器:

$this->Prg->commonProcess();
        $this->Desorden->recursive = 0;
        $this->Paginator->settings['conditions'] = $this->Desorden->parseCriteria($this->Prg->parsedParams());
        $this->set('desordens', $this->Paginator->paginate());


我认为在模型或cakedc filterArg配置中没有问题,因为查询是通过正确的方式构造的。

$this->Desorden->bindModel(array(
            'hasMany' => array(
                'Sinonimo' => array(
                    'foreignKey' => false,
                    'conditions' => array('Desorden.id = Sinonimo.desorden_id')
                )
            )
        ), false);


有人可以帮助我加入此活动吗?

谢谢

最佳答案

我在两个模型中使用$ belongsTo解决了问题。当我设置$ belongsTo CakeDC时,在表之间创建一个Join。我不知道这是否是最佳解决方案,但对我有用。如果这可以帮助某人。

关于mysql - 使用CakeDC Search Plugin链接两个模型,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31230225/

10-10 11:37