CakePHP 3中相关表的条件

问题描述 投票:0回答:1

我有正确编写此代码的问题。我有companiescategoriescompanies_tagstags表。关系如下(自动烘焙):

// CompaniesTable.php
$this->belongsToMany('Tags', [
            'foreignKey' => 'company_id',
            'targetForeignKey' => 'tag_id',
            'joinTable' => 'companies_tags'
        ]);

$this->belongsTo('Categories', [
            'foreignKey' => 'categorie_id'
        ]);

// CategoriesTable.php
$this->hasMany('Etablissements', [
            'foreignKey' => 'categorie_id'
        ]);

// TagsTable.php
$this->belongsToMany('Companies', [
            'foreignKey' => 'tag_id',
            'targetForeignKey' => 'company_id',
            'joinTable' => 'companies_tags'
        ]);

// CompaniesTags.php
$this->belongsTo('Companies', [
            'foreignKey' => 'company_id',
            'joinType' => 'INNER'
        ]);
        $this->belongsTo('Tags', [
            'foreignKey' => 'tag_id',
            'joinType' => 'INNER'
        ]);

我必须选择公司的名称,类别名称或其标签名称之一包含特定文本。

$ets = (new TableRegistry())
                ->get('Companies')
                ->find('published')
                ->distinct()
                ->contain([
                    "Tags",
                    "Categories"
                    ])
                ->leftJoinWith('Tags', function (\Cake\ORM\Query $q) use ($quoi) {
                    return $q->where(['OR'  => ['Tags.nom LIKE ' => '%' . $quoi . '%', 'Tags.description LIKE' => '%' . $quoi . '%']]);
                })
                ->where(['OR' => ["Companies.nom LIKE" => "%" . $quoi . "%",
                    "Companies.description LIKE" => "%" . $quoi . "%",
                    "Categories.description LIKE" => "%" . $quoi . "%",
                    "Categories.nom LIKE" => "%" . $quoi . "%",
                    ]
                    ]);

这个查询是我能想象的,但似乎我在tags上的LEFT JOIN无效。有人可以帮我解决这个问题吗?

php cakephp cakephp-3.0
1个回答
0
投票

你可以这样做。

->contain(["Tags" => function (Query $query, $quoi) {
return $query->select(['field_1', 'field_2'])
    ->where(['OR'  => ['Tags.nom LIKE ' => '%' . $quoi . '%', 'Tags.description LIKE' => '%' . $quoi . '%']]); }, "Categories" ])
© www.soinside.com 2019 - 2024. All rights reserved.