Been wrestling with this a while and need some help.
I have a document array in PHP that looks something like this
array("title"=>"some title",
"description" => "Short description",
"categories" => array ( array("id" => "1", "name" => "category 1"),
array("id" => "5", "name" => "category 5"),
array("id" => "55", "name" => "category 55"),
)
);
So basically I have a document that has many categories assigned. My ultimate aim with this is to return all documents that contain a specific category by ID.
I have setup mappings, and the documents are being index OK. I have tried both "nested" type of mapping for category, no mapping at all, and "lists" type but that did not work at all with the error on creation of the mappings.
sample of how I set up "lists"
array(
'_source' => array(
'enabled' => true
),
'properties' => array(
'title' => array(
'type' => 'string',
'analyzer' => 'standard'
),
'description' => array(
'type' => 'string',
'analyzer' => 'standard'
),
'categories' => array(
"type" => "lists",
"properties"=> array(
"id" => array("type"=> "integer" ),
"name" => array( "type"=> "string" ),
),
),
)
);
And for completeness, how I set it up as "nested"
array(
'_source' => array(
'enabled' => true
),
'properties' => array(
'title' => array(
'type' => 'string',
'analyzer' => 'standard'
),
'description' => array(
'type' => 'string',
'analyzer' => 'standard'
),
'categories' => array(
"type" => "nested",
"properties"=> array(
"id" => array("type"=> "integer" ),
"name" => array( "type"=> "string" ),
),
),
)
);
When trying to make a search against nested mappings, it tells me
QueryParsingException: [nested] nested object under path [categories] is not of nested type
When trying to make a nested search against an un mapped set of categories (defaulting to what the system finds) it also returns the same error as above.
My questions are:
what mapping should I setup for these array of categories ?
Given a "match_all" query, what filter (and how) should I use to get a single category?
so much thanks in advance for anyone who even takes the time to read this, let alone answer. I'm really hopeful that you can help.
The answer was pretty simple. I had misread the documentation and has added an extra object type into my mapping requests that did not need to be there.
'categories' => array(
"type" => "lists",
"properties"=> array(
"id" => array("type"=> "integer" ),
"name" => array( "type"=> "string" ),
),
),
should have been
'categories' => array(
"properties"=> array(
"id" => array("type"=> "integer" ),
"name" => array( "type"=> "string" ),
),
),
The array type does not need to be defined as a type, it is assumed to be an array. Once removed, it loads the mapping properly.
Now on to searching....
Related
I am creating a custom Elementor widget with two image controls, unfortunately, I can only get one of the two to work in the content tab. I thought if I add two sections it would work but seems not, looked at Elementor documentation at https://developers.elementor.com/docs/editor-controls/control-media/ but cant find anything there.
$this->start_controls_section(
'section_image_one',
[
'label' => esc_html__( 'Image One' , $this->domain ),
]
);
$this->add_control(
'image',
[
'label' => esc_html__( 'Choose Image', $this->domain ),
'type' => Controls_Manager::MEDIA,
'dynamic' => [
'active' => true,
],
'default' => [
'url' => Utils::get_placeholder_image_src(),
],
]
);
$this->end_controls_section();
$this->start_controls_section(
'section_image_two',
[
'label' => esc_html__( 'Image Two' , $this->domain ),
]
);
$this->add_control(
'image',
[
'label' => esc_html__( 'Choose Image', $this->domain ),
'type' => Controls_Manager::MEDIA,
'dynamic' => [
'active' => true,
],
'default' => [
'url' => Utils::get_placeholder_image_src(),
],
]
);
$this->end_controls_section();
It looks like it is a rather simple solution
.....
$this->add_control(
'image',
.....
As mentioned on https://developers.elementor.com/docs/editor-controls/regular-control/, the "image" mentioned is a control name and needs to be unique.
I need help with saving/updating belongsToMany data with custom field.
I have a tables acl, managers, groups, sections.
I need to save/update data in acl table from sections/edit or sections/add
acl table
id|target_id|target_type|section_id
I need set target_id data 'user' or 'group' depending entities.
edit.ctp
echo $this->Form->control('managers._ids', ['class' => 'multiple', 'multiple' => true]);
echo $this->Form->control('groups._ids', ['class' => 'multiple', 'multiple' => true]);
SectionsTable.php
$this->belongsToMany('Managers', [
'joinTable' => 'acl',
'through' => 'Acl',
'foreignKey' => 'section_id',
'targetForeignKey' => 'target_id',
'conditions' => [
'target_type' => 'user'
]
]);
$this->belongsToMany('Groups', [
'joinTable' => 'acl',
'through' => 'Acl',
'foreignKey' => 'section_id',
'targetForeignKey' => 'target_id',
'conditions' => [
'target_type' => 'group'
]
]);
AclTable.php
$this->belongsTo('Managers', [
'foreignKey' => 'target_id',
'conditions' => [
'target_type' => 'user'
]
]);
$this->belongsTo('Groups', [
'foreignKey' => 'target_id',
'conditions' => [
'target_type' => 'group'
]
]);
I tried to use _joinData, but nothing work
before and after patchEntity()
foreach ($section->groups as &$group) {
$group->_joinData = ['target_type' => 'group'];
}
whithout save()
foreach ($this->request->data['managers']['_ids'] as $id) {
$manager = $this->Sections->Managers->get($id);
$manager->_joinData = new Entity(['target_type' => 'user'], ['markNew' => true]);
$this->Sections->Managers->link($section, [$manager]);
}
$this->request->data has a follow structure:
[
'controller' => '*',
'action' => '*',
'title' => '*',
'managers' => [
'_ids' => [
'0' => '*',
'1' => '*',
'2' => '*',
]
],
'groups' => [
'_ids' => [
'0' => '*',
'1' => '*',
'2' => '*',
]
]
]
Alltimes error: Cannot insert row, some of the primary key values are missing. Got (3, 114, ), expecting (target_id, section_id, target_type)
Now working with following code
$section = $this->Sections->patchEntity($section, $this->request->data, ['associated' => ['Managers', 'Groups']]);
foreach ($section->managers as &$manager) {
$manager->_joinData = new Entity([
'target_id' => $manager->id,
'target_type' => 'user',
'section_id' => $section->id
], ['markNew' => true]);
}
foreach ($section->groups as &$group) {
$group->_joinData = new Entity([
'target_id' => $group->id,
'target_type' => 'group',
'section_id' => $section->id
], ['markNew' => true]);
}
if ($this->Sections->save($section, ['associated' => ['Managers', 'Groups']])) {
$this->Flash->success(__('The section has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The section could not be saved. Please, try again.'));
But, maybe can do it don't listing all fields?
Also I need to create/add new records to acl table when adding new section
But section_id unavailable until save(). Can I save/add acl records by associations
Updated
AclTAble.php
$this->setPrimaryKey(['target_type', 'target_id', 'section_id']);
.
.
.
$this->belongsTo('Managers', [
'foreignKey' => 'target_id',
'conditions' => [
'target_type' => 'user'
]
]);
$this->belongsTo('Groups', [
'foreignKey' => 'target_id',
'conditions' => [
'target_type' => 'group'
]
]);
With primaryKey id, adding and editing entities working, but editing makes dublicates, it's are not perfect for me. And in documentations use composite primaryKey, so i use ['target_id', 'target_type', 'section_id'], with it cake don't make dublicated, but aggain show me error: Cannot insert row, some of the primary key values are missing. Got (8, 197, ), expecting (target_id, section_id, target_type)
I have three tables, and I want know what is the right Find command to pull the right car models based the category chosen.
Category table
---------------------
Cat_ID Category
1 Sedan
2 SUV
3 Truck
Manufacturers tables
--------------------------
CAR_ID Cat_ID Manufacturer
1 1 BMW
2 1 BMW
3 2 Mercedes
4 3 Dodge
Model table
--------------
CAR_ID Model
1 i320
2 i540
3 GL320
4 RAM
I was able to pull the right cars based on manufacturers, but it is not working when I try to pull the cars based on the category.
$catid='Sedan';
$car_id = $this->car->find('all',array('fields' => array('car.id'),'conditions' => array('car.category_id' => $catid),'order' => array('car.id' => 'desc')));
$models = $this->Model->find('all',array('conditions' => array('Model.car_id'=>$car_id),'order' => array('Model.id' => 'desc')));
I want the output to show for the if I pick Sedan, both BMW models i32 and i520.
Use join statement
I don't know what version of cakephp you are using but i am using cakephp 2.0.
$this->Model->find('all',[
'joins' => [
[
'table' => 'Manufacturers',
'alias' => 'Manufacturers',
'type' => 'LEFT',
'conditions' => [
'Category.Cat_ID = Manufacturers.Cat_ID'
]
],
[
'table' => 'Model',
'type' => 'LEFT',
'conditions' => [
'Model.CAR_ID = Manufacturers.CAR_ID'
]
]
]
]);
if you want to restrict what category you will use then use conditions like below
$this->Model->find('all',[
'joins' => [
[
'table' => 'Manufacturers',
'alias' => 'Manufacturers',
'type' => 'LEFT',
'conditions' => [
'Category.Cat_ID = Manufacturers.Cat_ID'
]
],
[
'table' => 'Model',
'type' => 'LEFT',
'conditions' => [
'Model.CAR_ID = Manufacturers.CAR_ID'
]
]
],
'conditions' => [
'Category.Cat_ID' => 1
]
]);
Side note : Using '[]' and array() are technically the same but nevertheless, here is the sample code using array().
$this->Category->find('all',
array(
'joins' => array(
array(
'table' => 'Manufacturers',
'alias' => 'Manufacturers',
'type' => 'LEFT',
'conditions' => array(
'Category.Cat_ID = Manufacturers.Cat_ID'
)
),
array(
'table' => 'Model',
'type' => 'LEFT',
'conditions' => array(
'Model.CAR_ID = Manufacturers.CAR_ID'
)
),
),
'conditions' => array(
'Category.Cat_ID' => 1
)
));
Update 2
$this->Model->find('all',
array(
'joins' => array(
array(
'table' => 'Manufacturers',
'alias' => 'Manufacturers',
'type' => 'LEFT',
'conditions' => array(
'Model.Car_ID = Manufacturers.Car_ID '
)
),
array(
'table' => 'Category',
'type' => 'LEFT',
'conditions' => array(
'Category.Cat_ID= Manufacturers.Cat_ID'
)
),
),
'conditions' => array(
'Category.Cat_ID'=>$cat_id
)
));
The Issue
I have two methods I need to call in my getContacts function. These methods are as follows:
Method 1:
$authenticated = $this->authenticateUserInternal($inputData['auth']);
Method 2:
$result = $this->Consumer->find('first', array( 'conditions' => array('Consumer.id' => 81)));
When I invoke these methods one after the other my resultset in $result does not have data from related tables. It seems like Method 1 is killing the relationship or affecting my results somehow, but it's a completely un-related function in the AppController.
The Code
The the Consumer model has a HABTM relationship to my Users model through a join table consumers_users.
Consumer Model
public $hasAndBelongsToMany = array(
'User' => array( 'className' => 'User', 'joinTable' => 'consumers_users', 'foreignKey' => 'consumer_id', 'associationForeignKey' => 'user_id', ));
User Model
public $hasAndBelongsToMany = array(
'Consumer' => array(
'className' => 'Consumer',
'joinTable' => 'consumers_users',
'foreignKey' => 'user_id',
'associationForeignKey' => 'consumer_id',
));
ConsumersUsers Model
public $belongsTo = array(
'User' => array( 'className' => 'User', 'foreignKey' => 'user_id', ), 'Consumer' => array( 'className' => 'Consumer', 'foreignKey' => 'consumer_id', ), );
My normal result
"Consumer": {
"id": "81",
"fullname": "consumer2",
"email": "consumer2#cici.mailgun.org",
"password": "111111",
"dateregistered": "2013-08-18",
"audio": "0"
}, "User": [
{
"id": "179",
"fullname": "leesa",
"email": "lda#hotmail.com",
"password": "111111",
"imageurl": "http://bridgefiles.s3.amazonaws.com/2013-07-18-144613_IMG_0773.jpeg",
"thumb_url": "http://bridgefiles.s3.amazonaws.com/2013-07-18-144613_IMG_0773.jpeg",
"messagecount": "2",
"dateregistered": "2013-07-29",
"phone1": null,
"phone2": null,
"videoaddress": null,
"ConsumersUser": {
"id": "17",
"consumer_id": "81",
"status": "accepted",
"user_id": "179"
}
},
The Actual Result
"Consumer": {
"id": "81",
"fullname": "consumer2",
"email": "consumer2#cici.mailgun.org",
"password": "111111",
"dateregistered": "2013-08-18",
"audio": "0"
}
You are selecting only the table consumer, I think that the right way is to load the join table and insert recursive 1 to retrieve both table Consumer and User
try this:
$this->loadModel('ConsumersUsers');
$result = $this->ConsumersUsers->find('first', array( 'conditions' => array('Consumer.id' => 81), 'recursive' => 1));
I have a problem with CakePHP. I tested many ways, but can't find a solution for it.
I have these tables:
users
id
name
country_code
countries
code
title
skills
code
title
roles
id
title
events
id
title
country_code
events_users
event_id
user_id
role_id
skill_code
events hasAndBelongsToMany users and events_users is my join table, and when I read data, it gives me something like this:
array(
(int) 0 => array(
'Event' => array(
'id' => '1',
'title' => '40th WorldSkills Competitions',
'year' => '2011',
'country_code' => 'LV',
'created' => '2013-02-08 00:00:00'
),
'User' => array(
(int) 0 => array(
'password' => '*****',
'id' => '14',
'name' => 'test',
'family' => 'testian',
'username' => 'test#mail.me',
'country_code' => 'BR',
'telphone' => '465465',
'avatar' => 'avatar_51299c1da268f20110912043238_rodekamp_04.jpg',
'address' => 'AA',
'admin' => '0',
'created' => '2013-02-24 05:50:37',
'modified' => '2013-02-24 05:50:37',
'EventsUser' => array(
'id' => '1',
'user_id' => '14',
'event_id' => '1',
'role_id' => '1',
'skill_code' => '17',
'manage' => '100'
)
),
(int) 1 => array(
'password' => '*****',
'id' => '5',
'name' => 'John',
'family' => 'Smith',
'username' => 'john#me.com',
'country_code' => 'IE',
'telphone' => '147',
'avatar' => '20120504124040_franziska_peter_ok.jpg',
'address' => 'No 55',
'admin' => '0',
'created' => '2013-02-10 15:24:13',
'modified' => '2013-02-10 15:28:50',
'EventsUser' => array(
'id' => '2',
'user_id' => '5',
'event_id' => '1',
'role_id' => '2',
'skill_code' => '17',
'manage' => '0'
)
),
(int) 2 => array(
'password' => '*****',
'id' => '4',
'name' => 'hadi',
'family' => 'mm',
'username' => 'design.zerzem#gmail.com',
'country_code' => 'AE',
'telphone' => '415456',
'avatar' => '',
'address' => 'sadjfklsaf asd f',
'admin' => '0',
'created' => '2013-02-10 09:01:28',
'modified' => '2013-02-24 06:43:42',
'EventsUser' => array(
'id' => '3',
'user_id' => '4',
'event_id' => '1',
'role_id' => '4',
'skill_code' => '17',
'manage' => '0'
)
)
)
It's ok, but with events_users I want get data about skill and role that are in events_users (skill_code and role_id). How should I create models to give me all this data?
I think reading about "hasMany Through" should answer your question.
Basically, you make a model for your join table, then you can associate it like any other model.
When you go to retrieve your data, use CakePHP's Containable behavior (if you haven't used it before, be prepared to get addicted to it).