cakephp


Finding results from primary and associated tables


I'm just starting to work with CakePHP v3 after some years of v1.3, so there's a few things to I'm at a loss for, or rather at a loss as to how to do it 'properly'
I have 2 tables (products and data) that I want to search. However, the keywords provided by the end user may match the products.title or one of the data.value fields. I want a single query to return all of the results for pagination, but can't seem to get the OR component to cross the products and data tables.
The following code (in pieces as I've been trying all sorts of things)
$query = $this->Products->find("all");
$query->select($this->Products);
$query->contain(['Data']);
$query->where("MATCH(Products.code, Products.title) AGAINST(:search IN BOOLEAN MODE)");
$query->orWhere("MATCH(Data.value) AGAINST(:search IN BOOLEAN MODE)");
$query->bind(":search", $terms);
$query->select(["relevance" => "MATCH(Products.code, Products.title) AGAINST(:search IN BOOLEAN MODE)"]);
$this->paginate['sortWhitelist'] = ['code', 'title', 'uom', 'relevance'];
$products = $this->paginate($query);
$this->set(compact('products'));
returns the following error
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Data.value' in 'where clause'
and when checking the query I can see because it has not joined the data table to the Products, and the where clause therefore fails. Using the matching() method doesn't seem to allow the returning of results where it's in the products but not the data, or vise versa.
How do I construct my query statement so as to achieve something like the following query (specifically the 'where' conditions, I've got the fields themselves sorted)
SELECT
Products.id, Products.title, [etc.],
MATCH(Products.code, Products.title) AGAINST('keyword' IN BOOLEAN MODE)
+ MATCH(Data.value) AGAINST('keyword' IN BOOLEAN MODE)
AS `relevance`
FROM
products Products
LEFT JOIN
data Data
on Data.product_id = Products.id
WHERE
MATCH(Products.code, Products.title) AGAINST('keyword' IN BOOLEAN MODE)
or
MATCH(Data.value) AGAINST('keyword' IN BOOLEAN MODE)
I of course want to use prepared statements and bind() to assist with the sanitization and reuse of the user data.
Typically, moments later I solved it.
In the end, rather than using $query->contain(['Data']);
I had to use $query->leftJoinWith('Data')
Once that was swapped out, it worked!

Related Links

Triggering event manually in JS Helper CakePHP
CakePHP Count in a Loop
Cakephp 2.x REST
Dynamically add virtual field in cakephp
CakePHP - I can't get App::uses() to find my class
CakePHP 2.2.0 - AuthComponent - different Model than User - password always incorrect
Groups Controller could not be found
Cakephp 2.0 + Elements + requestAction = empty result
CakePHP Sharing code between helpers
how to load or use a Model in a Component File especially in Cakephp 2.0?
$this->Auth->login() creates correct query that returns 1 row, but fails IF check (server specific issue)
Is it possible to insert all related records in a HABTM at once with saveAll in Cakephp?
Caching controller action in CakePHP
CakePHP Cron Job on GoDaddy
CakePHP 1.3 HABTM - Add works fine, Edit will not save HABTMs
CakePHP displaying “admi” at the top of any page

Categories

HOME
pandas
yii2
vim
atom-editor
keycloak
plot
view
office365api
relayjs
platform-builder
sqlite3
callback
ravendb
python-unittest
php-7.1
windows-azure-storage
alignment
visual-studio-2005
event-handling
opentracing
aurigma
database-replication
paging
css-animations
vision
c++-amp
instant-messaging
android-nestedscrollview
bower-install
dartium
opshub
android-tabhost
sfdc
nxlog
total-commander
acoustics
eclipse-gef
theming
python-webbrowser
mcafee
quartz-composer
spring-android
google-web-starter-kit
nessus
php-internals
php-parse-error
maven-tomcat-plugin
intellij-14
retina
wordml
computer-algebra-systems
angular-leaflet-directive
riak-cs
ibaction
ios9.1
graph-drawing
fluid-dynamics
ami
census
c++03
modalpopup
tablelayout
p4java
oam
dataadapter
chronoforms
android-hardware
angularjs-controller
runas
objective-c-2.0
dbproviderfactories
pendrive
hamachi
fluent-interface
dmx512
revisions
ext3
castle-monorail
fixed-width
boost-smart-ptr
port-number
msdev
dbisam
windows-live-messenger

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App